将变量传递给 applescript 中的处理程序(函数)

passing a variable to a handler(function) in applescript

test_two()test_three work(),但 test_one() 没有。这是为什么?如何将参数传递给处理程序并将其用作局部变量?

set the_application to "Safari"
test_one(the_application)
on test_one(the_application)
    tell application the_application
        make new document with properties {URL:"http://www.whosebug.com"}
    end tell
end test_one
# ----

test_two()
on test_two()
    tell application "Safari"
        make new document with properties {URL:"http://www.whosebug.com"}
    end tell
end test_two
# ----

set the_application to "Safari"
test_three(the_application)
on test_three(the_application)
    tell application the_application
        activate
    end tell
end test_three

如果您想使用一些多个应用程序通用的术语,您可以使用其中一个的术语。此条款也适用于其他应用程序。您可以在代码的第一行指明 任何应用程序的名称 ,其中包含命令 make new document with properties 和 属性 URL,

set the_application to "some Safari like application"
test_one(the_application)

on test_one(the_application)
    using terms from application "Safari"
        tell application the_application
            make new document with properties {URL:"http://www.whosebug.com"}
        end tell
    end using terms from
end test_one

其他示例(在桌面上创建新文件夹):

set the_application to "Finder"
test_one(the_application)

on test_one(the_application)
    using terms from application "System Events"
        tell application the_application
            make new folder with properties {name:"TestFolder"}
        end tell
    end using terms from
end test_one

这是我的另一个可行解决方案,也许比我的第一个更好:

set the_application to "Safari"
test_one(the_application)

on test_one(the_application)
    set theScript to "tell application \"" & the_application & "\"
make new document with properties {URL:\"http://www.whosebug.com\"}
    end tell"
    run script theScript
end test_one