你好,我想在我的应用程序MacOS 的swift代码中使用终端命令打开带有参数的新应用程序
我有终端命令open -n /Applications/test.app -- args arg1
当我在终端中运行它时,它工作得很好
但当我试图使用swift代码运行它时
static func shellCommand () {
let task = Process()
task.launchPath = "/bin/zsh"
let args:[String] = ["-c","open -n /Applications/test.app","--args aaaa"]
task.arguments = args
let pipe = Pipe()
let errorPipe = Pipe()
task.standardOutput = pipe
task.standardError = errorPipe
task.launch()
task.waitUntilExit()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output = String(data: data, encoding: String.Encoding.utf8)!
let errorData = errorPipe.fileHandleForReading.readDataToEndOfFile()
let error = String(decoding: errorData, as: UTF8.self)
print("out put from shell command (output) error (error)")
}
它不起作用我也试过了let args:[String] = ["-c","open -n /Applications/test.app --args arg1"]
感谢您的任何提示或帮助
我很确定
let args = ["-c","open -n /Applications/test.app","--args aaaa"]
应该是这个
let args = ["-c", "open", "-n", "/Applications/test.app", "--args", "aaaa"]
此外,你并不真的需要通过zsh。您可以直接致电open
。
task.launchPath = "/usr/bin/open"
let args = ["-n", "/Applications/test.app", "--args", "aaaa"]