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

Working with the Scripts You Have

As you'll quickly realize from using the Script Menu, your Mac is teeming with dozens of free, built-in scripts. You can run, rename, and organize these scripts, as described on the preceding pages.

But if you're just starting to learn AppleScript, these scripts can be lifesavers. Not only can you examine how the scripts work, but you can also make changes and see how they affect the scripts' behavior. Best of all, if you get stuck when writing a new AppleScript, you can copy some of the code from the Script Menu's scripts and paste that code right into your own script.

Opening a Script

The first step in working with a script, of course, is opening it up. Fortunately, this is an easy process: just double-click the script in the Finder. The script opens in Script Editor, the all-purpose AppleScript program described in Chapter 2.

When you're just learning AppleScript, you might as well start by looking at a simple script. Double-click Library Scripts Navigation Scripts New Application Window.scpt, and Script Editor opens the script file in a new window (Figure 1-9).

If you've never seen an AppleScript before, you may be surprised at how simple the code looks. As you can probably guess from the commands in the window, this script simply opens the Applications folder in the Finder.

Figure 1-10. If you've never seen an AppleScript before, you may be surprised at how simple the code looks. As you can probably guess from the commands in the window, this script simply opens the Applications folder in the Finder.

At this point, you can click the Run button to see what the script actually does (in this case, the script opens a new Finder window and takes you to the Applications folder).

Analyzing How It Works

The next step in working with a script, of course, is understanding how the script actually works. Some commands are self-evident, while others require you to examine a program's dictionary—its master command list (Section 3.2.2)—to understand exactly what's going on. Luckily, the commands in the New Applications Window script are all pretty simple:

  • tell application "Finder" instructs Mac OS X that the commands that follow should be run by the Finder.

  • activate brings the Finder to the front, much as you would by clicking its Dock icon. Unlike clicking the Dock icon, however, the activate command doesn't automatically open a new Finder window; for that stunt, you'll need the next command.

  • open folder "Applications" of the startup disk tells the Finder to open a new window, displaying the Applications folder of your main hard drive. (If you already have the Applications folder open, this command simply brings that window forward.)

  • end tell directs the Finder to go about its regular business, ignoring further AppleScript commands.

Changing What It Does

Now that you understand how the script works, you can change it to better suit your needs. To make the script open the Users folder, for example, you'd follow this procedure:

  1. Replace Applications with Users.

    File names have to be surrounded in double quotes, so make sure you don't delete the quotation marks.

  2. Click the Run button to test your script.

    You should see the Finder come forward and display the Users folder.

  3. Choose File Save As (Shift--S).

    Name the file New Users Window.scpt, make sure the File Format is set to Script, and save it in the Library Scripts Navigation Scripts folder.

  4. Open your Script Menu, and run your new script from the Navigation Scripts submenu.

    If you did everything correctly, your new menu item opens the Users folder in the Finder (Figure 1-10).

When you run your newly edited script (top left), you see your Users folder (bottom right). The house icon represents the account that's currently logged in.

Figure 1-11. When you run your newly edited script (top left), you see your Users folder (bottom right). The house icon represents the account that's currently logged in.

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