PyObjC is a bridge between Python and Objective-C that allows full featured Cocoa applications to be written in pure Python.
—
Dynamic loading of macOS frameworks and libraries, bundle resource access, and framework initialization. These functions enable runtime access to system frameworks and custom libraries, allowing Python code to integrate with the full macOS ecosystem.
Functions for loading macOS frameworks at runtime.
def dyld_framework(path: str):
"""
Load a framework from the specified path.
Args:
path (str): Full path to the framework bundle
Returns:
The loaded framework bundle object
Usage:
framework = objc.dyld_framework("/System/Library/Frameworks/AppKit.framework")
"""
def pathForFramework(name: str):
"""
Find the path to a framework by name.
Args:
name (str): Name of the framework (without .framework extension)
Returns:
str: Full path to the framework, or None if not found
Usage:
path = objc.pathForFramework("AppKit")
# Returns: "/System/Library/Frameworks/AppKit.framework"
"""Functions for loading dynamic libraries and shared objects.
def dyld_library(path: str):
"""
Load a dynamic library from the specified path.
Args:
path (str): Full path to the dynamic library file
Returns:
The loaded library object
Usage:
lib = objc.dyld_library("/usr/lib/libsqlite3.dylib")
"""Context manager and functions for working with application bundles and their resources.
def inBundle(bundlePath: str):
"""
Context manager for loading resources from a bundle.
Args:
bundlePath (str): Path to the application bundle
Usage:
with objc.inBundle("/Applications/MyApp.app"):
# Access bundle resources
resource = loadBundleResource("config.plist")
"""
def current_bundle():
"""
Get the current application bundle.
Returns:
The current NSBundle object, or None if not in a bundle context
"""Functions for parsing BridgeSupport XML files and initializing framework wrappers.
def parseBridgeSupport(xmldata: bytes, globals: dict, frameworkName: str):
"""
Parse BridgeSupport XML data and populate globals.
Args:
xmldata (bytes): XML data from BridgeSupport file
globals (dict): Dictionary to populate with framework symbols
frameworkName (str): Name of the framework being loaded
BridgeSupport files contain metadata about C functions, structures,
and constants that cannot be automatically discovered by the bridge.
"""
def initFrameworkWrapper(name: str, frameworkPath: str, frameworkIdentifier: str, globals: dict):
"""
Initialize a framework wrapper with metadata.
Args:
name (str): Framework name
frameworkPath (str): Path to framework bundle
frameworkIdentifier (str): Bundle identifier
globals (dict): Dictionary to populate with framework symbols
"""Functions for loading variables and functions from loaded bundles.
def loadBundleVariables(bundle, globals: dict, variableInfo: dict):
"""
Load global variables from a bundle.
Args:
bundle: The loaded bundle object
globals (dict): Dictionary to populate with variables
variableInfo (dict): Metadata about variables to load
"""
def loadBundleFunctions(bundle, globals: dict, functionInfo: dict):
"""
Load C functions from a bundle.
Args:
bundle: The loaded bundle object
globals (dict): Dictionary to populate with functions
functionInfo (dict): Metadata about functions to load
"""import objc
# Find and load the WebKit framework
webkit_path = objc.pathForFramework("WebKit")
if webkit_path:
webkit_bundle = objc.dyld_framework(webkit_path)
# Now WebKit classes are available
from WebKit import WKWebView, WKWebViewConfiguration
config = WKWebViewConfiguration.alloc().init()
webview = WKWebView.alloc().initWithFrame_configuration_(
((0, 0), (800, 600)), config
)import objc
from Foundation import NSBundle, NSURL
# Get the main application bundle
main_bundle = NSBundle.mainBundle()
# Access bundle resources
with objc.inBundle(main_bundle.bundlePath()):
# Load a plist file from the bundle
plist_path = main_bundle.pathForResource_ofType_("Info", "plist")
if plist_path:
plist_url = NSURL.fileURLWithPath_(plist_path)
# Process the plist fileimport objc
import os
# Load a custom framework from your application
custom_framework_path = os.path.expanduser("~/Library/Frameworks/MyFramework.framework")
if os.path.exists(custom_framework_path):
# Load the framework
framework = objc.dyld_framework(custom_framework_path)
# Initialize wrapper with BridgeSupport if available
bridge_support_path = os.path.join(custom_framework_path, "Resources/BridgeSupport/MyFramework.bridgesupport")
if os.path.exists(bridge_support_path):
with open(bridge_support_path, 'rb') as f:
xml_data = f.read()
# Create globals dictionary for framework symbols
framework_globals = {}
objc.parseBridgeSupport(xml_data, framework_globals, "MyFramework")
# Now framework classes and functions are available
MyCustomClass = framework_globals.get('MyCustomClass')
if MyCustomClass:
instance = MyCustomClass.alloc().init()import objc
# Load a system library
sqlite_lib = objc.dyld_library("/usr/lib/libsqlite3.dylib")
# Load a custom library
custom_lib_path = "/usr/local/lib/libmycustomlib.dylib"
if os.path.exists(custom_lib_path):
custom_lib = objc.dyld_library(custom_lib_path)Install with Tessl CLI
npx tessl i tessl/pypi-pyobjc