PyObjC OSAKit Framework Wrappers
PyObjC is a bridge between Python and the Objective-C runtime, enabling Python scripts to interact with Apple's Cocoa frameworks. This specific package, `pyobjc-framework-osakit`, provides Python bindings for the OSAKit framework on macOS, allowing for the creation, compilation, and execution of AppleScripts and other scripting components. The current version is 12.1. Releases are frequent, often correlating with new macOS SDKs and Python version support changes.
Warnings
- breaking PyObjC 12.0 (and thus `pyobjc-framework-osakit` 12.0) dropped support for Python 3.9. Ensure your project uses Python 3.10 or newer.
- breaking PyObjC 11.0 dropped support for Python 3.8. Ensure your project uses Python 3.9 or newer.
- gotcha Objective-C method selectors with colons (e.g., `someMethod:withArgument:`) are translated to Python methods using underscores (e.g., `someMethod_withArgument_`). Remember to add a trailing underscore for single-argument methods.
- gotcha PyObjC 11.1 aligned the core bridge's behavior with `clang`'s Automatic Reference Counting (ARC) documentation for initializer methods. Methods in the 'init' family now correctly steal a reference to `self` and return a new reference, which might affect reference counting in specific, complex scenarios.
- gotcha In PyObjC 10.3, the ability to use `__init__` in Python classes that also implement `__new__` (or inherit from a class that implements it) was temporarily broken, but later reintroduced in 10.3.1. However, classes relying on `PyObjC`'s default `__new__` still cannot use `__init__`.
Install
-
pip install pyobjc-framework-osakit
Imports
- OSAKit
import OSAKit
- Foundation
import Foundation
Quickstart
import OSAKit
import Foundation
import os
# Source for a simple AppleScript
script_source = """
display dialog "Hello from PyObjC OSAKit!" with title "PyObjC"
return "AppleScript executed successfully."
"""
try:
# Create an OSAScript instance
script = OSAKit.OSAScript.alloc().initWithSource_(script_source)
# Prepare an error dictionary (Objective-C style error handling)
error = Foundation.NSDictionary.dictionary()
# Compile the script
compiled = script.compileAndReturnError_(error)
if not compiled:
print(f"Error compiling script: {error}")
else:
print("Script compiled successfully.")
# Execute the script
result_desc, error_exec = script.executeAndReturnError_(error)
if error_exec:
print(f"Error executing script: {error_exec}")
else:
# OSADisplayResult is often an NSAppleEventDescriptor, use stringValue() to get Python string
print(f"Script output: {result_desc.stringValue()}")
except Exception as e:
print(f"An error occurred: {e}")