Re: NSTask and NSPipe
Re: NSTask and NSPipe
- Subject: Re: NSTask and NSPipe
- From: Jorge Salvador Caffarena <email@hidden>
- Date: Wed, 27 Jun 2001 11:29:40 +0200
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?
Here you are, this works O.K. (tested):
NSTask *theTask = [[NSTask alloc] init];
NSMutableArray *theArgs = [NSMutableArray array];
NSPipe *thePipte = [NSPipe pipe];
NSFileHandle *pipeOutput = [thePipe fileHandleForReading];
NSData *outData = nil;
NSString *theString = nil;
[theArgs addObject:@"-al"];
[theArgs addObject:NSHomeDirectory()];
[theTask setLaunchPath:@"/bin/ls"];
[theTask setArguments:theArgs];
[theTask setStandardOutput:thePipe];
[theTask launch];
while ((outData = [pipeOutput availableData]) && ([outData length]) {
theString = [[NSString alloc] initWith
Data:outData
encoding:NSASCIIStringEncoding];
[theTextField setString:[[theTextField stringValue]
stringByAppendingString:theString]];
[theString release];
}
[theTask release];
Jorge Salvador Caffarena
http://homepage.mac.com/eevyl/