AppleScript Lexical Conventions
AppleScript Lexical Conventions
Identifiers
An AppleScript identifier is a series of characters that identifies a class name, variable, or other
language element, such as labels for properties and handlers.
An identifier must begin with a letter and can contain any of these characters:
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_
Identifiers are not case sensitive. For example, the identifiers myvariable and MyVariable are
equivalent.
AppleScript remembers and enforces the first capitalization it comes across for an identifier. So if it
first encounters an identifier as myAccount, it will later, during compilation, change versions such
as MyAccount and myaccount to myAccount.
The following are not valid identifiers: C-, back&forth, 999, Why^Not.
AppleScript provides a loophole to the preceding rules: identifiers whose first and last characters
are vertical bars (|) can contain any characters. The leading and trailing vertical bars are not
considered part of the identifier.
Important: This use of vertical bars can make scripts difficult to read, and is not recommended.
An identifier can contain additional vertical bars preceded by a backslash (\) character, as in the
identifier |This\|Or\|That|. Use of the backslash character is described further in the Special
String Characters section of the text class.
Keywords
A keyword is a reserved word in the AppleScript language. Keywords consist of lower-case,
alphabetic characters: abcdefghijklmnopqrstuvwxyz. In a few cases, such as aside from, they
come in pairs.
Important: You should not attempt to reuse keywords in your scripts for variable names or
other purposes. Developers should not re-define keywords in the terminology for their
scriptable applications.
Table 1-1 lists the keywords reserved in AppleScript 2.0 (which are the same as those used in
AppleScript 1.x). For additional information, see Table A-1, which provides a brief description for
each keyword and points to related information, where available.
without
Comments
A comment is text that is ignored by AppleScript when a script is executed. You can use comments
to describe what is happening in the script or make other kinds of notes. There are three kinds of
comments:
A block comment begins with the characters (* and ends with the characters *). Block
comments must be placed between other statements. That means they can be placed on the
same line at the beginning or end of a statement, but cannot be embedded within a simple
(one-line) statement.
An end-of-line comment begins with the characters -- (two hyphens) and ends with the end of
the line:
Starting in version 2.0, AppleScript also supports use of the # symbol as an end-of-line
comment. This allows you to make a plain AppleScript script into a Unix executable by
beginning it with the following line and giving it execute permission:
#!/usr/bin/osascript
Compiled scripts that use # will run normally on pre-2.0 systems, and if edited will display
using --. Executable text scripts using #!/usr/bin/osascript will not run on pre-2.0
systems, since the # will be considered a syntax error.
You can nest comments—that is, comments can contain other comments, as in this example:
A continuation character within a quoted text string is treated like any other character.
Boolean
AppleScript defines the Boolean values true and false and supplies the boolean class.
Constant
Global Constants in AppleScript describes constants that can be used throughout your scripts. For
related information, see the constant class.
List
A list defines an ordered collection of values, known as items, of any class. As depicted in a script,
a list consists of a series of expressions contained within braces and separated by commas, such
as the following:
A list can contain other lists. An empty list (containing no items) is represented by a pair of empty
braces: {}.
Number
A numeric literal is a sequence of digits, possibly including other characters, such as a unary minus
sign, period (in reals), or "E+" (in exponential notation). The following are some numeric literals:
-94596
3.1415
9.9999999999E+10
AppleScript defines classes for working with real and integer values, as well as the number
class, which serves as a synonym for either real or integer.
Record
A record is an unordered collection of labeled properties. A record appears in a script as a series of
property definitions contained within braces and separated by commas. Each property definition
consists of a unique label, a colon, and a value for the property. For example, the following is a
record with two properties:
{product:"pen", price:2.34}
Text
A text literal consists of a series of Unicode characters enclosed in a pair of double quote marks,
as in the following example:
AppleScript text objects are instances of the text class, which provides mechanisms for working
with text. The Special String Characters section of that class describes how to use white space,
backslash characters, and double quotes in text.
Operators
An operator is a symbol, word, or phrase that derives a value from another value or pair of values.
For example, the multiplication operator (*) multiplies two numeric operands, while the
concatenation operator (&) joins two objects (such as text strings). The is equal operator
performs a test on two Boolean values.
Variables
A variable is a named container in which to store a value. Its name, which you specify when you
create the variable, follows the rules described in Identifiers. You can declare and initialize a
variable at the same time with a copy or set command. For example:
When AppleScript encounters a variable, it evaluates the variable by getting its value. A variable is
contained in a script and its value is normally lost when you close the script that contains it.
AppleScript variables can hold values of any class. For example, you can assign the integer value
17 to a variable, then later assign the Boolean value true to the same variable.
Expressions
An expression is any series of lexical elements that has a value. Expressions are used in scripts to
represent or derive values. The simplest kinds of expressions, called literal expressions, are
representations of values in scripts. More complex expressions typically combine literals, variables,
operators, and object specifiers.
When you run a script, AppleScript converts its expressions into values. This process is known as
evaluation. For example, when the following simple expression is evaluated, the result is 21:
3 * 7 --result: 21
An object specifier specifies some or all of the information needed to find another object. For
example, the following object specifier specifies a named document:
Statements
A statement is a series of lexical elements that follows a particular AppleScript syntax. Statements
can include keywords, variables, operators, constants, expressions, and so on.
Every script consists of statements. When AppleScript executes a script, it reads the statements in
order and carries out their instructions.
A control statement is a statement that determines when and how other statements are executed.
AppleScript defines standard control statements such as if, repeat, and while statements, which
are described in detail in Control Statements Reference.
Note: You can use a continuation character (¬) to extend a simple statement onto a second line.
A compound statement is written on more than one line, can contain other statements, and has the
word end (followed, optionally, by the first word of the statement) in its last line. For example the
following is a compound tell statement:
Commands
A command is a word or series of words used in an AppleScript statement to request an action.
Every command is directed at a target, which is the object that responds to the command. The
target is usually an application object or an object in macOS, but it can also be a script object or
a value in the current script.
The following statement uses AppleScript’s get command to obtain the name of a window; the
target is the front window of the Finder application:
For more information on command types, parameters, and targets, see Commands Overview.
Results
The result of a statement is the value generated, if any, when the statement is executed. For
example, executing the statement 3 + 4 results in the value 7. The result of the statement set
myText to "keyboard" is the text object "keyboard". A result can be of any class. AppleScript
stores the result in the globally available property result, described in AppleScript Constant.
Raw Codes
When you open, compile, edit, or run scripts with a script editor, you may occasionally see terms
enclosed in double angle brackets, or chevrons («»), in a script window or in another window.
These terms are called raw format or raw codes, because they represent the underlying Apple
event codes that AppleScript uses to represent scripting terms.
For compatibility with Asian national encodings, “《” and “》” are allowed as synonyms for “«” and
“»” ( (Option- \ and Option-Shift- \, respectively, on a U.S. keyboard), since the latter do not exist
in some Asian encodings.
Previous Next
Copyright © 2016 Apple Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2016-01-25