In Applescript, if you declare a handler using "with" labeled parameters, local variables get the values of the arguments and the parameters themselves are undefined. For example:
on bam of thing with frst and scnd
local eat_frst
return {thing: thing, frst:frst, scnd:scnd} -- this line throws an error
end bam
bam of "bug-AWWK!" with frst without scnd
results in an error message that "scnd" isn't defined in the second line of bam
. thing
and frst
are both defined, getting the arguments passed in the call to bam
. Why is this happening? Why is scnd
undefined?
Note: I know that declaring variables as "local" within a handler is unnecessary. It's done in the examples for illustrative purposes.
Here are some more examples that don't throw errors, illustrating what variable gets what value. To distinguish between the first and second given parameters, each handler is invoked with
the first given parameter and without
the second given parameter. Note that using the given userLabel:userParamName
syntax has no problems with value capturing.
on foo of thing given frst:frst_with, scnd:scnd_with
local eat_nothing
return {frst:frst_with, scnd:scnd_with}
end foo
on bar of thing with frst and scnd
local eat_frst
return {frst:eat_frst, scnd:scnd}
end bar
on baz of thing with frst and scnd
eat_frst
local eat_scnd, eat_others
return {frst:eat_frst, scnd:eat_scnd}
end baz
{foo:(foo of "foo" with frst without scnd), ¬
bar:(bar of "bar" with frst without scnd), ¬
baz:(baz of "baz" with frst without scnd)}
Result:
{ foo:{frst:true, scnd:false}, bar:{frst:true, scnd:false}, baz:{frst:true, scnd:false}}