PyObjC SafariServices Framework
pyobjc-framework-safariservices provides Python bindings for Apple's SafariServices.framework on macOS, enabling Python applications to integrate with Safari functionalities like content blockers and shared web credentials. It is part of the larger PyObjC project, which wraps many macOS frameworks. The current version is 12.1, with releases typically aligning with macOS SDK updates and Python version support cycles.
Warnings
- breaking PyObjC major versions frequently drop support for older Python versions. For example, v12.0 dropped Python 3.9, and v11.0 dropped Python 3.8. Ensure your Python environment meets the `requires_python` specification for the PyObjC version you are using.
- breaking PyObjC releases are tightly coupled with macOS SDK versions. Upgrading macOS might require upgrading PyObjC to a version that includes updated framework bindings for the new SDK, or vice-versa. Using an incompatible combination can lead to missing symbols or unexpected behavior.
- gotcha PyObjC v11.1 introduced significant changes to how initializer (`init` family) methods handle automatic reference counting (ARC). Previously, PyObjC might not have correctly modeled that `init` methods steal a reference to `self` and return a new reference. This can lead to memory management issues if not handled correctly when implementing custom Cocoa classes in Python.
- gotcha PyObjC's handling of `__init__` and `__new__` for user-defined subclasses of Cocoa objects can be tricky. In v10.3, `__init__` was disabled when a user-defined `__new__` was present, causing breakage. While v10.3.1 re-enabled `__init__` for specific `__new__` scenarios, it highlights a potential footgun when customizing object creation.
- gotcha Frameworks can be deprecated or removed by Apple in newer macOS versions. PyObjC will drop bindings for such frameworks when they can no longer be built against the latest SDK (e.g., IMServicePlugIn in v10.0). Code relying on deprecated frameworks may stop working unexpectedly.
Install
-
pip install pyobjc-framework-safariservices -
pip install pyobjc
Imports
- SafariServices
import SafariServices
Quickstart
import objc
from Foundation import NSURL
import SafariServices
# Get the Python class for SFSafariViewController
# This class is part of SafariServices.framework
SFSafariViewController = SafariServices.SFSafariViewController
# Create a sample URL
url = NSURL.URLWithString_("https://www.example.com")
# Instantiate SFSafariViewController
# In a real macOS application, this controller would need to be
# initialized on the main thread and presented by an NSViewController.
# For this quickstart, we just demonstrate instantiation.
try:
controller = SFSafariViewController.alloc().initWithURL_entersReaderIfAvailable_(url, False)
print(f"Successfully created SFSafariViewController instance: {controller}")
# Example of accessing a property (if available and safe outside main thread)
# if hasattr(controller, 'URL'):
# print(f"Controller URL: {controller.URL()}")
except Exception as e:
print(f"Error instantiating SFSafariViewController (may require main thread/application context): {e}")
print("Note: SFSafariViewController typically requires a running Cocoa application and main thread for full functionality.")