Skip to content

Instantly share code, notes, and snippets.

View RhetTbull's full-sized avatar

Rhet Turnbull RhetTbull

View GitHub Profile
@RhetTbull
RhetTbull / news.py
Last active March 12, 2023 01:36
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
@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])