Introducing AppleScript


Apple event messages aren’t just for professional software engineers. Macintosh enthusiasts with little technical training can use Apple event messages to control applications by writing statements in the AppleScript language. For example, suppose that you want to quit all open applications. Mac OS X doesn’t have a Quit All command, but you can create one with AppleScript. You can use AppleScript commands to automate simple tasks as well as to automate a more complicated series of tasks, as the rest of this Chapter demonstrates.

AppleScript language

AppleScript is a language that you can use to tell applications what to do using natural language. AppleScript is a programming language that is designed especially to make it easy for computer users, not computer engineers, to build their own solutions. (Actually, engineers use it, too.)

You tell applications what to do by writing statements in the AppleScript language. Although AppleScript is an artificial language, its statements look like sentences in a natural language, such as English. You can look at many AppleScript statements and easily figure out what they’re supposed to do.

The words and phrases in AppleScript statements resemble English, but they are terms that have special meanings in the context of AppleScript. Some terms are commands, and some terms are objects that the commands act on. Other terms control how AppleScript performs the statements.

A single AppleScript statement can perform a simple task, but most tasks require a series of statements that are performed one after the other. A set of AppleScript statements that accomplishes a task (or several tasks) is called a script. A script can rename a batch of files, change an application’s preference settings, copy data from a database to another application, or automate a sequence of tasks that you previously performed one at a time by hand. You can develop your own script tools to accomplish exactly what you need.

As an added boon, AppleScript can actually watch you as you work with an application and write a script for you behind the scenes. This process is called script recording.

Although AppleScript is designed to be a simple to understand language, it offers all the capabilities of a traditional programming language and won’t frustrate programmers and advanced users. You can store information in variables for later use; write if...then statements to perform commands selectively according to a condition that you specify; or repeat a set of commands as many times as you want. AppleScript also offers error checking and object-oriented programming.

Scripting additions

AppleScript has an expandable lexicon of terms. It knows meanings of basic terms, and it augments this knowledge with terms from other sources. Many additional AppleScript terms come from the very applications that AppleScript controls. We explore this source of AppleScript terms in greater detail later in this Chapter.

Additional AppleScript terms also come from special files called scripting additions. AppleScript looks for scripting addition files in the following folders:

  • ScriptingAdditions. In the Library folder of the System folder (path /System/Library/ScriptingAdditions/) — contains standard scripting additions from Apple that are available to all users of your computer. (This folder may also contain other files that are not scripting additions.)

  • Scripts. In the main Library folder (path /Library/Scripts/) — contains more scripting additions that are available to all users of your computer.

  • Scripting Additions. In the folder named System Folder (path /System Folder/ Scripting Additions/ unless you have moved or renamed System Folder) — contains more scripting additions that are available in the Classic environment to all users of your computer.

  • Scripting Additions and Scripts. In the Extensions folder of the folder named System Folder (path /System Folder/Extensions/Scripting Additions/ and path /System Folder/Extensions/Scripts/ unless you have moved or renamed System Folder) — contains more scripting additions that are available in the Classic environment to all users of your computer. (This Scripting Additions folder usually exists only on computers that previously used Mac OS 8.)

You need separate scripting additions for Mac OS X and the Classic environment. Scripting additions made for Mac OS X do not work in the Classic environment. Conversely, scripting additions made for Mac OS 9 and earlier work only in the Classic environment. If you get more scripting additions made for Mac OS X, put them in one of the ScriptingAdditions folders that is inside a Library folder. If you get more scripting additions made for Mac OS 9, put them in one of the Scripting Additions folders that are inside the System Folder used for the Classic environment.

Tip

If the ScriptingAdditions folder doesn’t exist in a Library folder where you want to put a scripting addition file, create a new folder and name it ScriptingAdditions (put no spaces in the name). Put your scripting addition file in this new folder.




Mac OS X Bible, Panther Edition
Mac OS X Bible, Panther Edition
ISBN: 0764543997
EAN: 2147483647
Year: 2003
Pages: 290

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