PyObjC ApplicationServices Framework
PyObjC provides Python bindings for Objective-C frameworks on macOS, enabling Python developers to interact with Apple's system APIs. `pyobjc-framework-applicationservices` offers wrappers for the ApplicationServices umbrella framework, which includes various core services like CoreGraphics, CoreText, and HIServices. The library is currently at version 12.1 and maintains an active release cadence, typically aligning with macOS and Python version updates.
Warnings
- breaking PyObjC frequently drops support for older Python versions. PyObjC 12.0 dropped Python 3.9, and PyObjC 11.0 dropped Python 3.8. Always check the `requires_python` metadata or release notes to ensure compatibility with your Python environment.
- breaking PyObjC 11.1 aligned its core bridge 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. This changed behavior for `[NSObject alloc]` proxies and can affect custom `init` implementations, potentially leading to crashes if old patterns are used.
- gotcha Version 10.3 initially broke `__init__` usage for Python subclasses of Objective-C classes, particularly when a user implemented `__new__` or when relying on PyObjC's `__new__` behavior. This was partially reverted in 10.3.1 to reintroduce `__init__` support when a user implements `__new__`.
- gotcha PyObjC 11.0 introduced experimental support for free-threading (PEP 703) with Python 3.13+. While PyObjC protects its own implementation, Apple's frameworks are not necessarily thread-safe (e.g., `NSMutableArray` for concurrent updates, non-atomic properties, GUI classes on non-main threads).
- gotcha `NSString` and `NSMutableString` are marked as 'final' in PyObjC versions 11.1 and later. Attempting to subclass these in Python will result in crashes due to special handling within PyObjC.
- gotcha Installing PyObjC (especially from source or if binary wheels aren't suitable) requires Xcode or the Command Line Tools to be installed on macOS, including a compatible SDK for the target macOS version.
Install
-
pip install pyobjc-framework-applicationservices -
pip install 'pyobjc[allbindings]'
Imports
- CoreGraphics
from ApplicationServices import CoreGraphics
- kCGColorSpaceGenericRGB
from ApplicationServices import CoreGraphics; colorSpace = CoreGraphics.CGColorSpaceCreateDeviceRGB()
Quickstart
import ApplicationServices
import os
def get_display_bounds():
"""
Retrieves the bounds of the primary display using CoreGraphics
from ApplicationServices.
"""
# Access CoreGraphics functions via ApplicationServices.CoreGraphics
main_display_id = ApplicationServices.CoreGraphics.CGMainDisplayID()
bounds = ApplicationServices.CoreGraphics.CGDisplayBounds(main_display_id)
# PyObjC automatically bridges CGRect to a dictionary-like object
x = bounds.origin.x
y = bounds.origin.y
width = bounds.size.width
height = bounds.size.height
print(f"Primary display bounds: x={x}, y={y}, width={width}, height={height}")
return {"x": x, "y": y, "width": width, "height": height}
if __name__ == '__main__':
# This example does not require an active NSApplication runloop
# for just getting display bounds.
display_info = get_display_bounds()
assert display_info['width'] > 0 and display_info['height'] > 0
print("Successfully retrieved display information.")