AppleScript_ The Definitive Guide - Matt Neuburg [83]
Prepositional Parameters
Prepositional parameters are also called labeled parameters . Each parameter is preceded by a preposition drawn from the list in Table 9-1.
Table 9-1. The prepositions
above
beneath
into
against
beside
on
apart from
between
onto
around
by
out of
aside from
for
over
at
from
thru
below
instead of
under
In addition to the prepositions in Table 9-1, there is also a preposition of. This is used in a special way: if you use it, it must come first, and there must be more than one parameter. (This odd rule seems to be due to a mistake in the original design of AppleScript. In AppleScript 1.0, the of parameter was intended as a way of distinguishing the "direct object," the handler's main parameter. Then it was realized that where there was just one parameter and it was the of parameter, an unresolvable ambiguity with the of operator existed. So AppleScript 1.1 resolved the ambiguity by forbidding of to be used that way. But no alternative way of distinguishing the direct object was supplied, so in a sense this feature has been broken ever since.)
If a handler has prepositional parameters, the name of the handler in the definition is followed by a preposition and a variable name, and then possibly another preposition and another variable name, and so on.
In the call, the name of the handler is followed by a preposition and a value, and then possibly another preposition and another value, and so forth. The prepositions used must match those of the definition, but they may appear in any order, except for of, which must be first if it appears at all.
Here are some examples of handlers with prepositional parameters and calls to them:
on firstLetter from aWord
return character 1 of aWord
end firstLetter
display dialog (firstLetter from "hello")
on sum of x beside y
return x + y
end sum
display dialog (sum of 1 beside 2)
on stopping by woods on aSnowyEvening
return woods & aSnowyEvening
end stopping
display dialog (stopping on "horse" by "farm")
In the call, if the value you wish to pass is a boolean (Chapter 13), you may use with or without (to indicate true and false respectively) followed by the preposition. If you don't use this syntax, AppleScript will probably use it for you when it compiles the script: any prepositional parameters for which you pass the literal value true or false will end up as with or without followed by the preposition. Multiple with parameters or without parameters can be joined using and. This looks quite silly when the labels are prepositions, but here goes:
on stopping by woods on aSnowyEvening
if woods and aSnowyEvening then
return "lovely, dark and deep"
else
return "ugly and shallow"
end if
end stopping
display dialog (stopping with on and by)
display dialog (stopping with by without on)
There's a weird bug (at least I presume it's a bug) that allows you to define a handler with prepositional parameters but call it using positional parameters. The bug operates only in the case where you use of for the first parameter in the definition (which means that you also have to have a second parameter in the definition). When you call the handler with positional parameters, they all arrive as a list in the first parameter; the second parameter is empty. This bug was pointed out to me by Michael Terry, who notes that you can take advantage of it to implement a handler that can take any number of parameters. As your positional parameters are going to end up in a list anyway, there can be any number of them. AppleScript won't complain, no matter how few parameters there are (including none), and they all end up in a list in your handler, so none of the values is lost. For example:
on sum of L beside dummy
set total to 0
repeat with aNumber in L
set total to total + aNumber
end repeat
return total
end sum
sum(1, 2, 3, 4, 5, 6, 7) -- 28
(On some systems, the bug is not present, and there's an error at runtime. On others, AppleScript may crash. But this trick works fine on Tiger. Still,