PyObjC iTunesLibrary Framework
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
- breaking PyObjC has periodically dropped support for older Python versions to align with Python's end-of-life schedule and leverage newer Python features.
- gotcha Accessing the iTunes/Music Library.framework requires 'Full Disk Access' permission for the Python application (or its parent process like Terminal/IDE) in macOS System Settings.
- gotcha PyObjC 10.3 introduced breaking changes regarding the interaction between `__init__` and PyObjC's `__new__` for Objective-C wrapped classes. While partially reverted in 10.3.1 for user-defined `__new__`, direct `__init__` usage with PyObjC-provided `__new__` might still behave differently.
- gotcha PyObjC 11.1 changed how it models 'init' family methods (e.g., `initWithObjects_`) to align with Clang's Automatic Reference Counting (ARC) behavior, specifically regarding reference stealing for `self` and returning a new reference. This could affect memory management or object lifecycle in advanced scenarios.
Install
-
pip install pyobjc-framework-ituneslibrary
Imports
- ITLibrary
from iTunesLibrary import ITLibrary
Quickstart
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()