NTBTask
NTBTask copied to clipboard
A Cocoa class for running shell commands, supporting asynchronous I/O. Specifically it's a wrapper for NSTask making it simpler to use.
NTBTask
A Cocoa class for running shell commands, supporting asynchronous I/O. Specifically it's a wrapper for NSTask making it simpler to use.
Installation
Add NTBTask.h and NTBTask.m to your project.
Usage
Get output from command
NTBTask *task = [[NTBTask alloc] initWithLaunchPath:@"env"];
NSString *output = [task waitForOutputString];
Send input to command
NTBTask *task = [[NTBTask alloc] initWithLaunchPath:@"cat"];
NSString *input = @"What goes in, must come out";
[task write:input];
NSString *output = [task waitForOutputString];
Add arguments
task.arguments = @[ @"testing testing", @"123" ];
Run shell script
NTBTask *task = [[NTBTask alloc] initWithLaunchPath:@"bash"];
NSString *slowscript = @"echo 'sleeping for 0.3'\n"
"sleep 0.3\n"
"echo 'sleeping for 0.3'\n"
"sleep 0.3\n"
"echo 'sleeping for 0.3'\n"
"sleep 0.3";
[task writeAndCloseInput:slowscript];
[task launch];
Get continuous output
NTBTask *task = [[NTBTask alloc] initWithLaunchPath:@"cp"];
NSString *tempdir = NSTemporaryDirectory();
task.arguments = @[ @"-Rpnv", @".", tempdir ];
NSMutableString *result = [[NSMutableString alloc] init];
task.outputHandler = ^(NSString *output)
{
[result appendString:output];
[self newOutputAvailable:output];
};
task.completionHandler = ^(NTBTask *thistask)
{
[self doSomethingWhenCopyingIsFinished];
};
[task launch];