• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
More on NSTask and output to NSTextView
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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] initWithData: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.


  • Follow-Ups:
    • Re: More on NSTask and output to NSTextView
      • From: "Louis C. Sacha" <email@hidden>
    • Re: More on NSTask and output to NSTextView
      • From: p3consulting <email@hidden>
  • Prev by Date: Re: Representing UTC time in a readable format
  • Next by Date: [Newbie] Who's changing my radius?
  • Previous by thread: Re: Drawing a view and its subviews to an NSImage
  • Next by thread: Re: More on NSTask and output to NSTextView
  • Index(es):
    • Date
    • Thread