Skip to content

Instantly share code, notes, and snippets.

View RhetTbull's full-sized avatar

Rhet Turnbull RhetTbull

View GitHub Profile
@RhetTbull
RhetTbull / whichmodule.py
Created July 30, 2019 01:18
Simple command line tool to print path of a given python module (passed as arg on the command line)
#!/usr/bin/env python3
import os
import sys
import importlib
if __name__ == "__main__":
if len(sys.argv) > 1:
modname = sys.argv[1]
try:
@RhetTbull
RhetTbull / fullpath.py
Created August 4, 2019 19:19
Print full path of a file and copy the path to the OS X clipboard
#!/usr/bin/env python3
import os.path
import os
import sys
import subprocess
def write_to_clipboard(output):
process = subprocess.Popen(
'pbcopy', env={'LANG': 'en_US.UTF-8'}, stdin=subprocess.PIPE)
@RhetTbull
RhetTbull / jsondict.py
Last active September 10, 2019 11:41
Python dictionary class that stores its data in a JSON file for persistence--useful for storing key/value pairs
import collections
import traceback
import json
import sys
import os.path
class JSONDict(collections.UserDict):
def __init__(self, file_name, autosave=False, default=None, data=None):
if file_name == None:
@RhetTbull
RhetTbull / copyfile_with_osx_metadata.py
Last active September 30, 2019 16:48
Python function to copy Mac OSX files while preserving metadata (ACLs, extended attributes, resource forks and optionally Finder comments)
import pathlib
import subprocess
import osxmetadata
def copyfile_with_osx_metadata(src, dest, overwrite_dest=False, findercomments=False):
""" copy file from src (source) to dest (destination) """
""" src is path with filename, dest is path only """
""" if overwrite_dest = False (default), will create dest file in form 'filename (1).ext', """
""" 'filename (2).ext', and so on if dest file already exists"""
@RhetTbull
RhetTbull / photokit.py
Last active February 23, 2024 22:53
Access images from Apple Photos and associated metadata. Uses PyObjC to call native PhotoKit framekwork to access the user's Photos library.
""" Use Apple PhotoKit via PyObjC bridge to download and save a photo
from users's Photos Library
Copyright Rhet Turnbull, 2020. Released under MIT License.
Required pyobjc >= 6.2 see: https://pypi.org/project/pyobjc/ """
import platform
import logging
import sys
import CoreServices
@RhetTbull
RhetTbull / FreezableRecord.py
Last active February 8, 2020 20:17
A simple record class that can be made immutable (frozen) but also behaves like a mutable named tuple when not frozen
from types import SimpleNamespace
class FreezableRecord(SimpleNamespace):
""" A simple namespace record class that allows attributes to be made immutable (frozen) after creation
Must be initialized with allowed attributes at creation via **kwargs
Behaves somewhat like a namedtuple in that once initialized, new attributes are not permitted,
however, unlike a nametuple, attribute values are mutable (unless frozen)
Attempt to set a new attribute after initialization will raise AttributeError """
@RhetTbull
RhetTbull / apple-photo-schema.sql
Created May 4, 2020 11:41 — forked from aslakr/apple-photo-schema.sql
sqlite ".schema" dump
CREATE TABLE ZADDITIONALASSETATTRIBUTES (
Z_PK INTEGER PRIMARY KEY, Z_ENT INTEGER,
Z_OPT INTEGER, ZALLOWEDFORANALYSIS INTEGER,
ZCAMERACAPTUREDEVICE INTEGER, ZCLOUDAVALANCHEPICKTYPE INTEGER,
ZCLOUDGROUPINGSTATE INTEGER, ZCLOUDKINDSUBTYPE INTEGER,
ZCLOUDRECOVERYSTATE INTEGER, ZCLOUDSTATERECOVERYATTEMPTSCOUNT INTEGER,
ZDESTINATIONASSETCOPYSTATE INTEGER,
ZEMBEDDEDTHUMBNAILHEIGHT INTEGER,
ZEMBEDDEDTHUMBNAILLENGTH INTEGER,
ZEMBEDDEDTHUMBNAILOFFSET INTEGER,
@RhetTbull
RhetTbull / Apple Photos database schema 10.12.6
Last active May 24, 2023 08:51
sqlite3 ".schema" of Photos Library.photoslibrary/database/photos.db on 10.12.6
CREATE VIRTUAL TABLE RidList_VirtualReader using RidList_VirtualReaderModule;
CREATE VIRTUAL TABLE Array_VirtualReader using Array_VirtualReaderModule;
CREATE TABLE LiGlobals (modelId integer primary key, keyPath varchar, value varchar, blobValue blob);
CREATE INDEX LiGlobals_keyPath_index on LiGlobals(keyPath);
CREATE VIRTUAL TABLE LiGlobals_VirtualBufferReader using VirtualBufferReaderModule;
CREATE TABLE LiLibHistory (modelId integer primary key, modDate timestamp, eventType varchar, metaSchemaVersion integer, libraryVersion integer, comment varchar);
CREATE INDEX LiLibHistory_eventType_index on LiLibHistory(eventType);
CREATE VIRTUAL TABLE LiLibHistory_VirtualBufferReader using VirtualBufferReaderModule;
CREATE TABLE LiStringAtom (modelId integer primary key, string varchar);
CREATE INDEX LiStringAtom_string_index on LiStringAtom(string);
@RhetTbull
RhetTbull / Apple Photos database schema 10.14.6
Created May 9, 2020 14:45
sqlite3 ".schema" for Photos Library.photoslibrary/database/photos.db on 10.14.6
CREATE TABLE LiGlobals (modelId integer primary key, keyPath varchar, value varchar, blobValue blob);
CREATE TABLE LiLibHistory (modelId integer primary key, modDate timestamp, eventType varchar, metaSchemaVersion integer, libraryVersion integer, comment varchar);
CREATE TABLE LiStringAtom (modelId integer primary key, string varchar);
CREATE TABLE LiTableDef (modelId integer primary key, tableName varchar, databaseName varchar, digest varchar, ridIndexField varchar, tableOptions integer);
CREATE TABLE LiPropertyDef (modelId integer primary key, keyPath varchar, tableId integer, propertyType integer, storage integer, desiredStorage integer, options integer);
CREATE TABLE LiPropertyHistory (modelId integer primary key, tableId integer, modDate timestamp, originatingClient integer, changeId integer, keyPathList blob, changeType integer, memo varchar, reason integer, changeIdList blob);
CREATE TABLE RKAdjustmentData (modelId integer primary key autoincrement, uuid varchar, versionId integer, data blob, originato
""" Find all photos in Apple photos with a detected face and
add them to a new album named """
import osxphotos
# there's more than one applescript package on pypi
# the one you need is py-applescript:
# pip install py-applescript
from applescript import AppleScript