PyObjC ScriptingBridge Framework

12.1 · active · verified Tue Apr 14

PyObjC provides Python bindings for macOS frameworks, allowing Python applications to interact with Objective-C APIs. `pyobjc-framework-scriptingbridge` specifically offers wrappers for the ScriptingBridge framework, enabling Python to control scriptable applications on macOS. The library is currently at version 12.1 and typically releases alongside major macOS SDK updates and Python version support changes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `ScriptingBridge` to interact with the Music app (or iTunes on older macOS) to check if it's running and retrieve the name of the currently playing track. It showcases `SBApplication` for referencing scriptable applications by their bundle identifier.

from ScriptingBridge import SBApplication
from AppKit import NSWorkspace # NSWorkspace can be used to check if an app is running

def get_itunes_track_name():
    # Get a reference to iTunes (or Music app on newer macOS)
    try:
        itunes = SBApplication.applicationWithBundleIdentifier_("com.apple.itunes")
    except Exception:
        # On macOS Catalina and later, iTunes was replaced by the Music app.
        # ScriptingBridge might still reference 'itunes', but bundle ID changed.
        itunes = SBApplication.applicationWithBundleIdentifier_("com.apple.Music")

    if itunes and itunes.isRunning():
        current_track = itunes.currentTrack()
        if current_track:
            return f"iTunes/Music is playing: {current_track.name()}"
        else:
            return "iTunes/Music is running, but no track is playing."
    else:
        return "iTunes/Music is not running."

if __name__ == "__main__":
    print(get_itunes_track_name())

view raw JSON →