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:
round random number rounding up -- compile-time errorThe 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 tellBut AppleScript did, when it compiled:
tell application "System Events"
copy (name of every process where it is frontmost) to theProc
end tellThe reason seems to be to delimit a phrase implying a
get command. But if you actually use
get explicitly here without parentheses,
AppleScript refuses to compile at all:
tell application "System Events" copy get name of every process where it is frontmost to theProc -- ...
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