gpt4 book ai didi

function - AppleScript 处理程序中的可选参数

转载 作者:行者123 更新时间:2023-12-04 23:42:44 25 4
gpt4 key购买 nike

Applescript documentation说从优胜美地开始,处理程序的参数可以是可选的。

从“参数规范”部分:

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

它编译,但给了我错误“变量 thestring 未定义。”

如果我只用一个参数调用它:
set theResult to Create of me given thestring:"stuff"

我收到错误消息:“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*)

关于function - AppleScript 处理程序中的可选参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33035959/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com