PyObjC OSAKit Framework Wrappers

12.1 · active · verified Tue Apr 14

PyObjC is a bridge between Python and the Objective-C runtime, enabling Python scripts to interact with Apple's Cocoa frameworks. This specific package, `pyobjc-framework-osakit`, provides Python bindings for the OSAKit framework on macOS, allowing for the creation, compilation, and execution of AppleScripts and other scripting components. The current version is 12.1. Releases are frequent, often correlating with new macOS SDKs and Python version support changes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `pyobjc-framework-osakit` to programmatically compile and execute a simple AppleScript. It initializes an `OSAScript` object with source code, attempts to compile it, and if successful, executes it. Error handling follows the common PyObjC pattern using `NSDictionary` to capture Objective-C errors.

import OSAKit
import Foundation
import os

# Source for a simple AppleScript
script_source = """
display dialog "Hello from PyObjC OSAKit!" with title "PyObjC"
return "AppleScript executed successfully."
"""

try:
    # Create an OSAScript instance
    script = OSAKit.OSAScript.alloc().initWithSource_(script_source)

    # Prepare an error dictionary (Objective-C style error handling)
    error = Foundation.NSDictionary.dictionary()

    # Compile the script
    compiled = script.compileAndReturnError_(error)

    if not compiled:
        print(f"Error compiling script: {error}")
    else:
        print("Script compiled successfully.")
        # Execute the script
        result_desc, error_exec = script.executeAndReturnError_(error)
        if error_exec:
            print(f"Error executing script: {error_exec}")
        else:
            # OSADisplayResult is often an NSAppleEventDescriptor, use stringValue() to get Python string
            print(f"Script output: {result_desc.stringValue()}")

except Exception as e:
    print(f"An error occurred: {e}")

view raw JSON →