I am testing the use of an NSTask because I want to try to run a bunch of commands within my Cocoa application written in Swift that I usually run in Terminal.
In viewDidLoad I have the following code:
let task = NSTask()
let pipe = NSPipe()
task.standardOutput = pipe
task.launchPath = "/usr/bash"
task.currentDirectoryPath = dir
task.arguments = ["ls"]
let file:NSFileHandle = pipe.fileHandleForReading
task.launch()
task.waitUntilExit()
let data = file.readDataToEndOfFile()
let datastring = NSString(data: data, encoding: NSUTF8StringEncoding)
print("datastring = \(datastring)")
The app runs but I am getting the following error:
Failed to set (contentViewController) user defined inspected property on (NSWindow): launch path not accessible
Can somebody help me understand what I am doing wrong? What I am trying to do now is to run the ls command and then store the result into an array of strings...
Thanks
ls
is a real example consider using the native Foundation classNSFileManager
instead. – vadian