Mimsy Were the Borogoves

Hacks: Articles about programming in Python, Perl, PHP, and whatever else I happen to feel like hacking at.

AppleScript: The Definitive Guide

Jerry Stratton, August 13, 2005

On his web page, author Matt Neuburg writes that “this is the first really good book on AppleScript--the book I wish I’d had years ago.”

It is definitely better than the other O’Reilly AppleScript book I‘ve got. Despite being filled with text about something that looks like AppleScript, whenever I’ve tried to find an answer in AppleScript in a Nutshell I haven’t been able to find one. This Definitive Guide is much better, and much more useful.

My life with AppleScript

AppleScript was the first scripting language I ever used. Before AppleScript, my programming was with C, PL/I, or batch languages on various computer systems. Because AppleScript was my first taste of scripting, I used it for far too much. When it slowed down on some task, I’d pull out the Metrowerks compiler and write an osax in C.

The epitome of this was when I created a web log analysis script to analyze my nascent web site’s log files in 1995. Analyzing these small log files--the site ran over a 14.4 modem--took about twenty minutes at first, but as the hits increased, the analysis time grew to an hour, a couple of hours, up to about ten hours to complete the daily log analysis.

It was obviously about to reach the point where a day’s analysis was going to take longer than a day to complete. I was about to see which routines could be sped up with a C-written osax, when I realized this would be a good chance to look at this new scripting language called “perl” that a friend of mine had been using. I worked up a short perl script to compare speed, and could not get the thing to work. I’d start up the perl script, and it would immediately quit.

I was about to give up when I noticed that the log analysis web page was dated a few seconds past, and it had stuff in it; sure enough, I opened it in iCab and discovered that what took several hours in AppleScript went by so fast in perl that I thought it wasn’t even working.

Elegance is in the moment

This anecdote, while completely true, is unfair to modern AppleScript for several reasons. The two most obvious are that, first, AppleScript at that time was emulated 68k code on a PowerPC, whereas the perl I was using was native. Nowadays, AppleScript is a lot faster. Since moving to Mac OS X it has gone native. I haven’t even bothered to re-compile the FB Osax that I used to use in OS-9, because it hasn’t been needed.

Second, I was using AppleScript for something it wasn’t designed for, but that perl was designed for: chomping through lines of text.

As you can see from the previous two hacking articles, I’ve come back around to using AppleScript; this time I’m using it for what it was designed for, and for which it is an elegant solution: leveraging the applications I use daily to perform repetitive tasks that those applications are designed to perform.

AppleScript at its simplest can look very user-friendly. If you want to tell Firefox to get a web page, you can

  • tell application "Firefox" to Get URL "http://www.hoboes.com/Mimsy/"

If you want to tell GraphicConverter to trim an image, you can

  • tell application "GraphicConverter" to trim window 1

A well-written application will let you do, with AppleScript, just about anything you can do by hand. This makes it very easy to automate repetitive tasks. For example, if you often receive photographs that need to be rotated 90 degrees and autoleveled, you can make a script that will do this: