PyObjC ScreenTime Framework
PyObjC is a bridge between Python and Objective-C, enabling Python scripts to use and extend existing Objective-C class libraries, primarily Apple's Cocoa frameworks. `pyobjc-framework-screentime` provides Python wrappers for the macOS ScreenTime framework, allowing access to its APIs. 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).
Install
-
pip install pyobjc-framework-screentime
Imports
- ScreenTime
import ScreenTime
Quickstart
import Foundation
import ScreenTime # Import the specific framework
def demonstrate_pyobjc_basics():
"""Demonstrates basic PyObjC object creation and method calls."""
# Example using a common Foundation class
now = Foundation.NSDate.date() # Pythonic instantiation (available since PyObjC 10.3)
print(f"Current NSDate object: {now}")
print(f"Description of NSDate: {now.description()}")
# To use ScreenTime, you would typically interact with its specific classes and functions.
# For instance, if ScreenTime had a class like 'STConfiguration' (hypothetical example):
# try:
# config_manager = ScreenTime.STConfiguration.alloc().init()
# print(f"ScreenTime configuration manager: {config_manager}")
# except AttributeError:
# print("Hypothetical STConfiguration class not found or API not accessible.")
print("Note: Actual ScreenTime API usage requires specific macOS versions, permissions, and understanding of Apple's documentation.")
if __name__ == "__main__":
demonstrate_pyobjc_basics()