Skip to content

Instantly share code, notes, and snippets.

View RhetTbull's full-sized avatar

Rhet Turnbull RhetTbull

View GitHub Profile
@RhetTbull
RhetTbull / update_favorites.py
Last active August 6, 2022 19:44
Update favorites in Photos based on values in a JSON file produced by this iOS Shortcut: https://www.icloud.com/shortcuts/2057bcae53b146b3847260dc0cced1b6
"""Update favorites in Photos based on values in a JSON file produced by this iOS Shortcut: https://www.icloud.com/shortcuts/2057bcae53b146b3847260dc0cced1b6
First install osxphotos: https://github.com/RhetTbull/osxphotos
Run with `osxphotos run update_favorites.py --help` to see help
The input JSON file has form:
{"28":{"date":"2021-08-05 13:18:37.232-0700","name":"IMG_5702"}}
Where "28" is the index (not used), date is photo creation date and name is stem of photo name.
@RhetTbull
RhetTbull / set_wallpaper.py
Created November 23, 2021 14:07
Set Mac Desktop Wallpaper from python
"""Programatically set the Mac Desktop wallpaper from python
Also shows how to load a framework with pyobjc
Source: https://github.com/jbmorley/download-bing-image
"""
import objc
from CoreFoundation import CFUUIDCreateFromString
from Foundation import NSBundle
@RhetTbull
RhetTbull / offset.py
Last active October 8, 2021 20:07
Get offset from UTC as seconds for a python datetime.datetime timezone aware object
"""Get offset seconds from UTC from a datetime"""
import datetime
time1 = datetime.datetime(2021, 9, 1, 0, 0, 0, 0, tzinfo=datetime.timezone.utc)
offset_seconds = time1.tzinfo.utcoffset(time1).total_seconds()
print(time1, offset_seconds)
time2 = datetime.datetime(
2021, 9, 1, 0, 0, 0, 0, tzinfo=datetime.timezone(datetime.timedelta(seconds=-25200))
@RhetTbull
RhetTbull / pre-commit.sh
Created September 22, 2021 03:14 — forked from intjonathan/pre-commit.sh
pre-commit hook (add to .git/hooks/pre-commit) to refuse to commit debugger strings
#!/bin/sh
# Refuse to commit files with the string ZZZ present
# I frequently use ZZZ to mark a spot where I'm working on an issue so I can come back to it
#
NOCOMMIT="ZZZ"
files=$(git diff-index --name-status --cached HEAD | grep -v ^D | cut -c3-)
if [ "$files" != "" ]
@RhetTbull
RhetTbull / sqlite.py
Created August 31, 2021 20:16 — forked from michalc/sqlite.py
Use libsqlite3 directly from Python with ctypes: without using the built-in sqlite3 Python package, and without compiling anything
# From https://stackoverflow.com/a/68876046/1319998, which is itself inspired by https://stackoverflow.com/a/68814418/1319998
from contextlib import contextmanager
from collections import namedtuple
from ctypes import cdll, byref, string_at, c_char_p, c_int, c_double, c_int64, c_void_p
from sys import platform
def query(db_file, sql, params=()):
libsqlite3 = cdll.LoadLibrary({'linux': 'libsqlite3.so', 'darwin': 'libsqlite3.dylib'}[platform])
@RhetTbull
RhetTbull / uti.csv
Created June 28, 2021 04:10
A CSV list of Apple macOS Universal Type Identifiers (UTIs) and associated extensions & MIME types. Generated programmaticaly with calls to UTTypeCreatePreferredIdentifierForTag (https://developer.apple.com/documentation/coreservices/1448939-uttypecreatepreferredidentifierf)
extension UTI preferred_extension MIME_type
c public.c-source c None
f public.fortran-source f None
h public.c-header h None
i public.c-source.preprocessed i None
l public.lex-source l None
m public.objective-c-source m None
o public.object-code o None
r com.apple.rez-source r None
s public.assembly-source s None
@RhetTbull
RhetTbull / adjustments.py
Created February 18, 2021 05:56
Retrieve adjustment/edit data from edited photos in Apple Photos
""" Read adjustments data for photos edited in Apple's Photos.app
In Catalina and Big Sur, the adjustments data (data about edits done to the photo)
is stored in a plist file in
~/Pictures/Photos Library.photoslibrary/resources/renders/X/UUID.plist
where X is first character of the photo's UUID string and UUID is the full UUID,
e.g.: ~/Pictures/Photos Library.photoslibrary/resources/renders/3/30362C1D-192F-4CCD-9A2A-968F436DC0DE.plist
Thanks to @neilpa who figured out how to decode this information:
Reference: https://github.com/neilpa/photohack/issues/4
"""
@RhetTbull
RhetTbull / merge_apple_photos_libraries.py
Last active January 25, 2021 02:28
Proof of concept to merge Apple Photos libraries -- still very unfinished
"""Proof of concept showing how to merge Apple Photos libraries including most of the metadata """
# Currently working:
# * places photos into correct albums and folder structure
# * sets title, description, keywords, location
# Limitations:
# * doesn't currently handle Live Photos or RAW+JPEG pairs
# * only merges the most recent version of the photo (edit history is lost)
# * very limited error handling
# * doesn't merge Person In Image
@RhetTbull
RhetTbull / vision.py
Last active June 2, 2025 16:39
Use Apple's Vision framework from Python to detect text in images
""" Use Apple's Vision Framework via PyObjC to detect text in images
To use:
python3 -m pip install pyobjc-core pyobjc-framework-Quartz pyobjc-framework-Vision wurlitzer
"""
import pathlib
@RhetTbull
RhetTbull / tempdiskimage.py
Last active April 10, 2022 11:10
Create and mount a temporary disk image (with auto-cleanup) on MacOS. Useful for when you need a temp file on a different file system.
""" Create and mount a temporary disk image on MacOS """
import pathlib
import platform
import subprocess
import tempfile
import time
class TempDiskImage: