Friday, November 02, 2007

Python has Apple Events support - from Apple

AppleScript: Scripting Bridge is an Apple product for sending Apple Events from Python.

I wonder how Python compares to AppleScript now as an OS X scripting language. I certainly prefer Python's syntax and scoping rules.

Also, Apple has at last updated their AppleScript documentation site. It was about 8-10 years out of date until recently. I'd assumed Apple was giving up on AppleScript, perhaps replacing it with an Apple version of Python. They've obviously decided to make another go of AppleScript, but it will be interesting to see how well Python and Scripting Bridge work.

Update: Be sure to check out the comments on the historic third party support for Python Apple Events and the associated links.

1 comment:

has said...

Python's actually had very good third-party Apple event support via appscript for several years now - see my sig for links. Appscript provides the same level of functionality as AppleScript and recent releases have just about the same degree of application compatibility, so it can certainly replace AppleScript for all your application scripting needs.

The one area where Python still trails AppleScript in its OSA language component support, which is needed for stuff like folder actions and Mail rules. To be honest though this isn't such a big deal as most automation tasks don't require OSA attachability anyway, and for those that do you can always find a developer release of PyOSA on the appscript website if you're feeling adventurous.

As for Scripting Bridge, I'm still in the process of figuring it out, although if you're hoping for a nice Pythonic syntax then you might be a little disappointed as its a Cocoa API. e.g.:

# AppleScript
tell application id "com.apple.textedit"
set s to paragraph 1 of document "ReadMe.txt"
end tell

# appscript
from appscript import *
s = app(id='com.apple.textedit').documents['ReadMe.txt'].paragraphs[1].get()

# Scripting Bridge (via PyObjC)
from ScriptingBridge import *
textedit = SBApplication.alloc().initWithBundleIdentifier_('com.apple.textedit')
s = textedit.documents().objectWithName_('ReadMe.txt').text().paragraphs()[0].get()

HTH

has
--
http://appscript.sourceforge.net
http://rb-appscript.rubyforge.org