Re: NSTask & NSPipe (followup)
Re: NSTask & NSPipe (followup)
- Subject: Re: NSTask & NSPipe (followup)
- From: Eric Clements <email@hidden>
- Date: Wed, 27 Jun 2001 21:43:32 -0400
That is because the Task has not ended, or did not return enough data...
if you do the following just after you launch, it will wait until the
task exits... Otherwise you need to do notification...
[theTask waitUntilExit]; // Wait until the task finishes
Eric
On Wednesday, June 27, 2001, at 05:58 PM, cocoa-dev-
email@hidden wrote:
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] initWithData: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