Blocks
A block is one or
more lines of code demarcated from its surroundings as having a
separate nature or purpose. A block is announced by a line stating
what type of block it is; then comes the code of the block; and
finally the block is terminated by a line starting with the
keyword
end. Blocks can occur within blocks.
It’s very easy to spot a block in AppleScript code, because at compile time the code lines are indented from the announcement line and the termination line. For example:
myHandler( )
on myHandler( )
repeat 3 times
display dialog "Howdy"
end repeat
end myHandlerThat code contains two blocks. One is announced with the on myHandler( ) line, and is terminated by the end myHandler line; everything in between them is the code of
that block. That code consists of another block, announced with the
repeat line and terminated by the end repeat line; the line of code in between them is the code
of that block.
In this book I frequently refer to such blocks by their announcement keyword or type; for example, I might say “an on block” or “a repeat block”.
The only blocks you can make in AppleScript are those for which keywords are supplied; you cannot indent arbitrarily for clarity, as you can in UserTalk or C. So for example in UserTalk you can say this:
local (x)
bundle
x = 4
msg (x)The keyword bundle here does nothing except to allow some code to be indented for clarity and to provide a further level of local scope. In AppleScript the scoping issue doesn’t arise (as we shall ...
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