- Table of Contents
- Copyright
- About the Author
- Acknowledgments
- Tell Us What You Think!
- Introduction
- Part I: Introduction to Mac OS X
- Chapter 1. Mac OS X Component Architecture
- Chapter 2. Installing Mac OS X
- Chapter 3. Mac OS X Basics
- Chapter 4. The Finder: Working with Files and Applications
- Chapter 5. Running Classic Mac OS Applications
- Part II: Inside Mac OS X
- Chapter 6. Native Utilities and Applications
- Chapter 7. Internet Communications
- Chapter 8. Installing Third-Party Applications
- Part III: User-Level OS X Configuration
- Chapter 9. Network Setup
- Chapter 10. Printer and Font Management
- Chapter 11. Additional System Components
- Part IV: Introduction to BSD Applications
- Chapter 12. Introducing the BSD Subsystem
- Chapter 13. Common Unix Shell Commands: File Operations
- Part V: Advanced Command-Line Concepts
- Chapter 14. Advanced Shell Concepts and Commands
- Chapter 15. Command-Line Applications and Application Suites
- Chapter 16. Command-Line Software Installation
- Chapter 17. Troubleshooting Software Installs, and Compiling and Debugging Manually
- Chapter 18. Advanced Unix Shell Use: Configuration and Programming (Shell Scripting)
- Part VI: Server/Network Administration
- Chapter 19. X Window System Applications
- Chapter 20. Command-Line Configuration and Administration
- Chapter 21. AppleScript
- Chapter 22. Perl Scripting and SQL Connectivity
- Chapter 23. File and Resource Sharing with NetInfo
- Chapter 24. User Management and Machine Clustering
- Chapter 25. FTP Serving
- Chapter 26. Remote Access and Administration
- Chapter 27. Web Serving
- Part VII: Server Health
- Chapter 28. Web Programming
- Chapter 29. Creating a Mail Server
- Chapter 30. Accessing and Serving a Windows Network
- Chapter 31. Server Security and Advanced Network Configuration
- Chapter 32. System Maintenance
- Appendix A. Command-Line Reference
- Appendix B. Administration Reference
Script Editor
The easiest way to get started with AppleScript is with the AppleScript editor. Besides being a context-sensitive programming editor, it also acts as a script recorder. A user can open the Script Editor, click record, and generate an AppleScript by using the editor to monitor his actions while interacting with an AppleScriptable application.
Apple has made remarkable strides in making Mac OS X 10.1 scriptable. Applications such as the Finder and Terminal are now fully scriptable. The Script Editor will serve as your primary entry and testing point for any AppleScript development—either recorded or entered by hand.
Basic Usage
Launch the Script Editor (Applications/AppleScript/Script Editor) to begin scripting. The basic editor window is shown in Figure 21.1.
Figure 21.1 The Script Editor is used when editing or recording AppleScripts.
The Script Editor is composed of script recording and editing controls, which include:
- Description— This area is used to advertise the purpose and use instructions of your AppleScript.
- Recording/Playback— Similar to a tape deck, these buttons are used to control recording and playback of an AppleScript. Click the Record button (Command+D) to start monitoring your system for Apple events within scriptable applications. These events are then stored in a script. The Stop button (Command+.) is used to stop recording, whereas the Run button (Command+R) executes the actions.
- Check Syntax— Reviews the syntax of the current script for errors and automatically reformats the script if needed.
- Content— The content area is used to compose and edit script content. It functions like any Mac OS X text editor, but has the benefit of auto-formatting code when syntax is checked or the script is run.
Start using the editor by clicking the Record button and moving a few Finder windows around. As you move and open windows, the script will build in the editor window. Click Stop to finish off the code block and prepare it for execution. Figure 21.2 displays a script that has just finished generating.
Figure 21.2 Click Record to monitor your actions and build an AppleScript, and then click Stop to finish the script.
Scripting Dictionary
Obviously, the biggest draw to AppleScript is the capability to create scripts from scratch. Recording is a good way to get a quick start, but can't be used to generate anything truly useful. The basic AppleScript syntax will be covered later in this chapter. Even this, however, is useless without knowledge of what commands an application can accept. Luckily, you can view a scripting dictionary that shows the functions and properties offered by a given piece of software.
To access a scripting dictionary for any application, choose Open Dictionary from the File menu. A list of the available scriptable applications is displayed, as demonstrated by Figure 21.3.
Figure 21.3 Choose from the available scriptable applications.
Be aware that some applications might not be shown—the Browse button at the bottom of the window opens a standard File Open dialog box for choosing an arbitrary file. After picking an application, from the default or browse view, a dictionary window should appear, as seen in Figure 21.4.
Figure 21.4 The dictionary documents the available AppleScript functions.
Along the left side of the dictionary window is a list of the AppleScript functions that are provided. These functions are divided into categories, depending on their purpose. These categories are called suites. To display the syntax for a given item, click its name in the list. Highlighting a suite name displays a description of the functions within that grouping, and a complete view of the syntax for each.
AppleScript abstracts the parts of an application into objects within classes. These objects have properties that can be set or modified to effect changes to the object. The properties can also be retrieved with get to return results for evaluation. For example, the Finder has a file object with a file type property. The following script gets and displays the type for an arbitrary file:
1: tell application "Finder" 2: set thisFile to (choose file with prompt "Pick the file to examine:") 3: set theType to get the file type of thisFile 4: display dialog theType 5: end tell
Line 1 sends instructions to the Finder. Line 2 sets a variable called thisFile to point to a file. The choose command opens a file selection dialog box for visually selecting a file. Line 3 sets a variable called theType to the results of a command that gets the file type of the file reference by thisFile. Line 4 displays a dialog box containing the contents of theType. Finally, line 5 stops talking to the Finder.
This example introduces the structure you will see in most AppleScript programs. The tell, set, and get statements form the basis of scripts. The objects and the parameters that can be modified, however, will have to be looked up in the application's dictionary.
Results
When an AppleScript function returns a result, it is stored in a special temporary variable called result. This can be used to access a value without the need for additional variables. For example, lines 3 and 4 of the preceding script could be changed to
get the file type of thisFile display dialog the result
To display the contents of the result container within the Script Editor, choose Show Result (Command+L) from the Controls menu. Mac OS X will open a small window that contains the current value of result.
Syntax Highlighting
The Script Editor automatically highlights and formats AppleScript as you type. To change the default font styles and sizes, choose AppleScript Formatting from the Edit menu. The format dialog box can be seen in Figure 21.5.
Figure 21.5 AppleScript Formatting controls enable the user to adjust the appearance of the Script Editor.
Choose one of the lines in the formatting dialog box, and use the Font and Style menus to pick a new appearance for the item. To reset to the built-in defaults, click Use Defaults.
Script Tracing
To trace the execution of a script as it runs, use the Event Log (Command+E). This log keeps track of the events (commands) sent to an application and, if the Show Event Results button is checked, displays the results that are returned, immediately. Figure 21.6 shows the Event Log after replaying a simple script to get the location of a Finder window.
Figure 21.6 The Event Log can be used to monitor script execution.
Saving
After creating a script that functions the way you want, you can save it for double-click execution whenever you'd like. Choose Save As or Save As Run-Only from the File menu. The Run-Only option should be used to protect the script from future edits. Figure 21.7 displays the Save As dialog box.
Figure 21.7 Save a script for later execution.
There are three possible file formats for scripts:
- Text— Save the contents of the script in a plain-text file.
- Compiled Script— Save the script as a compiled binary file.
- Application— Save the script for double-click execution under Mac OS X.
When saving the script as an Application, there are two additional options in the lower-left corner of the dialog box. Choose Stay Open to have a script remain open after running, or Never Show Startup Screen to remove the AppleScript startup screen when the program runs.
Scripting Syntax | Next Section

Account Sign In
View your cart