Re: NSTask and NSPipe
Re: NSTask and NSPipe
- Subject: Re: NSTask and NSPipe
- From: "Mike Vannorsdel" <email@hidden>
- Date: Wed, 27 Jun 2001 02:30:31 -0600
Here's an example of piping "ls -al" to a text field:
NSTask * theTask = [[NSTask alloc] init]; //init'ing the task
NSArray * args = []NSArray alloc] initWithObjects:@"-al", nil]; //init'ing
arguments
NSPipe * outPipe = [NSPipe pipe]; //creating a pipe to use for output
NSFileHandle * handleToPipe = [outPipe fileHandleForReading]; //making a file
handle to the pipe so we can read from it
NSString * taskOutput; //string pointer for later use
//start task code
[theTask setLaunchPath:@"/bin/ls"];
[theTask setArguments:args];
[theTask setStandardOutput:outPipe];
[theTask launch];
[theTask waitUntilExit];
[theTask release];
[args release];
taskOutput = [[NSString alloc] initWith
Data:[handleToPipe
readDataToEndOfFile] encoding:NSASCIIStringEncoding]; //putting the
output into a string
[myTextField setStringValue: taskOutput];
[taskOutput release];
That's it.
On Tuesday, June 26, 2001, at 03:13 PM, Darin Duphorne wrote:
Can anyone provide me a simple example of how to pipe standardoutput
from a NSTask to a textfield on a window? I can't for the life of me
get it to work. Perhaps a simple example of calling ls -al and
displaying in a tableview or textfield?