Mac Os X 10.4 Tiger (Visual Quickstart Guides)
AppleScript (Figure 19) is the scripting language that comes with Mac OS. It enables you to automate tasks and extend the functionality of Mac OS X. Figure 19. The contents of the AppleScript folder inside the Applications folder.
You use AppleScript's Script Editor application to write small programs or scripts that include specially worded statements. AppleScript statements are converted by Mac OS into Apple eventsmessages that can be understood by the operating system and applications. When you run a script, the script can send instructions to the operating system or applications and receive messages in return. For example, say that at the end of each working day, you back up the contents of a specific folder to a network disk before you shut down your computer. The folder is large and the network is slow, so you often have to wait ten minutes or more to shut down the computer when the backup is finished. You can write a script that mounts the network drive, backs up the folder, and shuts down your computer automatically. You simply run the script, turn out the lights, and go home. AppleScript does the rest. In this part of the chapter, I introduce AppleScript's components to give you an idea of how it works and what you can do with it.
AppleScript Files
There are three main types of AppleScript files (Figure 20):
Figure 20. The three basic file formats for an AppleScript: a script (left), an application (middle), and a text file (right). Note the file name extensions for these formats.
Script Editor
Script Editor is an application you can use to write AppleScript scripts. It has a number of features that make it an extremely useful tool for script writing:
AppleScript Dictionaries
Scriptable applications include AppleScript dictionaries, which list and provide syntax information for valid AppleScript commands and classes. These dictionaries are a valuable reference for anyone who wants to write scripts. An AppleScript dictionary is organized into suites. Each suite includes a number of related commands and objects. Commands are like verbsthey tell an application to do something. Objects, which include classes and elements, are what a command can be performed on. For example, in TextEdit's Standard Suite, close is a command that can be performed on an object such as window. Properties help distinguish objects; for example, the file type property can help distinguish one file object from another. Figures 22 and 23 show examples of AppleScript Dictionaries for two applications: Finder and TextEdit. If you've worked with previous versions of Script Editor, you may notice that the Dictionary window has been reworked for Mac OS X 10.4 to better organize information. Colored symbols help visually distinguish between suites, commands, classes, elements, and properties. You can click an item in one of the top three columns or a link in the bottom half of the window to instantly display related information in the bottom half of the window. Figure 23. The Standard Suite of TextEdit's AppleScript dictionary.
AppleScript Utility
AppleScript Utility, which is new in Mac OS X 10.4, enables you to set various scripting options, such as the default script editing application, and install or remove a Script Menu. When installed, the Script Menu adds a menu full of example scripts to the menu bar. You can modify this menu by removing scripts you don't use or adding your own custom scripts.
Folder Actions
Folder Actions is a feature of Mac OS X that works with AppleScript. You create a script that performs a specific task, then attach that script to a folder. When the folder is modified in a predefined wayfor example, when it is opened or a file is added to itthe script activates and performs its task. How can Folder Actions help you? Here's an example. Suppose you're writing a book and every time you finish a chapter, you need to upload a copy of it to an FTP site so your editors can download and review it. You can write a script that uploads any new file added to a folder to the FTP site. Attach that script to a folder and voilà! Every time you save a copy of a chapter to the folder, it is automatically sent for review.
|