Home > Articles

  • Print
  • + Share This
This chapter is from the book

Basics

This section presents three scripts that provide a good jumping-off point for learning about AppleScript. The first one opens AppleScript Help, the second takes you to the AppleScript Web site, and the third opens Script Editor.

It's worth taking a look at these scripts, because they can serve as templates for a variety of your own scripts.

Scripting Help Viewer

Help Viewer is the application that displays help for the operating system and applications in Mac OS X. You've probably seen its window, which is shown in Figure 3.1.

Figure 3.1Figure 3.1 The Help Viewer displays Mac Help.

You can type a word or phrase into the field at the upper right to search for articles relevant to that topic. As you can see in Figure 3.2, a different listing displays the results of that search. In the right-hand column, you can see that Help Viewer has retrieved information from a variety of sources.

Figure 3.2Figure 3.2 Help Viewer lets you search for help in many locations.

Here's the script for AppleScript Help:

tell application "Help Viewer"
 activate
 search looking for "AppleScript"
end tell

Although you'll find out more about the full syntax shown here, it's not hard to figure out that what this script does is just what would happen if you typed "AppleScript" into the search field at the upper right of the window.

Want to create an AppleScript to provide information about printers? Change one word so that the third line reads:

search looking for "printers"

Why would you want to do this? If you're setting up an environment for others to use, you may know how to use Help Viewer, but not everyone else does. Of course you can teach people how to use it, but it might be easier to create several scripts with names such as Printer Help, Network Help, and the like. Then, people need only double-click those scripts (or choose them from Script Menu if they're installed there).

Using Scripts to Navigate the Web

The second script takes you to the AppleScript Web site, and it is only one line long:

open location "http://www.apple.com/applescript/"

As you can guess, this is exactly the same code that you use to open any Web site in the default browser. If your company has its own online help, you can easily add to Printer Help and Network Help a third script: Our Company Help. These basic scripts require only a double-click to give help to users—albeit help from different types of resources.

Launching an Application with a Script

The third script launches Script Editor; its syntax is the same for any application that you want to launch:

tell application "Script Editor" to activate

If the application is not running, it is launched. If it is running (either beforehand or as a result of being launched), it is activated: brought forward so that it is the frontmost application. When you interact with an application using the mouse and keyboard, it is activated. With AppleScript, you can send commands to an application that is running but not the frontmost process (the equivalent of typing in a window that is behind another window—something you cannot do interactively).

  • + Share This
  • 🔖 Save To Your Account