Language Guide
CHAPTER 7
Control Statements
If Statements 191
else
set myMessage to " is equal to "
end if
set myResult to (x as string) & myMessage & (y as string)
If the expression x > y is true, the value of the variable myMessage is set to
" is greater than " and the If statement is finished. Control passes to the
Set statement, which uses the value of the variable myMessage to set the value
of another variable, called myResult. The value of myResult is a string such
as "7 is greater than 5". If the first Boolean expression is false, the
next expression, x < y, is evaluated with similar results.
An If statement can contain any number of Else If clauses; AppleScript looks
for the first Boolean expression contained in an If or Else If clause that is true,
executes the statements contained in its block (the statements between one Else
If and the following Else If or Else clause), and then exits the If statement.
An If statement can also include a final Else clause. The statements in its block
are executed if no other test in the If statement passes. For example, suppose
the values of x and y in the previous example are both 112. The first two tests,
x > y and x < y, fail. The value of the variable myMessage is set to " is
equal to ", and the value of myResult is "112 is equal to 112".
If statements can be more elaborate, as in this example:
display dialog "How many dependents?" default answer ""
set dependents to (text returned of result) as integer
display dialog "Have you ever been audited?" buttons ¬
{"No", "Yes"}
if button returned of result = "Yes" then
set audit to true
else
set audit to false
end if
if dependents < 9 and audit = false then
display dialog "No extra forms are required."
else if dependents < 9 and audit = true then
display dialog "You might need to file an extra form."