AddThis Social Bookmark Button


AppleScript Primer for Mac OS X

by Bruce W. Perry, author of AppleScript in a Nutshell

AppleScript is a built-in Macintosh automation tool that gives users the ability to control the operating system and several of their favorite applications. While this powerful scripting system has always had a loyal following of Macintosh aficionados and publishing professionals, the release of Mac OS X 10.1.2 may mean AppleScript is ready to strut its stuff in front of a wider audience. Here are some of the exciting AppleScript developments on Mac OS X:

  • AppleScript Studio, a development tool for creating an AppleScript application with the enhanced Aqua graphical interface.
  • The calling of Unix shell commands with the "do shell script" AppleScript command and the scripting of Mac OS X's Terminal application.
  • Web services, which allow focused information like stock quotes or weather data to be transferred between applications on the Web using a universal format called Extensible Markup Language (XML).

In this article, I'll begin by showing you where to find the applications in which you develop your AppleScripts. Then, to get you going, I'll give an example of a simple AppleScript. Since you are interested in not just the simple but the sublime, I'll also give you descriptions and code samples relating to AppleScript Studio, Web services, and Unix-shell integration.

Related Reading

AppleScript in a NutshellAppleScript in a Nutshell
By Bruce W. Perry
Table of Contents
Sample Chapter
Full Description

For those who are eager to dive into scripting their favorite Mac OS X and third-party applications, such as Adobe Illustrator 10 or FileMaker Pro, refer to the sidebar "Scriptable Mac Applications" for a list of applications that can be used with AppleScript.

For Starters

You can find AppleScript by opening a new Finder window, clicking on the "Applications" icon, then opening the AppleScript folder. There you will find the Script Editor application. (You can also type in Option:Command:A to get to the Applications folder.) Figure 1 shows a Script Editor window and a Finder window in column view, with the AppleScript folder selected. Script Editor is similar to a text editor where you can type in, check the syntax of, and compile AppleScripts.

Applescripting Window

Here's a simple scripting example. I use a printer that is connected to a Windows machine in another part of my office. While I have not been able to add documents directly to this machine's print queue, I can keep the other machine's disk mounted on my laptop's desktop and move the files that I want to print into a "print" folder on that distant machine (from where they are printed). Here is the AppleScript that does it:

Scriptable Mac Applications

Here is a list of some of the Mac OS X applications can be controlled by AppleScripts:

Apple System Profiler
Image Capture
iTunes (See the "Scripting iTunes 2" sidebar)
Print Center
QuickTime Player
Interface Builder
Project Builder

Here’s a short list of scriptable third-party applications:

Adobe Illustrator
Adobe Photoshop
Eudora Email
FileMaker Pro
Internet Explorer
Microsoft Word
Netscape Navigator
Stone Studio

tell application "Finder"

   move selection to folder "print" of disk "M"

end tell

This script targets the Finder application with its script messages, which are technically referred to on the Macintosh as Apple events. The "tell" block contains the statements that its specified application understands; you probably guessed that to target other applications with AppleScript you use syntax such as " tell application "iTunes" " or " tell application "Adobe Photoshop®5.5"".

The "activate" command makes the targeted application the active layer on the desktop (so the program’s menu is displayed on the top of your screen). The file that is selected in the Finder is then moved to a folder called "print" on a disk named "M" (on another machine in this case). To keep things simple we have not checked if the selected item is a file or not (we may not want to move any folders), but this example should suggest how easy it is to get started with concise and useful scripts. A few applications (such as BBEdit 6.5) will generate the AppleScript for you via Script Editor's "Record" feature. Of course, you can do much more with AppleScript.

AppleScript Studio

The marquee feature of Mac OS X 10.1.2 for script mavens is AppleScript Studio. This tool allows you to develop great-looking Aqua interfaces for an AppleScript application. For example, Figure 2 shows the window for an AppleScript Studio application that asks the user for a stock symbol, then goes and grabs the stock value using Web services and the chosen currency for the display format.

AppleScript Studio has been incorporated into an existing integrated development environment (IDE) consisting of Project Builder and Interface Builder, the same tools that are used to create Cocoa applications in the Java or Objective-C languages. AppleScript Studio is included with the December 2001 Mac OS X Developer Tools (see ), which are a free download for Apple Developer Connection members. An ADC online membership is free, so it's a great deal to be able to develop your own Mac OS X software with this feature-rich and free-of-charge IDE.

Applescript Studio Window

AppleScript Studio is quite a step up from Script Editor, in both functionality and learning curve. In an AppleScript Studio application, you can optionally add new Java and/or Objective-C classes to your project in Project Builder, then call the methods of these objects directly from AppleScript. Creating sophisticated interfaces in Interface Builder is as easy as dragging and dropping Cocoa interface objects (such as text fields, buttons, and progress indicators) from a palette onto an AppleScript Studio application window. Figure 3 shows a Studio application window and palettes in Interface Builder.

Studio app and palettes

AppleScript Studio looks like a promising development environment for automation suites that require user interaction.

Pages: 1, 2

Next Pagearrow