Language Guide
CHAPTER 6
Expressions
162 Operations
When evaluating expressions containing operators, AppleScript checks the
leftmost operand first. If the operand does not belong to one of the legal classes
for the operator, AppleScript coerces it if possible. After coercing the leftmost
operand or verifying that it belongs to a legal class, AppleScript checks the
rightmost operand and coerces it (if necessary and possible) to be compatible
with the leftmost operand. The exceptions to this rule are expressions with the
Is Contained By, Equal, and Is Not Equal operators. AppleScript checks the
rightmost operand first in expressions with the Is Contained By operator.
AppleScript never coerces operands of the Equal and Is Not Equal operators.
If AppleScript cannot coerce the operands, it returns an error. For example, the
addition operator (+) works with numbers (integers and real numbers) only. If
you attempt to evaluate an expression such as 3 + "cat", you’ll get an error,
because AppleScript cannot coerce "cat" to a number.
Operations can be performed either by AppleScript or by an application. The
rule is that if the leftmost operand is a value, AppleScript performs the
operation, and if the leftmost operand is a reference to an application object,
the application performs the operation. For example, the comparison
"Hello" contains word 1 of document "Report"
is performed by AppleScript, because the first operand is a string. Before
performing the comparison, AppleScript must get the value of the first
word. In contrast, the comparison
word 1 of document "Report" contains "Hello"
is performed by the application containing the document named Report.
The Is Contained By operator is an exception to this rule. In expressions with
the Is Contained By operator, AppleScript performs the operation if the
rightmost operand is a value and the application performs the operation if the
rightmost operand is a reference to an application object.
Table 6-1 summarizes the AppleScript operators. For each operator, it includes a
brief description of the operation and lists the class (or classes) of the operands
and the class (or classes) of the result. A few of the operators are characters that
you type with modifier keys. For these operators, the keystrokes are shown in
parentheses. The section following the table provides more information about
how operators treat different classes of operands.
The sections following the table contain more detailed explanations and
examples of operations.