I’m using NSTask in my Cocoa APP , and I need to be able to get result, and store it in Array, or something… I’m executing terminal commands from APP, and I need outputs for them.
NSString *path = @"/path/to/command";
NSArray *args = [NSArray arrayWithObjects:..., nil];
[[NSTask launchedTaskWithLaunchPath:path arguments:args] waitUntilExit];
//After task is finished , need output
Thanks a lot!
You want to use -[NSTask setStandardOutput:] to attach an NSPipe to the task before launching it. A pipe holds two file handles, the task will write to one end of the pipe, and you’ll read from the other. You can schedule the file handle to read all of the data from the background task and notify you when it’s complete.
It will look something like this (compiled in stack overflow):
If you also want to capture the output of standard error, you can use a second pipe and notification.