PyObjC MediaLibrary Framework

12.1 · active · verified Tue Apr 14

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

Install

Imports

Quickstart

This quickstart demonstrates how to import and instantiate the `MLMediaLibrary` class to access macOS media sources. Due to restrictions of the underlying macOS framework, this code may encounter runtime errors (e.g., 'XPC connection interrupted') if not executed within a properly signed macOS application bundle or without appropriate entitlements.

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

view raw JSON →