
Please note that PyOSA is no longer under active development. This material is provided for historical/educational interest only.
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
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...
PyOSA requires Mac OS X 10.4 or later and py-appscript 0.19.x. Please note that PyOSA is no longer under active development, and some APIs and functionality may be broken, incomplete or not implemented.