Function 使用变量';s值,用于在AppleScript中调用函数/子例程

Function 使用变量';s值,用于在AppleScript中调用函数/子例程,function,variables,applescript,Function,Variables,Applescript,如何使用变量的值调用AppleScript中的函数/子例程?下面是我想做的一个例子(它尝试调用“something”函数) 我希望它调用HelloWorld(变量值),而不是变量名“something”。正确的方法是将处理程序包装在脚本对象中,并将它们放在可搜索列表中: -- define one or more script objects, each with a custom `doIt()` handler script HelloWorld to doIt()

如何使用变量的值调用AppleScript中的函数/子例程?下面是我想做的一个例子(它尝试调用“something”函数)


我希望它调用HelloWorld(变量值),而不是变量名“something”。

正确的方法是将处理程序包装在脚本对象中,并将它们放在可搜索列表中:

-- 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"

正确的方法是将处理程序包装在脚本对象中,并将其放入可搜索列表中:

-- 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"