AppleScript Basics


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.

Tip

  • You can find a lot more information about AppleScript, including tutorials, sample scripts, and a reference manual, at Apple's AppleScript Web site, www.apple.com/applescript/.


AppleScript Files

There are three main types of AppleScript files (Figure 20):

  • Scripts (formerly compiled scripts) are completed scripts that can be launched from an application's script menu or the Script Menu. Double-clicking a compiled script icon launches Script Editor.

  • Applications (or applets) are full-fledged applications that can be launched by double-clicking their icons.

  • Text files are plain text files containing AppleScript statements. They can be opened with Script Editor or any text editor and can be run from within Script Editor. Double-clicking a script text file icon launches the application in which it was written.

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:

  • The Script Editor window (Figure 21) can automatically format script statements so they're easy to read.

    Figure 21. The Script Editor window with a very simple script. Note how Script Editor formats the script for easy reading.


  • The syntax checker can examine your script statements and identify any syntax errors that would prevent the script from running or compiling.

  • The Open Dictionary command makes it possible to view an application's dictionary of AppleScript commands and classes (Figure 22).

    Figure 22. The Standard Suite of Finder's AppleScript dictionary.


  • The record script feature can record actions as script steps.

  • The Save and Save As commands enable you to save scripts in a variety of formats.

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.


Tip

  • Although dictionaries are helpful for learning valid AppleScript commands, they are not sufficient for teaching a beginner how to write scripts.


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.

Tip

  • AppleScript Utility replaces Install Script Menu and Remove Script Menu, two utilities that appeared in the Mac OS X 10.3 version of AppleScript.


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.

Tip

  • To learn more about writing scripts for Folder Actions, visit www.apple.com/applescript/folder_actions/.




Visual QuickStart Guide. Mac OS X 10. 4 Tiger
Mac Os X 10.4 Tiger (Visual Quickstart Guides)
ISBN: 0321423372
EAN: 2147483647
Year: 2003
Pages: 301
Authors: Maria Langer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net