Flow-Control Statements
AppleScript includes syntax that you can use to test expressions and only have some code execute if certain conditions are met, as well as loop through code statements and then exit from the loop. These programming constructs are often called flow-control statements. These statements are covered extensively in Chapter 7.
Like many other programming languages, AppleScript uses an
if...then...else...end
if
statement to test various expressions. AppleScript also has an
if simple statement and an if
compound statement (one that extends over several lines). For
example, you can use code such as:
if numVar > 1000 then return 1000
on one line without including any end ifs. Example 1-13 includes both types of if
statements.
AppleScript uses several variations of the repeat
statement to loop through code (see Example 1-13).
Repeat is AppleScript’s version
of the for (;;;) or while... or foreach... loops of other languages
like Perl or Java. AppleScripters often use repeat
to loop through the values of a list. For example,
the code:
repeat with m in listVar...end repeat
will loop through all of the list members stored
in the variable listVar. Within the loop, the
iterator variable m will sequentially contain
each
list-member value. Example 1-13 includes several versions of the
repeat and if...end if
statements.
set userNum to 75.66 (* compound if statement *) if the class of userNum is real then display dialog "It's ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access