What is PyOSA?

PyOSA is an OSA language component for the Python scripting language, allowing Python code to be executed by any OSA-enabled application (Script Editor, Mail, iTunes, etc.). PyOSA makes Python a full peer to AppleScript.

For example, the following PyOSA script will return the string "Hello world!" when run in Script Editor:

def run():
    return "Hello world!"

This is equivalent to the AppleScript:

on run
    return "Hello world!"
end run

How does PyOSA relate to py-appscript?

Py-appscript is an Apple event bridge, allowing Python scripts to send Apple events to applications. That's great when you want to control one process from another, but no help if you need a script that can call and/or be called by by its host process ("attachability"). For that level of integration/interaction, you need a full OSA language.

PyOSA packages the Python interpreter as an OSA language component, allowing Python scripts to be attached to any OSA-aware application. It also integrates fully with py-appscript, allowing PyOSA scripts to send Apple events directly to the host process and vice-versa. This is essential for tasks like Folder Actions and Mail rules, which can't be done with py-appscript alone.

For example, you could create a Mail rule that performs a Run AppleScript action whenever new messages are received. That rule would load a PyOSA script and call its perform_mail_action_with_messages function, passing it a list of message references. The script could then use py-appscript to process those messages however it likes, getting and setting their properties, moving them to other mailboxes, and so on:

def perform_mail_action_with_messages(messages):
    for msg in messages:
        # process each message reference here...

Notes

PyOSA requires Mac OS X 10.4 or later and py-appscript 0.18.1 or later. Please note that PyOSA is currently under active development, with some APIs and functionality not yet implemented and others likely to change later on as the design is refined.

Please send any questions, criticisms, suggestions for improvements, etc. to the appscript maintainer. Developers interested in assisting with PyOSA or developing similar components for other languages can contact the project maintainer for more information.