Skip to content

Instantly share code, notes, and snippets.

View RhetTbull's full-sized avatar

Rhet Turnbull RhetTbull

View GitHub Profile
@RhetTbull
RhetTbull / get_latest_pypi_version.py
Created February 14, 2024 14:54
Get latest version of a python package from PyPI
"""Given a PyPI package name, print the latest version number of the package.
This uses the standard library instead of requests to avoid adding a dependency to the project.
"""
from __future__ import annotations
import json
import ssl
import sys
@RhetTbull
RhetTbull / pyapp.sh
Last active February 11, 2024 00:40
Run PyApp to build a python app
#!/bin/bash
# PyApp Runner
# This script is used to build and copy the PyApp executable
# See https://ofek.dev/pyapp/latest/how-to/ for more information on PyApp.
# Change this to the path where pyapp is installed
PYAPP="/path/to/pyapp-latest"
# Check that both arguments are provided
@RhetTbull
RhetTbull / pyapp_runner.sh
Last active February 11, 2024 22:06
Run PyApp on a remote server (tested only on macOS)
#!/bin/bash
# PyApp Runner
# This script is used to build and copy the PyApp executable from a remote server.
# See https://ofek.dev/pyapp/latest/how-to/ for more information on PyApp.
# The remote server must have PYAPP defined in the ~/.zshenv file
# to point to the install location of pyapp.
# For example:
# echo 'export PYAPP="/Users/johndoe/code/pyapp-latest"' >> ~/.zshenv
# For code signing to work via ssh, you must first run this one time on the machine via GUI
@RhetTbull
RhetTbull / minimal.py
Last active March 17, 2024 16:43
Implements a minimalist macOS Cocoa application in python using pyobjc that doesn't require a NIB file
"""Implements a minimalist Cocoa application using pyobjc
Based on the example found here:
https://www.cocoawithlove.com/2010/09/minimalist-cocoa-programming.html
To run this:
- save as minimal.py
- pip3 install pyobjc
- python3 minimal.py
@RhetTbull
RhetTbull / image_metadata.py
Last active October 23, 2023 19:03
Get EXIF/IPTC/XMP metadata from an image file on macOS in python using CoreGraphics via pyobjc
"""Get EXIF/IPTC/XMP metadata from an image file on macOS using CoreGraphics via pyobjc.
Requires installation of the following [pyobjc](https://pyobjc.readthedocs.io/en/latest/index.html) packages:
`pip install pyobjc-core pyobjc-framework-Quartz`
This module provides 3 public functions:
- get_image_properties(): Returns the metadata properties dictionary from the image at the given path.
- get_image_xmp_metadata(): Returns the XMP metadata dictionary from the image at the given path.
@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/@Cyberneticist@hachyderm.io/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.