如何使用变量值调用AppleScript中的函数/子例程?这是我想要做的一个例子(相反,它试图调用"某些东西"函数)
on HelloWorld()
display alert "Hello world."
end HelloWorld
set something to "HelloWorld"
something()
我希望它调用HelloWorld(变量值),而不是变量名称"某些东西"。
答案 0 :(得分:0)
正确的方法是将处理程序包装在脚本对象中并将它们放在可搜索的列表中:
-- 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"