PyObjC SafariServices Framework

12.1 · active · verified Tue Apr 14

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

Install

Imports

Quickstart

This example demonstrates how to import the SafariServices framework and instantiate a basic object like `SFSafariViewController`. In a full macOS application, UI-related objects like `SFSafariViewController` must be created and managed on the main thread within an `NSApplication` context.

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.")

view raw JSON →