Nonsensical Apple Events
As we saw in a previous section ("Compile-time Error"), AppleScript will sometimes blow the whistle at compile time to indicate that you are using a term as the wrong "part of speech." This is extremely helpful. For example, you can't use a verb as a noun:
tell application "Finder"
get name of eject -- compile-time error: Expected expression but found command name
end tell
And you can't assign to a class:
tell application "Finder"
set container to "howdy"
-- compile-time error: Can't set «class ctnr» to "howdy". Access not allowed
end tell
You have to supply valid parameter names:
tell application "Finder"
duplicate x by y -- compile-time error: Expected end of line but found "by"
end tell
The duplicate
command doesn't have a by
parameter, and the compiler knows this.
You can't make an element specifier out of something that isn't a class name:
tell application "Finder"
get name 1 -- compile-time error: Expected end of line but found number
end tell
The compiler also displays some intelligence about singular and plural forms of a class name. The plural form of a class name is taken to be a synonym for the every
element specifier; otherwise, if you use a plural where a singular is expected or vice versa, the compiler will usually change it for you, silently:
tell application "Finder" folder -- folder (the class name) folders -- {...}, a list of references to every folder folders 1 -- compiles as folder 1 folder 1 thru 2 -- compiles as folders 1 thru 2 end tell
This ...
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.