PyObjC MediaLibrary Framework
pyobjc-framework-medialibrary provides Python wrappers for the macOS MediaLibrary framework, which allows access to media content managed by applications like Photos, Music, and TV. It is part of the larger PyObjC project, a bridge that enables Python scripts to interact with Objective-C class libraries, most notably Apple's Cocoa frameworks. The current version is 12.1, and the project maintains an active release cadence, frequently updating bindings for new macOS SDKs.
Warnings
- breaking PyObjC 12.0 dropped support for Python 3.9. Projects using older Python versions must upgrade to Python 3.10 or later to use `pyobjc-framework-medialibrary` 12.x. PyObjC 11.0 dropped Python 3.8 support.
- breaking PyObjC 11.1 introduced significant changes to how initializer methods (methods in the 'init' family) handle Automatic Reference Counting (ARC), aligning with `clang`'s documentation. This means such methods now correctly 'steal' a reference to `self` and return a new one, which might alter reference counting behavior in complex Objective-C/Python interactions.
- gotcha Using `MLMediaLibrary` (and many other macOS frameworks) from a plain Python script executed from the command line can lead to `XPC connection interrupted` errors. These frameworks often expect to be run within a full macOS application bundle with specific entitlements.
- gotcha PyObjC 10.3 initially removed the ability to call `__init__` on Objective-C classes when a Python class (or its superclass) defines `__new__`. While 10.3.1 partially reintroduced this for user-implemented `__new__`, it remains a nuanced interaction where standard Python object initialization patterns might clash with PyObjC's object lifecycle.
- gotcha PyObjC 11.0 introduced *experimental* support for free-threading (PEP 703) in Python 3.13. While this is an exciting development, experimental features may have stability issues or unexpected behavior. Note that earlier versions (e.g., 10.3) explicitly stated lack of support for Python 3.13's free threading.
Install
-
pip install pyobjc-framework-medialibrary -
pip install pyobjc
Imports
- MLMediaLibrary
from MediaLibrary import MLMediaLibrary
Quickstart
import MediaLibrary
# Create an instance of MLMediaLibrary
# WARNING: MLMediaLibrary often requires running within a macOS application bundle
# or with specific entitlements to avoid 'XPC connection interrupted' errors
# in command-line contexts. This example may not function fully outside an app.
try:
media_library = MediaLibrary.MLMediaLibrary.alloc().initWithOptions_(None)
if media_library:
print(f"Successfully instantiated MLMediaLibrary: {media_library}")
# Attempt to get media sources - this might fail if not in an app context
media_sources = media_library.mediaSources()
if media_sources:
print(f"Found {len(media_sources)} media sources.")
for source in media_sources:
# Accessing properties like mediaSourceIdentifier() might require an active app context.
try:
identifier = source.mediaSourceIdentifier()
print(f"- Source Identifier: {identifier}")
except Exception as prop_e:
print(f"- Could not get source identifier for a media source: {prop_e}")
else:
print("No media sources found or could not access them.")
else:
print("Failed to instantiate MLMediaLibrary (returned None).")
except Exception as e:
print(f"An error occurred during MLMediaLibrary instantiation or access: {e}")
print("This often indicates issues with entitlements or running outside an app bundle.")