Controlling other Applications from MacPython

Python has a fairly complete implementation of the Open Scripting Architecure (OSA, also commonly referred to as AppleScript), allowing you to control scriptable applications from your Python program, and with a fairly pythonic interface. This piece of Python:


import Finder

f = Finder.Finder()
print f.get(f.window(1).name)

is identical to the following piece of AppleScript:


tell application "Finder"
	get name of window 1
end tell

To send AppleEvents to an application you must first create the Python modules interfacing to the terminology of the application (what Script Editor calls the "Dictionary"). Use the IDE menu command File->Generate OSA Suite... for this. For more control run

pythonw .../Lib/plat-mac/gensuitemodule.py --help

from a terminal window.

Creating a scriptable application in Python

You can also create a scriptable application in Python, but this is not very well documented. For Carbon applications you should look at the MiniAEFrame module.