PyObjC AppleScriptKit

12.1 · active · verified Mon Apr 13

This library provides Python wrappers for the AppleScriptKit framework on macOS, allowing Python applications to embed, compile, and execute AppleScript code. It's part of the larger PyObjC project, which maintains a fairly active release cadence, typically releasing minor updates monthly/bi-monthly and major versions annually following macOS SDK updates. The current version is 12.1.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize and execute a simple AppleScript string using the `AppleScriptKit` framework. It shows the typical pattern of allocating an Objective-C object, calling an initializer, and handling potential errors.

import AppleScriptKit
import Foundation # For common Objective-C types like NSError

# Define a simple AppleScript
script_source = '''
display dialog "Hello from PyObjC AppleScriptKit!"
return "Script executed successfully."
'''

# Create an ASAppleScript instance
# Note: In PyObjC, None is used for 'nil' or 'NULL' in Objective-C
error_ptr = None # We'll pass None and check the returned tuple for errors
script = AppleScriptKit.ASAppleScript.alloc().initWithSource_error_(script_source, error_ptr)

if script:
    # Execute the script
    result, error = script.executeAndReturnError_(error_ptr)
    if error:
        print(f"Error executing script: {error.localizedDescription()}")
    elif result:
        print(f"Script result: {result.stringValue()}")
    else:
        print("Script executed, no result or error reported.")
else:
    print("Failed to initialize ASAppleScript object.")

view raw JSON →