Getting Started with AppleScript

Getting Started with AppleScript

At a MacWorld conference, Sal Soghoian taps the keyboard, waves the mouse and boom an amazing workflow process takes place. Photographs are opened automagically from a folder, resized, edited and saved. QuarkXpress creates new pages and places the photographs just so into the layout. The file is saved and prepared for prepress. Zoom! Hours and hours of labor completed in a couple minutes all with AppleScript.

The crowd roars with applause. Then. After a pause. Each person whispers to themselves, “Ain‘t no way in hell I’m going to do that.”

AppleScript has the potential to automated extremely complex workflows. However, analyzing and automating such a workflow requires hundreds of hours of practice and a mastery of the language as well as the associated tools. Many automation tool go unused because the task of replacing the workflow with an automated solution either removes too much control from the user or process is so complex and changes with such regularity that it’s not worth the cost in time or labor.

What about breaking down a larger process into smaller parts so that the small, predictable parts can be automated? Or what about fixing small problems or adding small features to an application? In my experience, the small problem set is where AppleScript actually shines. For more complex systems, a piece of software off the shelf might be a cheaper solution in terms of money or labor or a more robust programming language like Perl might be better fitted to providing a sophisticated solution.

In an earlier post, I discuss how AppleScript can be used for creating an email template with Apple Mail. This kind of solution is where I apply AppleScript as much as possible. It’s a short, discrete problem that doesn’t require a user interface or processing a lot of text.

In Mac OS X 10.4, you can make your AppleScripts available in the menu bar where your clock resides by double clicking the AppleScript Utility in the AppleScript folder of your Applications folder. At the bottom of the utility, make sure the Show Script Menu in menu bar option is checked. As a side note, you can actually drop any file in this menu as you could in the old Apple Menu under Mac OS 9 and open it from the AppleScript menu.

When you first turn this option on, you’ll notice that all of Apple’s default scripts are in the menu. I typically move all of these scripts out of the /Library/Scripts/ folder into another folder for safe keeping like /Users/yourname/Documents/AppleScript Examples. When I want to know how Apple would create an AppleScript, I scan these examples for a quick solution I can modify for my own use.

Apple provides a basic text editor application for creating simple AppleScript applications called Script Editor. It lives in the Applications/AppleScript folder. Double click the application to launch it. You’ll notice a text area to enter your script with a bar separating out the area for results–whatever value is returned at the end of your script. You notice that there is a Record, Run, and Compile button. The Record button is not as helpful as it used to be under Mac OS 9. Under Mac OS X, it seems to have lost the ability to capture as many commands. Still, it’s helpful when you want to have AppleScript fill in text for you–such as selecting folders in the Finder. The Compile button will help you see if your application has any errors and the Run button will run your application. Often, using the Compile button is great for debugging the application without having to wait for the application to run its course.

In the text area for your script type the following

display dialog “Hello, World!”

It’s important to make sure that the quotes are in the right place. The text of our message is called a string. AppleScript expects that strings will start with a double quote and end with a double quote. You can think of it like dialog in a short story. Go ahead and click the Run button. You should get a dialog box with our message and two buttons: Cancel and Okay. Apple provides these buttons as default behavior for any dialog box for free. AppleScript even tries to automate common behaviors for you when create your scripts.

You’ve just completed your first AppleScript. Look here for more information on AppleScript and other scripting languages for automating common tasks. If you would like more information on AppleScript or for example scripts check out these sites:

http://www.macscripter.net/
http://developer.apple.com/applescript/

When you want to read a good book on the subject, you may want to pick up a copy of Adam Goldstein’s AppleScript: The Missing Manual or Matt Neuberg’s AppleScript: The Definitive Guide. The Missing Manual provides a great, easy to understand introduction to AppleScript. Unlike many books on AppleScript, Adam Goldstein shows you how to manipulate actual applications like the iLife suite in order to get the most out of automating common tasks. Matt Neuberg’s book talks about the structure of the language, it’s data types and behaviors. If you need a more in depth understanding of AppleScript pick up a copy of AppleScript: The Definitive Guide in addition to The Missing manual to broaden your knowledge.

When Joshua Wait isn’t teaching himself new programming languages or computer tricks, he’s hiking, swimming, cooking and playing outdoors with his family.