问题
The Applescript documentation says that as of Yosemite, parameters for handlers can be made optional.
From the section 'Parameter Specifications':
Labeled parameters may be declared with a default value by following the formal parameter name with :literal. Doing so makes the parameter optional when called. For example, this declares a make handler with a default value for the with data parameter:
on make new theClass with data theData : missing value
This handler can now be called without supplying a with data parameter; the handler would see theData set to the specified default missing value, which it could then test for and handle appropriately.
So, being in need of a handler with optional parameters, I tried to create one. I have got this far:
set theResult to Create of me given the string:"stuff", info:"this"
on Create given info:missing value, thestring:"stuff"
if info is missing value then
set present to false
else
set present to true
end if
return {present, thestring}
end Create
which compiles, but gives me the error 'The variable thestring is not defined.'
If I call it with only one parameter:
set theResult to Create of me given thestring:"stuff"
I receive the error: 'The info parameter is missing for Create.' i.e. the parameter isn't optional after all.
How can I get optional parameters working in Applescript handlers?
回答1:
You have to define SDEF-based terminology for your command for this to work (which in turn means mucking about with XML and script bundles and potentially creating terminology conflicts, and so on). It's supposed to make AppleScript libraries friendlier to use, but it's really just a waste of everyone's time.
It's simplest just to use normal positional parameters and just have the user pass missing value
for 'optional' arguments, which your handler can check for:
set theResult to Create("stuff", "this")
set theResult to Create("stuff", missing value)
set theResult to Create(missing value, missing value)
on Create(thestring, info)
if thestring is missing value then set thestring to "stuff"
set present to info is not missing value
return {present, thestring}
end Create
or else have your handler take a single record as its parameter then concatenate that with a record of default properties:
set theResult to Create for {thestring:"stuff", info:"this"}
set theResult to Create for {thestring:"stuff"}
set theResult to Create for {}
on Create for args
set args to args & {info:missing value, thestring:"stuff"}
set present to info is not missing value
return {present, thestring of args}
end Create
Neither solution is ideal; but isn't true of everything in AppleScript?
回答2:
In order to take advantage of optional labeled parameters, the handler definition MUST assign a default value to the parameter(s) you want to be optional. Then, when the caller does not supply that labeled parameter, the default value is used.
Here is an example using user-defined labels, which I find clearer than the magic AppleScript-defined labels (of, by, for, etc.)
SayWhat given greeting:"Hola", farewell:"Adios"
SayWhat given greeting:"Aloha"
SayWhat given farewell:"Ciao"
on SayWhat given greeting:strGreeting : "Hello", farewell:strFarewell : "Goodbye"
log "You say " & strGreeting & "; I say " & strFarewell
end SayWhat
(*You say Hola; I say Adios*)
(*You say Aloha; I say Goodbye*)
(*You say Hello; I say Ciao*)
回答3:
Basically the syntax hasn't change, you can just add a default value in the handler declaration line.
set theResult to Create from "stuff" by "this" --> {true, "stuff"}
set theResult to Create from "stuff" --> {false, "stuff"}
on Create by info : missing value from thestring : "stuff"
if info is missing value then
set present to false
else
set present to true
end if
return {present, thestring}
end Create
The keyword given
is for boolean parameters which can be called with with
( = true
) and without
( = false
) keywords.
For example (from the documentation)
to findNumbers of numberList above minLimit given rounding:roundBoolean
…
end findNumbers
findNumbers of {5.1, 20.1, 20.5, 33} above 20 with rounding
来源:https://stackoverflow.com/questions/33035959/optional-parameters-in-applescript-handlers