As with most programming languages, AppleScript can be extremely complexfar too complex to fully cover in this book. On the following pages, I explain how you can get started using AppleScript. This introduction should be enough to help you decide whether you want to fully explore the world of AppleScript programming. Tip When you're ready for more how-to information for using AppleScript, I highly recommend Sal Soghioian's excellent book, AppleScript 1-2-3, published by Peachpit Press.
To launch Script Editor Open the Script Editor icon in the AppleScript folder in your Applications folder (Figure 19). An untitled Script Editor window appears (Figure 24). Figure 24. A new untitled Script Editor window. Or Open the icon for a script (Figure 20, left). The script appears in a Script Editor window (Figure 21). To write a script 1. | If necessary, choose File > New (Figure 25)or press to open an empty Script Editor window (Figure 24).
Figure 25. Script Editor's File menu.
| 2. | If desired, click the Description button at the bottom of the window and type a description for the script in the pane above it.
| 3. | Type the script steps in the top half of the window. Text appears in purple as you type. Be sure to press after each line. Figure 26 shows an example of another simple script.
Figure 26. A painfully simple script.
| To check the syntax for a script Click the Compile button in the script window (Figure 26). If your script's syntax is error-free, Script Editor formats and color-codes your statements (Figure 27). Figure 27. The script from Figure 26 after it has been compiled. Or If Script Editor finds a problem with your script, it displays a dialog that describes the problem (Figure 28) and indicates where it is in the script by selecting it (Figure 29). Click OK to dismiss the dialog and fix the problem. Figure 28. If the script contains an error, you'll see a dialog sheet like this one when you attempt to compile it. Figure 29. Script Editor helps you debug a script by highlighting problems. Tips The syntax checker uses AppleScript's compiler to translates the script into code that can be read and understood by your computer. (Compiled code does not appear on screen.) If the script cannot be compiled, a syntax error results. Unfortunately, even if you write a script without any syntax errors, the script is not guaranteed to work. The only way to make sure a script works is to run it.
To record a script 1. | If necessary, choose File > New Script (Figure 25) or press to open an empty Script Editor window (Figure 24).
| 2. | If desired, click the Description button at the bottom of the window and type a description for the script in the pane above it.
| 3. | Click the Record button.
| 4. | Perform the steps you want Script Editor to record. As you work, Script Editor writes AppleScript instructions in the Script Editor window (Figure 30).
Figure 30. Script Editor records the steps as you complete them.
| 5. | When you are finished recording steps, switch to the Script Editor window and click the Stop button. Script Editor writes the last instruction for the script (Figure 31).
Figure 31. When you click the Stop button, Script Editor writes the last step.
| Tips Unfortunately, Script Editor's recorder does not work with all applications. If you attempt to record a task and Script Editor does not write any instructions, the application you are using is not recordable. Before you record a script, it's a good idea to know exactly what you want to do. This will prevent errorswhich will also be recorded by Script Editor's recorder! Once you have a script recorded by Script Editor, you can edit it as necessary to customize it.
To save a script 1. | Choose File > Save (Figure 25) or press to display the Save Location dialog (Figure 32).
Figure 32. Script Editor's Save Location dialog.
| 2. | Enter a script name in the Save As box.
| 3. | Choose a file format from the File Format pop-up menu (Figure 33).
Figure 33. The File Format pop-up menu.
| 4. | Use the Where part of the dialog to select a location in which to save the file.
| 5. | Click Save.
The file is saved on disk. The script name appears in the title bar of the Script Editor window (Figure 34).
Figure 34. A script's name appears in the Script Editor window's title bar.
| Tips You cannot save a script if it will not compile. Check the script syntax before attempting to save the file; I explain how earlier in this section. If you're not sure what to choose in step 3, choose script. It's a good idea to save a script before trying to run it for the first time. Using the Save Location dialog is covered in Chapter 7.
To run a script Do one of the following: To run a compiled script from within Script Editor, click the Run button in the Script Editor window (Figure 31). To run an application from the Finder, double-click the icon for the applet (Figure 20, middle). If the script is valid, it performs all script commands. Or If the script is not valid, an error message appears (Figure 35). Click OK. Figure 35. When you run a script that contains an error, AppleScript displays an error message like this one. Tip To open an application's AppleScript dictionary 1. | Choose File > Open Dictionary (Figure 25) or press .
| 2. | In the Open Dictionary dialog that appears (Figure 36), select a dictionary and click Open. The dictionary opens in its own window (Figures 22 and 23).
Figure 36. Use the Open Dictionary dialog to open the AppleScript dictionary for a scriptable application.
| 3. | Click the name of a suite, command, or class to display its information in the right side of the window.
| To examine an example script 1. | Double-click the Example Scripts alias icon in the AppleScript folder (Figure 19) to open the Scripts folder (Figure 37).
Figure 37. AppleScript comes with dozens of sample scripts to perform a wide variety of tasks.
| 2. | Open the folder containing the script you want to examine.
| 3. | Double-click the example script file's icon to open it in Script Editor (Figures 38 and 39).
Figure 38. Some scripts can be complex, like this one, which accesses the Internet to get the current temperature at your location. (A thermometer outside your window would be simpler.) Figure 39. Other scripts can be very simple, like this one-liner to open the AppleScript Web site's home page.
| Tips You can modify and experiment with these example scripts as desired. If you make changes to an example script, I highly recommend that you use the Save As command to save the revised script with a different name or in a different location. Doing so will keep the original example intact, in case you want to consult it again. You can download additional sample scripts from Apple's AppleScript Web site, www.apple.com/applescript/.
To enable Script Menu 1. | Open the AppleScript Utility in the AppleScript folder (Figure 19).
| 2. | In the AppleScript Utility window that appears (Figure 40), turn on the Show Script Menu in menu bar check box. The Script Menu immediately appears in the menu bar (Figure 41).
Figure 40. AppleScript Utility's window. Figure 41. The Script menu icon appears near the right end of the menu bar.
| 3. | Choose AppleScript Utility > Quit AppleScript Utility, or press .
| Tip To run a script with Script Menu Choose the script you want to run from one of the submenus under the Script menu (Figure 42). Figure 42. Script Menu's submenus correspond to the folders in the Scripts folder shown in Figure 37. To remove a Script Menu script 1. | Double-click the Example Scripts alias icon in the AppleScript folder (Figure 19) to open the Scripts folder (Figure 37).
| 2. | Open the folder that contains the script you want to remove (Figure 43).
Figure 43. The contents of the folders inside the Scripts folder correspond to the items on the folder's submenu, as you can see by comparing this folder's contents to the submenu in Figure 42.
| 3. | Drag the script out of the folder.
| Tip To add a Script Menu script 1. | Write or record an AppleScript and save it as a script.
| 2. | Double-click the Example Scripts alias icon in the AppleScript folder (Figure 19) to open the Scripts folder (Figure 37).
| 3. | Drag the icon for the script you want to add into the folder corresponding to the submenu you want the script to appear on.
| |