Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/kareman/ntbtask

A Cocoa class for running shell commands, supporting asynchronous I/O. Specifically it's a wrapper for NSTask making it simpler to use.
https://github.com/kareman/ntbtask

Last synced: 30 days ago
JSON representation

A Cocoa class for running shell commands, supporting asynchronous I/O. Specifically it's a wrapper for NSTask making it simpler to use.

Awesome Lists containing this project

README

        

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

```Objective-C
NTBTask *task = [[NTBTask alloc] initWithLaunchPath:@"env"];
NSString *output = [task waitForOutputString];
```

#### Send input to command

```Objective-C
NTBTask *task = [[NTBTask alloc] initWithLaunchPath:@"cat"];
NSString *input = @"What goes in, must come out";
[task write:input];
NSString *output = [task waitForOutputString];
```
#### Add arguments

```Objective-C
task.arguments = @[ @"testing testing", @"123" ];
```

#### Run shell script

```Objective-C
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

```Objective-C
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];
```