Skip to content

Instantly share code, notes, and snippets.

View RhetTbull's full-sized avatar

Rhet Turnbull RhetTbull

View GitHub Profile
@RhetTbull
RhetTbull / pyver.sh
Last active October 9, 2023 16:46
Switch between multiple python versions on macOS when using python.org official installer
#!/bin/bash
# Change symlink to Python version in /usr/local/bin/python to point to a
# specified version as installed by the official python.org installer for macOS
# usage: ./pyver.sh <version>
# when called without arguments, prints the currently linked Python version
# This is useful for use with pyenv (https://github.com/pyenv/pyenv) which can only
# handle a single "system" python version and offers no way to switch between multiple
# system pythons
@RhetTbull
RhetTbull / copyfile.py
Created September 18, 2023 00:24
Copy a file on macOS with Python using native NSFileManager method which takes advantage of copy-on-write on APFS formatted volumes.
"""Copy a file on macOS using native API.
This allows copied files to use copy-on-write when used on a volume formatted with APFS.
When used on an APFS volume, a file copied with this function will be copied almost instantly
and will not use any additional disk space until the file is modified.
To use, you will need to install pyobjc-core and pyobjc-framework-Cocoa:
`python3 -m pip install pyobjc-core pyobjc-framework-Cocoa`
@RhetTbull
RhetTbull / count_photos.py
Last active June 22, 2023 18:22
Count photos in Photos.app matching a given criteria
"""Count photos for a given query criteria"""
from __future__ import annotations
import osxphotos
from osxphotos.cli import query_command
@query_command
def count_photos(photos: list[osxphotos.PhotoInfo], **kwargs):
@RhetTbull
RhetTbull / tkoutput.py
Created June 22, 2023 06:08
Redirect output of a subprocess to a tkinter text box in real-time
"""Redirect output of a command to a tkinter text box in real-time"""
from __future__ import annotations
import subprocess
import threading
import tkinter as tk
def update_text_box(text_box: tk.Text, proc: subprocess.Popen):
@RhetTbull
RhetTbull / noterescue.py
Last active March 15, 2023 23:52
Extract embedded media in Apple Notes notes exported to a bplist file
"""Rescue lost Apple Notes notes, reference: https://fosstodon.org/@[email protected]/109992039449199371
To run this:
1. Install python (I recommend the latest version of python 3 from python.org)
2. Install the dependencies: python3 -m pip install bpylist2
3. Run the script: python3 noterescue.py <path to note file(s)>
The extracted files will be saved in the parent directory of the note file.
@RhetTbull
RhetTbull / news.py
Last active May 10, 2025 22:24
Extract your "Saved Stories" articles from the Apple News app on macOS (thanks to @eecue who wrote much of this)
"""Get your "Saved Stories" articles from Apple News
Thanks to Dave Bullock (https://github.com/eecue) who's idea this was and who wrote the extract_info_from_apple_news function
This script requires the following modules be pip installed:
* bs4
* requests
Save this script to a file called news.py and run it with Python 3.9 or later
@RhetTbull
RhetTbull / finfo.sh
Last active October 2, 2022 16:34
Simple shell script to open the macOS Finder's "Info Window" for a file or folder from command line
#!/bin/bash
# Simple script to open the macOS Finder's "Info Window" for a file or folder.
# Usage: finfo [file or folder]
# This relies on realpath being installed (brew install coreutils)
if [ -z "$1" ]; then
echo "Usage: finfo [file or folder]"
exit 1
@RhetTbull
RhetTbull / setmd.py
Last active September 23, 2022 18:22
Set metadata on macOS files using undocumented function MDItemSetAttribute
"""Set metadata on macOS files using undocumented function MDItemSetAttribute
Background: Apple provides MDItemCopyAttribute to get metadata from files:
https://developer.apple.com/documentation/coreservices/1427080-mditemcopyattribute?language=objc
but does not provide a documented way to set file metadata.
This script shows how to use the undocumented function MDItemSetAttribute to do so.
`pip install pyobjc` to install the required Python<-->Objective C bridge package.
@RhetTbull
RhetTbull / qrcodes.py
Created September 5, 2022 20:22
Detect QR codes in python on MacOS using CoreImage API via PyObjC
"""Uses CoreImage API via PyObjC to detect QR Codes in images on MacOS.
This is a simple wrapper around the CIDetector API and only returns the text of the QR Code.
It does not return the location of the QR Code in the image.
Reference: https://developer.apple.com/documentation/coreimage/cidetector/detector_types?language=objc
"""
from typing import List
@RhetTbull
RhetTbull / fmydocstrings.py
Last active April 18, 2023 23:50
Python decorator to allow use of f-strings in docstrings (something not normally supported by Python)
"""Simple decorator that applies f-string formatting to docstrings
To use, simply apply `@fmydocstring` to your function
Only global variables are accessible for interpolation.
"""
import functools