I have installed chrome-cli in my Mac. I'm trying to execute chrome-cli commands from Swift with following code:
import Foundation
func shell(_ command: String) -> String {
let task = Process()
let pipe = Pipe()
task.standardOutput = pipe
task.arguments = ["-c", command]
task.launchPath = "/bin/bash"
task.launch()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output = String(data: data, encoding: .utf8)!
return output
}
// Example 1:
let example1 = shell("/usr/local/bin/chrome-cli --help") //returns expected result
let example2 = shell("/usr/local/bin/chrome-cli list tabs") // return ""
Here, example 1 is calling with single argument and works well, i.e it print same result like we execute the command in Terminal.
but for example 2, we have more than 1 argument and it always returns empty result. While the same command on Terminal print list of tab currently opened.
I think the process doesn't wait for command to return the result as it takes 1-2 seconds to list tabs in Terminal app. or its some other issue?
Please help. Thanks.
task.errorOutput, check if it's not in the error one (do the same as the standard one for the error.task.standardErrorand attached a pipe to it which also returns empty result.