Applescriptでは、「with」ラベル付きパラメータを使用してハンドラを宣言すると、ローカル変数は引数の値を取得し、パラメータ自体は未定義です。例えば:
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
の2行目に「scnd」が定義されていないというエラーメッセージが表示されbam
ます。thing
とfrst
は両方とも定義されており、への呼び出しで渡された引数を取得しますbam
。なぜこうなった?なぜscnd
未定義なのですか?
注:ハンドラー内で変数を「ローカル」として宣言する必要がないことは知っています。これは、説明のために例で行われています。
エラーをスローしないその他の例をいくつか示し、どの変数がどの値を取得するかを示します。最初に指定されたパラメーターと2番目に指定されたパラメーターを区別するために、各ハンドラーはwith
最初に指定されたパラメーターとwithout
2番目に指定されたパラメーターを呼び出されます。構文を使用しても、値の取得に問題はないことに注意してください。given userLabel:userParamName
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)}
結果:
{foo:{frst:true、scnd:false}、 bar:{frst:true、scnd:false}、 baz:{frst:true、scnd:false}}