gpt4 book ai didi

swift - 构建一个 Mac OS X Cocoa 应用程序以运行带参数的 shell 脚本

转载 作者:行者123 更新时间:2023-11-28 05:59:35 26 4
gpt4 key购买 nike

我正在用 Swift 构建一个 Mac OS X Cocoa 应用程序,它使用 rsync 来备份数据。该应用程序让用户在 Finder 中选择源和目标,它还能够运行脚本(使用硬编码的源和目标)。但我不知道如何将源和目标作为参数传递给脚本。

脚本由以下人员运行:

@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 应用程序以运行带参数的 shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50222861/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com