More on NSTask and output to NSTextView
More on NSTask and output to NSTextView
- Subject: More on NSTask and output to NSTextView
- From: Koen van der Drift <email@hidden>
- Date: Sat, 5 Jun 2004 07:47:05 -0400
Hi,
Thanks to all the good suggestions on the list I am now using an NSTask
to launch a commandline program in a GUI. It all works very nice except
displaying the output from the program in an NSTextView is displayed in
chunks. After searching the archives it seems that this is caused by
the NSTask - NSPipe - NSFileHandle combination that does some
buffering. This has been discussed before and the general suggestion to
prevent this is to use readInBackgroundAndNotify on the NSFileHandle.
Here is a snippet:
.....
[startButton setTitle: @"Running"];
[startButton setEnabled:NO];
pipe = [[NSPipe alloc] init];
myTask = [[NSTask alloc] init];
[myTask setLaunchPath:[[NSBundle mainBundle]
pathForResource:@"atask" ofType:nil]];
[myTask setArguments:args];
[myTask setStandardOutput:pipe];
handle = [pipe fileHandleForReading];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(getData:)
name:NSFileHandleReadCompletionNotification
object:handle];
[handle readInBackgroundAndNotify];
[myTask launch];
[pipe release];
.....
and:
- (void)getData:(NSNotification *)aNotification
{
NSData *inData = [[aNotification userInfo]
objectForKey:NSFileHandleNotificationDataItem];
if ([inData length])
{
NSString *string=[[NSString alloc] initWith
Data:inData
encoding:NSASCIIStringEncoding];
[resultsTextView replaceCharactersInRange:
NSMakeRange([[resultsTextView string] length], 0)
withString:string];
[resultsTextView scrollRangeToVisible:
NSMakeRange([[resultsTextView string] length], 0)];
[string release];
}
else // we're done
{
[startButton setTitle: @"Start"];
[startButton setEnabled:YES];
[myTask release];
myTask = nil;
}
}
However, the data is still displayed in chunks. It must be possible,
because when I don't send the output to the NSPipe, the ouput goes to
the console window of XCode and there the output scrolls nicely over
the screen without any hickups.
I also tried Moriarity and the AMTextShellWrapper, but the results are
still the same.
Any suggestions how to improve this?
thanks,
- Koen.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.