The Applescript 文档 https://developer.apple.com/library/mac/documentation/AppleScript/Conceptual/AppleScriptLangGuide/conceptual/ASLR_about_handlers.html说从 Yosemite 开始,处理程序的参数可以是可选的。
从“参数规格”部分:
带标签的参数可以通过在形式参数名称后面加上 :literal 来声明默认值。这样做会使参数在调用时成为可选参数。例如,这声明了一个 make 处理程序,其 with data 参数具有默认值:
on make new theClass with data theData : missing value
现在可以在不提供 with data 参数的情况下调用该处理程序;处理程序将看到数据设置为指定的默认缺失值,然后可以对其进行测试并进行适当处理。
因此,由于需要一个带有可选参数的处理程序,我尝试创建一个。我已经做到了这一点:
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
它可以编译,但给出错误“变量 thestring 未定义。”
如果我只用一个参数调用它:
set theResult to Create of me given thestring:"stuff"
我收到错误:“创建时缺少信息参数。”即该参数毕竟不是可选的。
如何获取在 Applescript 处理程序中工作的可选参数?