I am trying to run command lines to take screenshots in my Xcode project and using Pipe() to log output data. When I build and run this project in Xcode, I can get the output information as a string, like this screenshot's basic information. But when I archive this project as a MacOS applicaiton and run it, I get an error. I can't get any information for outputData.
The result of "outpipe.fileHandleForReading.readDataToEndOfFile()" is empty.
Here are my codes:
let task = Process()
task.launchPath = "/usr/sbin/screencapture"
var arguments = [String]();
arguments.append("-s")
let ScreenshotPath = "screenshotpath.jpg"
arguments.append(ScreenshotPath)
task.arguments = arguments
let outpipe = Pipe()
task.standardOutput = outpipe
task.standardError = outpipe
do {
try task.run()
} catch {
print("error in process")
}
let outputData = outpipe.fileHandleForReading.readDataToEndOfFile()
let resultInformation = String(data: outputData, encoding: .utf8)
task.waitUntilExit()
print(resultInformation)
Can somebody help me with this out? Thank you so much!