【问题标题】:Use variable's value for calling a function/subroutine in AppleScript使用变量的值在 AppleScript 中调用函数/子例程
【发布时间】:2017-07-07 18:38:11
【问题描述】:
如何使用变量的值在 AppleScript 中调用函数/子例程?这是我想做的一个例子(而是尝试调用“某事”函数)
on HelloWorld()
display alert "Hello world."
end HelloWorld
set something to "HelloWorld"
something()
我希望它调用 HelloWorld(变量值),而不是变量名称“某物”。
【问题讨论】:
标签:
function
variables
applescript
【解决方案1】:
正确的做法是将处理程序包装在脚本对象中并将它们放在可搜索的列表中:
-- define one or more script objects, each with a custom `doIt()` handler
script HelloWorld
to doIt()
display alert "Hello world."
end doIt
end script
script GoodnightSky
to doIt()
say "Goodnight sky."
end doIt
end script
-- put all the script objects in a list, and define a handler
-- for looking up a script object by name
property _namedObjects : {HelloWorld, GoodnightSky}
to objectForName(objectName)
repeat with objectRef in _namedObjects
if objectName is objectRef's name then return objectRef's contents
end repeat
error "Can't find object." number -1728 from objectName
end objectForName
-- look up an object by name and send it a `doIt()` command
set something to "HelloWorld"
objectForName(something)'s doIt() -- displays "Hello world"
set something to "GoodnightSky"
objectForName(something)'s doIt() -- says "Goodnight sky"