NSTask & NSPipe
NSTask & NSPipe
- Subject: NSTask & NSPipe
- From: Darin Duphorne <email@hidden>
- Date: Wed, 27 Jun 2001 14:54:08 -0500
Thanks to several folks, my code now works and is below; but, it only
works if the returned text is fairly short. It doesn't work for longer
strings. Is this because I didn't use the NotificationCenter? Any
ideas? By the way, I have replaced the "ls -al" command with a text
tool for returning bible verses. This is the purpose of the exercise.
#import "brsWrapper.h"
@implementation brsWrapper
- (IBAction)get:(id)sender
{
NSTask *theTask=[[NSTask alloc] init];
NSMutableArray *theArgs=[NSMutableArray array];
NSPipe *thePipe=[NSPipe pipe];
NSFileHandle *pipeOutput=[thePipe fileHandleForReading];
NSData *outData=nil;
NSString *theString=nil;
[theArgs addObject:[brsEntryField stringValue]];
[theTask setLaunchPath:@"/usr/bin/bible"];
[theTask setArguments:theArgs];
[theTask setStandardOutput:thePipe];
[theTask launch];
while ((outData=[pipeOutput availableData]) && ([outData length]))
{
theString=[[NSString alloc] initWith
Data:outData
encoding:NSASCIIStringEncoding];
[brsDisplayField setString:[[brsDisplayField stringValue]
stringByAppendingString:theString]];
[theString release];
}
[theTask release];
}
-
(BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)
theApplication
{
return YES;
}
- (void)awakeFromNib
{
[brs makeKeyAndOrderFront:nil];
}
@end