PyObjC AppleScriptObjC Framework
pyobjc-framework-applescriptobjc provides Python wrappers for the AppleScriptObjC framework on macOS. It enables Python applications and scripts to interact with and extend AppleScript-based application bundles. The current version is 12.1, and releases typically align with new macOS SDKs and Python version support, ensuring compatibility with the latest Apple technologies.
Warnings
- breaking PyObjC frequently drops support for older Python versions. PyObjC 12.0 dropped support for Python 3.9. PyObjC 11.0 dropped support for Python 3.8. Ensure your Python environment meets the `requires_python` spec (>=3.10 for version 12.1) to avoid compatibility issues.
- breaking The behavior of initializer methods (`init` family) was changed in PyObjC 11.1 to align with Objective-C's Automatic Reference Counting (ARC) semantics. Methods in the 'init' family now correctly steal a reference to `self` and return a new reference. This may affect custom object instantiation patterns, particularly those involving `SomeClass.alloc().init...()`.
- gotcha When subclassing Objective-C classes in Python, `__init__` behavior with a custom `__new__` can be tricky. PyObjC 10.3 introduced a regression, partially fixed in 10.3.1. While `__init__` can be used when a class or its superclasses implement a user-defined `__new__`, code relying on PyObjC's automatically provided `__new__` cannot necessarily use `__init__` in the traditional Pythonic way.
- gotcha While PyObjC 11 and later (including 12.x) support Python 3.13+'s experimental free-threading (PEP 703), many underlying Apple frameworks (especially GUI-related ones) are not inherently thread-safe. Concurrent access to mutable Objective-C collections (e.g., `NSMutableArray`) or GUI elements without explicit locking can lead to race conditions or crashes.
- gotcha PyObjC is a macOS-specific library and does not function on other operating systems like Linux or Windows. Its entire purpose is to bridge Python with Objective-C frameworks available on macOS.
- gotcha Installation of PyObjC often requires the Xcode Command Line Tools to be installed, as it relies on system headers and compilers. Missing these tools can lead to compilation errors during `pip install`.
Install
-
pip install pyobjc-framework-applescriptobjc
Imports
- AppleScriptObjC
import AppleScriptObjC
Quickstart
import objc
from Foundation import NSBundle
# The AppleScriptObjC framework is primarily used within AppleScript-based
# application bundles to extend their functionality.
# A direct functional example outside this context is complex.
# This snippet demonstrates how to check if the framework can be loaded.
try:
# Attempt to get the NSBundle for the AppleScriptObjC framework
bundle = NSBundle.bundleWithPath_(
"/System/Library/Frameworks/AppleScriptObjC.framework"
)
if bundle and bundle.load():
print("AppleScriptObjC framework loaded successfully.")
# At this point, classes and functions from the framework would be available
# through 'AppleScriptObjC' or through runtime introspection, e.g.,
# AppleScriptObjC.ASOCScript or similar if defined.
else:
print("Failed to load AppleScriptObjC framework or it's not present.")
except Exception as e:
print(f"An error occurred: {e}")