Mac OS X comes with three different ways to automate tasks: AppleScript, Folder Actions, and the Automator. Of the three, AppleScript is by far the most venerable, having first made its appearance in 1993 as part of Mac OS 7.1 Pro. Essentially a programming language of sorts that uses code that is comparatively easy to read and understand, the idea behind AppleScript was that it would provide users with a way to automate common tasks. AppleScript was substantially enhanced in OS X, being easier to use and more effective at controlling a wider variety of applications. With the arrival of OS X 10.3, it became possible to attach certain AppleScript scripts, known as Folder Actions, to folders via the Finder. These scripts would run whenever the folder was opened or changed in some way; for example, converting image files placed into that folder from one format to another.
While AppleScript is potentially quite powerful, it isn’t particularly easy to use, certainly not when compared with the Macintosh operating system in general. To fix this problem, OS X "Tiger" includes another automating tool, appropriately enough called Automator, which attempts to make things simpler. Automator uses a "building block" approach that doesn’t require any knowledge of programming or scripting from the user. Commands for various applications are dragged onto a document window where they can be configured using menus and buttons as required. The resulting workflows can then be saved as standalone applications. The chief drawback to Automator is the range of applications it works with; whereas AppleScript can (in theory at least) work with any Cocoa program, Automator is largely confined to Apple applications plus a few third-party programs that have had Automator modules written specifically for them. Still, because Automator is so easy to use, it can be very useful—as you’ll see.
Before going any further, it’s worth mentioning that the idea of this part of the article is to show how AppleScript works and why it’s useful. To actually learn AppleScript programming requires a bit more effort. The built-in help files that come with AppleScript will certainly point you in the right direction, but to really come to grips with the topic you’ll need a book like AppleScript 1-2-3 by Sal Soghoian and Bill Cheeseman.
But for now, let’s begin by launching the Script Editor application that you’ll find in the AppleScript folder inside the Applications folder. Choose New from the File menu (or Command+N) to create a new AppleScript document.
At its simplest, an AppleScript typically calls on a certain application, tells it to perform one or more tasks, and then releases that application when the script ends. The following example causes the application Terminal to ask for Administrator-level permission ("sudo") before running a trio of maintenance programs ("daily", "weekly", and "monthly"). In theory, your Mac would run these programs automatically at night at daily, weekly, or monthly intervals, having been designed for use on UNIX machines running 24/7. But unless you deliberately leave your Mac running all night, they’ve probably never been run. What they do is clean out old scratch files and log files and then update certain system databases. The daily and monthly tasks tend to be done rather quickly, but the weekly maintenance task takes several minutes. They help keep OS X running nice and smoothly.
tell application "Terminal" do script with command "sudo periodic daily weekly monthly" end tell
Let’s save this script as a standalone application. First choose Save from the File menu (or Command+S). By default, scripts are saved as they are, but to create a standalone application, choose "application" from the pull-down menu in the save screen. Don’t worry—you can edit standalone applications in Script Editor, so you can always edit the script inside the application later on. Once the application has been built, it can be placed anywhere you want for easy access. You could put it in the Dock, for example, so that you can quickly launch it every few weeks. Admittedly, this little program isn’t quite Norton Utilities, but even so you’ve just created your first application, and a darned useful one at that!
Figure 1 AppleScript documents can be saved either as scripts or as standalone applications.