为 运行 shell 带参数的脚本构建 Mac OS X Cocoa 应用程序
Build a Mac OS X Cocoa app for running a shell script with arguments
我正在 Swift 中构建一个 Mac OS X Cocoa 应用程序,它使用 rsync 来备份数据。该应用程序在 Finder 中为用户 select 提供源和目标,并且还能够 运行 脚本(具有硬编码的源和目标)。但我不知道如何将源和目标作为参数传递给脚本。
脚本是 运行 作者:
@IBAction func runScript(_ sender: NSButton) {
if sourcePath != nil && destinationPath != nil {
sender.isEnabled = false
let path = "/bin/bash"
let arguments = ["/path/to/script"]
let task = Process.launchedProcess(launchPath: path, arguments: arguments as! [String])
task.waitUntilExit()
sender.isEnabled = true
}
...
}
任何帮助将不胜感激,谢谢!
只需将参数添加到您的 arguments
数组:
let arguments = ["/path/to/script", sourcePath, destinationPath]
我正在 Swift 中构建一个 Mac OS X Cocoa 应用程序,它使用 rsync 来备份数据。该应用程序在 Finder 中为用户 select 提供源和目标,并且还能够 运行 脚本(具有硬编码的源和目标)。但我不知道如何将源和目标作为参数传递给脚本。
脚本是 运行 作者:
@IBAction func runScript(_ sender: NSButton) {
if sourcePath != nil && destinationPath != nil {
sender.isEnabled = false
let path = "/bin/bash"
let arguments = ["/path/to/script"]
let task = Process.launchedProcess(launchPath: path, arguments: arguments as! [String])
task.waitUntilExit()
sender.isEnabled = true
}
...
}
任何帮助将不胜感激,谢谢!
只需将参数添加到您的 arguments
数组:
let arguments = ["/path/to/script", sourcePath, destinationPath]