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 in decompiled code its 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 myHandler

That 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; for example, I might say "a repeat block."

Some blocks (just two, actually—tell blocks and if blocks) have single-line variants. This permits some rather twisted condensed syntax. For example:

tell application "Finder"
    if exists folder "Mannie" then
        reveal folder "Mannie"
    end if
end tell

You can reduce one or both of those blocks to a single line. So, this is legal (but I never talk this way, and I don't recommend you do either):

tell application "Finder" to if exists folder "Mannie" then reveal folder ...

Get AppleScript: The Definitive Guide, 2nd Edition 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.