PyObjC iTunesLibrary Framework

12.1 · active · verified Tue Apr 14

This library provides Python bindings for Apple's iTunesLibrary.framework on macOS, allowing Python applications to read and interact with the user's Music or iTunes library database. It's part of the larger PyObjC project, which wraps many macOS frameworks. The library is actively maintained, with frequent releases synchronised with macOS SDK updates and Python version compatibility changes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to load the user's iTunes/Music library and print some basic information about it. It uses the `ITLibrary` class to access media items and playlists. Note the requirement for macOS 'Full Disk Access' to prevent permission errors.

import os
from iTunesLibrary import ITLibrary

def get_itunes_library_info():
    """
    Retrieves and prints basic information from the user's Music library.
    Requires 'Full Disk Access' for the Python application in macOS System Settings.
    """
    try:
        # API Version 1 is generally current for basic access
        library = ITLibrary.libraryWithAPIVersion_error_(1, None)
        if library:
            print(f"Library Name: {library.applicationDisplayName()}")
            print(f"Total Tracks: {len(library.allMediaItems())}")
            print(f"Number of Playlists: {len(library.allPlaylists())}")

            # Example: Iterate through first 3 tracks
            print("\nFirst 3 Tracks:")
            for i, item in enumerate(library.allMediaItems()):
                if i >= 3:
                    break
                title = item.title()
                artist = item.artist().name() if item.artist() else "Unknown Artist"
                album = item.album().title() if item.album() else "Unknown Album"
                print(f"- Title: {title}, Artist: {artist}, Album: {album}")
                
                # Get file path if available
                if item.location():
                    file_path = os.fspath(item.location()) # Requires PyObjC 10.1+
                    # print(f"  Path: {file_path}") # Uncomment to see paths
        else:
            print("Failed to load iTunes Library.")

    except Exception as e:
        print(f"An error occurred: {e}")
        print("\nNote: Accessing the iTunes/Music Library typically requires 'Full Disk Access' for your Python application (or its parent process like Terminal/IDE) in macOS System Settings > Privacy & Security.")

if __name__ == "__main__":
    get_itunes_library_info()

view raw JSON →