The Top Level

The top level of all is the script as a whole. (Okay, I lied. There are actually some secret levels even higher than that. But just pretend, for now, that there aren't.) What's more, the script as a whole is itself a script object, even though it has no name and is not defined by a block.

This strange-sounding fact is actually extremely cool. It gives your script a sort of overall uniformity. For example, a handler definition can't occur anywhere except at the top level of a script object. So why is it possible for this to be a script?

on sayHowdy( )
    display dialog "howdy"
end sayHowdy
sayHowdy( )

There's a handler definition sitting there, not nested in a script block. But that doesn't break the rule; it accords with it, because the script as a whole is itself a script object, so this handler definition is in fact at the top level of a script object, precisely where it should be.

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.