Skip to content

Instantly share code, notes, and snippets.

@pudquick
pudquick / receipts.py
Created August 18, 2016 17:14
Programmatically access package receipt information using the OS X PrivateFramework PackageKit (same one pkgutil uses) with python and pyobjc
import objc
packagekit_bundle = objc.loadBundle('PackageKit', module_globals=globals(), bundle_path='/System/Library/PrivateFrameworks/PackageKit.framework', scan_classes=False)
PKReceipt = objc.lookUpClass('PKReceipt')
receipts = PKReceipt.receiptsOnVolumeAtPath_('/')
first_receipt = receipts[0]
# Things you can look up:
# installPrefixPath
@pudquick
pudquick / diskman.py
Last active December 14, 2022 17:03
Light pyobjc wrapper around the PrivateFramework DiskManagement.framework for direct access to disk devices and information about them
import objc
from Foundation import NSBundle
# Predefine some opaque types
DASessionRef = objc.createOpaquePointerType('DASessionRef', b'^{__DASession=}', None)
DADiskRef = objc.createOpaquePointerType('DADiskRef', b'^{__DADisk=}', None)
# Load DiskManagement framework classes
DiskManagment = objc.loadBundle('DiskManagment', globals(), bundle_path='/System/Library/PrivateFrameworks/DiskManagement.framework')
@pudquick
pudquick / lockscreen_pyobjc.py
Created June 15, 2016 17:14
Locking the screen immediately with pyobjc
import objc
from Foundation import NSBundle
login_bundle = NSBundle.bundleWithPath_('/System/Library/PrivateFrameworks/login.framework')
functions = [('SACLockScreenImmediate', '@'),]
objc.loadBundleFunctions(login_bundle, globals(), functions)
# Lock the screen regardless of security settings or who is logged in
result = SACLockScreenImmediate()
@pudquick
pudquick / fastuser_logincheck.py
Created April 26, 2016 05:39
Using CGSSessionCopyAllSessionProperties to detect logged-in users the way the Fast User switching menu extra does on OS X with python and pyobjc
import objc
from Foundation import NSBundle
CG_bundle = NSBundle.bundleWithIdentifier_('com.apple.CoreGraphics')
functions = [("CGSSessionCopyAllSessionProperties", b"@"),]
objc.loadBundleFunctions(CG_bundle, globals(), functions)
# example usage: graphical_security_sessions = CGSSessionCopyAllSessionProperties()
@pudquick
pudquick / filevault2_api.py
Last active March 25, 2019 05:17
Programmatic access to usernames, icons, encryption status, and more for FileVault2 for OS X
# This code must run as root
# We're mixing ObjC and C-style dylibs, so this is really fun
# The only reason we're doing this is that the OS is _really really_ picky about trying to do
# ANYTHING with the CoreStorage Family Properties CFDictionary that's in-memory EXCEPT for
# making a mutable copy of it.
# Once we've done that, we can bring it into pyObjC to play nicely with the data.
import objc
@pudquick
pudquick / get_platform.py
Last active August 18, 2022 21:02
Get Mac's serial number, hardware UUID, and board-id via python
import objc
from Foundation import NSBundle
IOKit_bundle = NSBundle.bundleWithIdentifier_('com.apple.framework.IOKit')
functions = [("IOServiceGetMatchingService", b"II@"),
("IOServiceMatching", b"@*"),
("IORegistryEntryCreateCFProperty", b"@I@@I"),
]
@pudquick
pudquick / lockscreen.py
Created December 23, 2015 19:12
Programmatically immediately lock the screen of a Mac running OS X, regardless of security settings, screensaver settings, or Fast User Switch settings
from ctypes import CDLL
loginPF = CDLL('/System/Library/PrivateFrameworks/login.framework/Versions/Current/login')
result = loginPF.SACLockScreenImmediate()
@homebysix
homebysix / EclipseIDE.download.recipe
Last active November 13, 2015 01:08
EclipseIDE.download.recipe
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Description</key>
<string>Downloads the current release version of Eclipse.</string>
<key>Identifier</key>
<string>com.github.sheagcraig.download.EclipseIDE</string>
<key>Input</key>
<dict>
@pudquick
pudquick / mount_shares_better.py
Last active January 19, 2023 22:07
Mounting shares in OS X using python and pyobjc - works with OS X 10.8+
import objc, CoreFoundation, Foundation
class attrdict(dict):
__getattr__ = dict.__getitem__
__setattr__ = dict.__setitem__
NetFS = attrdict()
# Can cheat and provide 'None' for the identifier, it'll just use frameworkPath instead
# scan_classes=False means only add the contents of this Framework
NetFS_bundle = objc.initFrameworkWrapper('NetFS', frameworkIdentifier=None, frameworkPath=objc.pathForFramework('NetFS.framework'), globals=NetFS, scan_classes=False)
import sys, os, os.path, tempfile, plistlib, shutil
class TempApp(object):
def __init__(self, infoPlist_dict, app_path=None, bundle_name='TempApp', cleanup=True, app_icon=None):
# infoPlist_dict: A dict containing key values that should be set/overridden
# vs. the normal Python.app keys.
# app_path: The path to where your app should go. Example: '/usr/local/myOrgStuff'
# This directory needs to pre-exist. If app_path is left at None,
# a temporary directory will be created and used and the value of
# cleanup will be forced to True