Hello I want to open new app with arguments using terminal command with in swift code of my application MacOS
I have terminal command
open -n /Applications/test.app -- args arg1
this work fine when I run it in terminal
but when I am trying to run it using swift code
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)")
}
It doesn't work I also tried
let args:[String] = ["-c","open -n /Applications/test.app --args arg1"]
Thank you for any kind of hint or help
I'm pretty sure that instead of this
let args = ["-c","open -n /Applications/test.app","--args aaaa"]
it should be this
let args = ["-c", "open", "-n", "/Applications/test.app", "--args", "aaaa"]
Also, you don't really need to go through zsh. You could just call open
directly.
task.launchPath = "/usr/bin/open"
let args = ["-n", "/Applications/test.app", "--args", "aaaa"]