AppleScript 处理程序中的可选参数

Optional parameters in AppleScript handlers

Applescript documentation 表示从 Yosemite 开始,处理程序的参数可以设为可选。

来自 '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.

因此,由于需要一个带有可选参数的处理程序,我尝试创建一个。我已经走到这一步了:

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

编译,但给我错误 'The variable thestring is not defined.'

如果我只用一个参数调用它:

set theResult to Create of me given thestring:"stuff"

我收到错误:'The info parameter is missing for Create.' 即该参数毕竟不是可选的。

如何在 Applescript 处理程序中获取可选参数?

基本上语法没有变化,你可以在处理程序声明行中添加一个默认值。

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

关键字given用于布尔参数,可以用with(=true)和without(=false)关键字调用。

例如(来自文档)

to findNumbers of numberList above minLimit given rounding:roundBoolean
…
end findNumbers

findNumbers of {5.1, 20.1, 20.5, 33} above 20 with rounding

您必须为您的命令定义基于 SDEF 的术语才能使它起作用(这反过来意味着要处理 XML 和脚本包并可能产生术语冲突,等等)。它应该使 AppleScript 库更易于使用,但实际上只是在浪费大家的时间。

最简单的方法是使用普通的位置参数,让用户为 'optional' 个参数传递 missing value,您的处理程序可以检查这些参数:

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

或者让您的处理程序将单个记录作为其参数,然后将其与默认属性的记录连接起来:

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

两种解决方案都不理想;但在 AppleScript 中并非所有内容都是如此?

为了利用可选的标记参数,处理程序定义必须为您希望成为可选的参数分配一个默认值。然后,当调用者不提供该标记参数时,将使用默认值。

这是一个使用用户定义标签的示例,我发现它比神奇的 AppleScript 定义标签(of、by、for 等)更清晰

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*)