This is the code I can't get to work. If I remove the directory part with the space my shell script runs fine.
shell script simplified is : /usr/bin/find $1 -name *.txt
error with the space is binary operator expected
let bundle = NSBundle.mainBundle()
let path = bundle.pathForResource("doSimpleFind", ofType: "sh")
let findTask = NSTask()
let pipe = NSPipe()
findTask.standardOutput = pipe
// directory where to search from the script. testing the space in the name
let constArgsString:String = "/Users/aUser/Library/Application\\ Support"
//let constArgsString:String = "/Users/aUser/Library/"
findTask.launchPath = path!
findTask.arguments = [constArgsString]
findTask.launch()
findTask.waitUntilExit()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let outputText:String = NSString(data: data, encoding:NSUTF8StringEncoding)!
cmdTextResult.textStorage?.mutableString.setString(outputText)
NSTask()
launches the process "directly" with the given arguments, without using a shell. Therefore it is not necessary to escape any
space or other characters in the launch arguments:
let constArgsString = "/Users/aUser/Library/Application Support"
But you have to handle arguments with spaces in your shell script correctly. In your example, $1
needs to be enclosed in double quotes,
otherwise it might be passed as multiple arguments to the find
command:
/usr/bin/find "$1" -name "*.txt"