Chapter 12. Control
This chapter describes the control structures of the AppleScript language. These are not commands; rather, they dictate the flow of a script—how the next line or batch of lines should be interpreted, what line should be executed next, that sort of thing.
Tip
When typing any block in this chapter, in the termination line just
type the word end
. AppleScript fills in the
missing term after compilation. This saves time and is helpful for
confirming that you have correctly structured your blocks. So, for
example, don’t type end if
; just
type end
.
Branching
The “intelligent” behavior of a computer program depends upon its ability to make choices at runtime. These choices generally take the form of evaluating some expression and executing a particular set of lines of code depending on how the evaluation turns out at that moment.
One major form of choice is branching. We have a line or block of code that can be executed optionally. The computer evaluates a boolean expression, called a condition . If the condition is true, the line or block of code is executed; if it isn’t, the line or block of code is skipped, and execution jumps to the line that follows it.
In AppleScript, branching control is performed with
if
. There are several forms of if
block.
Tip
When typing a multiline if block, don’t bother to
type the word then
. AppleScript will add it at
compile time.
The basic form is a block of code that is executed only if a condition is true. If the condition is false, the ...
Get AppleScript: The Definitive Guide 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.