Parentheses
Parentheses may be used to determine the order of operations at runtime:
3 + 4 * 2 -- 11 (3 + 4) * 2 -- 14
Parentheses can also help determine the order of interpretation of vocabulary at compile time. Thus they can make the difference between successful compilation and failed compilation. For example, this compiles fine, because all the expressions are legal:
set r to random number round r rounding up
Now try to save a line by combining them, and you get this ungrammatical and mysterious error:
round random number rounding up
-- compile-time error: A application constant [sic] or consideration can't
go after this identifier
The problem is that random number
is a command that can optionally take various labeled parameters, and rounding up
isn't one of them. Instead of rethinking its interpretation ("So, maybe random number
isn't taking any parameters here!"), AppleScript just gives up. You have to help it out, by using parentheses:
round (random number) rounding up
Sometimes AppleScript will insert parentheses for you, on compilation. For example, I didn't put any parentheses when I typed this code:
tell application "System Events" copy name of every process where it is frontmost to theProc end tell
But AppleScript did, when it compiled:
tell application "System Events" copy (name of every process where it is frontmost) to theProc end tell
The reason seems to be to delimit a phrase implying a get
command. But if you actually use get
explicitly here without parentheses, AppleScript ...
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.