在 Swift 中使用带有参数的 运行 AppleScript 代码的过程

Using an process to run AppleScript codes with Arguments in Swift

运行 AppleScript 使用 Swift 中的进程需要这样的东西

let process = Process()
if process.isRunning == false {
    let pipe = Pipe()
    process.launchPath = "/usr/bin/osascript"
    process.arguments = ["/Users/user/Documents/activateApplication.scpt"]
    process.standardError = pipe
    process.launch()
}

但是如何使用进程发送更多参数?喜欢“Safari”并在目标脚本中使用此参数?

activateApplication.scpt 是:

on run appName
    tell application appName to activate
end run

osascript 手册页中对此进行了记录。在脚本文件之后将脚本参数作为附加参数传递,它们将作为列表出现在 run 处理程序的第一个(也是唯一一个)参数中。您的 Swift 代码看起来像这样:

    process.arguments = ["/Users/user/Documents/activateApplication.scpt", "Safari"]

...您的脚本将如下所示:

on run argv
    tell app (item 1 of argv) to activate
end