run "ls" through NSTask in a Cocoa app not working -
i testing use of nstask because want try run bunch of commands within cocoa application written in swift run in terminal.
in viewdidload have 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 getting following error:
failed set (contentviewcontroller) user defined inspected property on (nswindow): launch path not accessible
can me understand doing wrong? trying run ls command , store result array of strings...
thanks
well, starters, /usr/bash
not path bash
executable. want /bin/bash
.
next, not work invoke /bin/bash ls
, equivalent of you're doing. @ shell, if want give command bash
process , execute, /bin/bash -c ls
. so, want set tasks's arguments
["-c", "ls"]
.
however, why involving shell interpreter here, @ all? why not set task's launchpath
"/bin/ls"
, run ls
command directly? either leave arguments
unset, if want list current working directory's contents, or set arguments pass ls
, such options or path (e.g. ["-l", "/applications"]
).
Comments
Post a Comment