Skip to content

Instantly share code, notes, and snippets.

View RhetTbull's full-sized avatar

Rhet Turnbull RhetTbull

View GitHub Profile
@RhetTbull
RhetTbull / resolve_macos_alias.py
Last active October 22, 2024 03:11
Resolve macOS alias path in Python
"""Resolve path to macOS alias file
Given a path to a file that is a macOS alias, resolve the path to the original file
Requires: pip install pyobjc-core
"""
from Foundation import (
NSURL,
NSData,
@RhetTbull
RhetTbull / resolve_macos_alias_path.py
Created May 27, 2024 00:21
Resolve path to original file pointed to by macOS alias
"""Resolve path to original file pointed to by macOS alias
To use:
pip install pyobjc-core
python3 resolve_macos_alias_path.py /path/to/alias
"""
from Foundation import (
NSURL,
@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):