distraction in action

Like my work? Check out HexaLex, my game for iPhone & iPod Touch. It's a crossword game like Scrabble, but played with hexagonal tiles. http://www.hexalex.com

AppleScript is remarkable for being perhaps the worst programming language in modern history that has managed nonetheless to reach a wide audience. This unlikely success can be attributed to the fact that application scripting is fantastically useful and, when it comes to application scripting on the Mac platform, AppleScript has always been pretty much the only game in town.

All that is on the brink of changing, however, thanks to Hamish Sanderson's appscript project. Appscript allows you to do OS X application scripting with Python in a fairly natural, object-oriented fashion. For example, to get a list of the file locations for all photos in the current iPhoto album you can write this Python code:

files = app("iPhoto.app").current_album.photos.image_path.get()

To find out what properties an album has you can write:


So why is this important? To be sure, it has been possible, if awkward, to do app scripting in Python for quite a while now using gensuitemodule and friends. The problem is that this process was incredibly awkward and error-prone, and it required a fairly deep understanding of how AppleScript works internally. It's also true that there have been other scripting languages besides AppleScript for Mac OS, but they have all been languages with little or no application outside of that niche. Many of them were commercial, which badly impeded widespread acceptance. With appscript, we Mac folks get the ability to script our apps in a clean, general-purpose, open-source language without the need to learn too many details about Apple Events and such.

Another reason this matters is that OS X is increasingly attracting Linux users who have become tired of recompiling kernels and having flaky hardware support. These people have lots of experience scripting in various languages like Bash and Python. They're not likely to pick up AppleScript since it's such a crappy language and has no application besides OS X app scripting. If they can accomplish the same things in Python, however, then the floodgate opens for all sorts of cool hacks and scripts.

As a demonstration of what I'm talking about, I offer iphoto2album, a script that takes the current iPhoto album and adds it to a photo album made by Album. It requires working installations of iPhoto, appscript, and album, of course. You'll probably need to tweak a couple of parameters at the top of the scripts, but there's nothing too tricky. Granted, this is probably too tricky for the average Mac user, but it's a start.

Speaking of Linux to OS X converts, I was pinged recently by Titus Brown, who was one of the first folks I know to make the jump. He recently established a blog and appears to be updating it frequently, unlike myself. I'm lucky to push out one post a month — he seems to be doing them daily. If his first few posts are anything to go by, the blog should be titled “How to Win Friends and Influence People in the Bioinformatics Software Industry.” BTW, Titus is one of the few people I know who actually writes code as often as he talks about writing code, so he truly has a leg to stand on when he tells you your software sucks ass. :-) Thankfully, my code is generally too short to suck ass. It would have to suck ankle or kneecap or something.

  • Share/Bookmark


1. garbanzito replies:

i’m piping up because this page comes up when searching for “appscript”, which i think people may be doing as they hunt for context

i agree with your assessment of appscript — i’m very enthusiastic about it — but i’d like to offer a couple of counterpoints to the larger picture you paint; this is not to take away from appscript, but to add some appreciation for where it stands and how it can develop

i’m not sure what “niche” you refer to above, so i think it’s worth mentioning Frontier, which predated AppleScript, has a broader scope than AppleScript (all the interapp capabilities, much more built-in functionality, and later a complete TCP/IP suite & server); Frontier has had two periods, including the present, in which it was free (the core is now open-source at frontierkernel.sourceforge.net); i like Frontier’s no-nonsense syntax, object database and outliner editor; Python/appscript is the logical next step for Mac Frontier programmers, and while Python is of course much more sophisticated, i think it could still benefit from Frontier’s outliner IDE concept and approach to persistence

also while i agree that AppleScript as a language has major usability flaws, i sputtered a bit at “perhaps the worst” — it has a unique, dynamic and expressive (if subtle, contorted and ambiguous) OO design that is much more sophisticated than so called “OO” VB, for instance; one might also question how AppleScript might have developed if it really had ever reached a “wide audience”; AppleScript is only now reaching belated maturity, hunchback wizard that it is; still, Python + appscript will need more integration and support it can supplant AppleScript for sheer convenience

finally, as you seem to acknowledge, anyone who might make headway with appscript in its current state will really need some respect for and comfort with AppleScript; for appscript to develop into the core Mac OS X interapp tool it could become, i think a synergy will be needed, such as direct ways to address Python/appscript objects from AppleScript

2. Dave Bayer replies:

I didn’t sputter at all at at “perhaps the worst”. My favorite AppleScript book author confessed his ambivalence for the language right there in the book he was trying to sell me, and that is why I bought the book. AppleScript has caused me to curse my screen more times than any of a dozen other languages I have used in recent years, yet scripting beats using the GUI for many Apple apps that I use.

It was pure genius for Apple to adopt NextStep and Unix, and it probably saved the company. I’m all for language experimentation, but it would have been more much-needed genius had Apple instead recognized the emerging importance of Python, and bet the bank on something like appscript for Python, instead of growing their own language.

Perl predates Python, and was highly original in its time. Yet, as others have observed, its author is a linguist, Python’s author is a mathematician, and it shows. That AppleScript was designed to be a more “human” language for “the rest of us” (how many people have switched to Linux out of contempt for that phrase?) is its fatal flaw. Like Perl, it lacks a mathematical predictability for its syntax and semantics.

If I had to cite three programming languages that one should understand, master and maintain fluency in, I’d list C/C++/Objective C (to understand how a computer most efficiently works), Haskell (to understand how the human mind most efficiently works) and Python (to get things done, if you can afford to have your code screw up at runtime). One writes better Python code, coming off a stint of Haskell programming than coming off a stint of C programming. (Yes, I’ve coded in Scheme, Ruby, OCaml, … and I stand by this list.)

From this point of view, appscript is a crucial development, because it works with a language everyone should know, not wasting their time with a different language that isn’t worth their trouble.

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Please type this word with the letters reversed: live

Like my work? Check out HexaLex, my game for iPhone & iPod Touch. It's a crossword game like Scrabble, but played with hexagonal tiles. http://www.hexalex.com