Hack 64 Running AppleScripts on a Regular Basis Automatically


Automating tasks with AppleScript has always been a powerful feature of the Macintosh operating system; but, until recently, automating tasks repetitively required new software. With OS X, repetitious automation is built in.

AppleScript (http://www.apple.com/applescript/) is a wonderful piece of technology that has long been part of the Macintosh OS. Just about any application can become AppleScript-powered, allowing automated tasks to be written in a simple, English-like language. Because of this power and the ease with which it can be had, kazillions of ApplesScripts have been written, ranging from automating backups of emails, archiving data from network-enabled services, and Finder-based scripts that promote organization (make all these files lowercase), to plug-ins (type itunes to place the currently playing song on the clipboard).

What has been missing, however, is automation of the automation, repetitively running an AppleScript without user intervention. Take the following simple script, for example:

say the (current date) as string

Exemplar of AppleScript bluntness, this uses text to speech to enunciate the current day and time. Type it into Script Editor (in Applications AppleScript), click Run, and listen closely.

That's all well and good, but what if you wanted the time spoken each and every hour, on the hour? Under Mac OS 9 and earlier, you would have had to make use of a third-party scheduling application ? or just clicked Run every hour on the hour.

Meet cron [Hack #53], a Unix shell program whose life revolves around running things every minute, hour, day, week, month, or year. Give it a command or script and a schedule and let it go. Of course, cron is available as part of the Unix goodness existing beneath the colorful OS X shell. Each user on the system can automate his own tasks with no restrictions: hear the date spoken every minute, have a backup performed every three days at 12:15, or automatically open his email every day at 7:00 a.m. and then again at 6:00 p.m. Whatever your scheduling needs, cron will satisfy them.

But cron is a Unix utility, and AppleScript traditionally lives above the GUI; how do we connect the two? A shell utility called osascript, which runs AppleScripts from the command line, can take our simple script and run right from the command line:

% osascript -e 'say the (current date) as string'

And, if you can run it from the command line, you can run it out of cron, as these examples show:

0 7 * * Mon-Fri osascript -e 'say "alright, time to go to work"'
0 18 * * Mon-Fri osascript -e 'say "whooo dawggy, time to go home!"'
0 12 * * Sat-Sun osascript -e 'say "maybe I should get up sometime soon"'

These three entries will speak veiled threats every weekday morning at 7:00 a.m., wonderful news at 6:00 p.m. the same day, and a questionable alarm clock at noon on the weekends. These are all parlor tricks, however; what you really want to do is run a complex operation with hundreds of lines of AppleScript, and you certainly don't want to include it all written out as part of a command line.

In that case, simply leave the -e option off osascript, and point to your AppleScript file:

osascript /Users/morbus/Scripts/Backup_Email.scpt

Merging the preceding osascript into your crontab will have your email backed up the 15th of every month at 9:32 a.m. ? assuming Backup_Email.scpt exists, of course:

32 9 15 * * osascript /Users/morbus/Scripts/Backup_Email.scpt