O'Reilly logo

AppleScript: The Missing Manual by Adam Goldstein

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Commanding Other Programs

Unless you plan to display dialog boxes in Script Editor all day, you're probably feeling pretty constrained by AppleScript right now. Yeah, you can display cool stuff in your dialog boxes, but that won't be enough to automate your Mac.

Luckily, you can use AppleScript to do other things—and one of the most powerful is the ability to control other applications. This feature alone unlocks a world of possibilities:

  • Automate your favorite word processor, so you can save all unsaved documents with a single click (Section 5.7.3).

  • Apply color-correction settings automatically using Adobe Photoshop (Section 7.2.2).

  • Convert song files in iTunes automatically while you recline in your lounge chair (Section 8.1.4).

Before you can write any of those complicated scripts, however, you must learn the basics of program control. This section, therefore, introduces the basics of how to send commands to specific programs, rather than just to Mac OS X itself.

Using tell Statements

When you want to target a program with your commands, you have to let AppleScript know which program to talk to. For such purposes, the tell statement is your best friend.

Say you want the Finder to open your Home folder, so you can copy some files onto your desktop. To do this, enter these commands in Script Editor, then click Run:

tell application "Finder"
    activate
    open home
end tell

Everything after the tell command is directed at the program you specify—in this case, the Finder. The activate command ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required