2.1. Working with Variables

Variables allow you to store data during your program's execution and give you the ability to refer to that data by a convenient name. You can store a value in a variable using the set or copy command, although I now introduce only the set command.

2.1.1. Variable Names and the set Command

The general format of the set command is as follows:

set  variable to value

Executing this command has the effect of storing the specified value (value) inside the variable (variable). As an example, the statement

set fileCount to 0

stores the value 0 inside the variable fileCount.

Variable names must begin with a letter (upper- or lowercase) or underscore character (_), and can be followed by any number of letters, digits (0-9), or underscore characters. You can't use a name that otherwise has a special meaning, such as the name of a command. Also, when talking to an application (see Chapter 10), you can't use a variable name that's part of that application's dictionary.

You actually can use a reserved word as a variable name, but the syntax is a little awkward. The method is described later in this section. So many words are significant AppleScript including command names, class names, operator names, and so on. Sometime the best way to see if a word has a special meaning is to type it into a statement in Script Editor and notice its color after compiling. If it's blue, it has a special meaning. If it's green, you can safely use it as a variable name in your program. ...

Get Beginning AppleScript® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.