Re: NSTask and Notification?
Re: NSTask and Notification?
- Subject: Re: NSTask and Notification?
- From: "Sailesh Agrawal" <email@hidden>
- Date: Tue, 21 Oct 2003 16:40:56 -0400
One thing I don't understand, do you want the output all at once when the
process terminates or continuously ?? In the code below, getOutput gets
called each time the task flushes stdout. If you're only interested in
the final output then listen for the NSTaskDidTerminateNotification
notification instead of NSFileHandleReadCompletionNotification. Then
just do [[theTask standardOutput] readDataToEndOfFile].
Good luck !
Sailesh
On Tue, 21 Oct 2003 21:33:16 +0200, "David Dauer" <email@hidden>
said:
>
Hello,
>
>
I'm creating an NSTask:
>
>
- (void)blah
>
{
>
theTask = [[NSTask alloc] init];
>
NSMutableArray *theArguments = [NSMutableArray array];
>
>
[theArguments addObject:@"-123"];
>
>
>
[theTask setStandardOutput:[NSPipe pipe]];
>
[theTask setArguments:theArguments];
>
>
[[NSNotificationCenter defaultCenter] addObserver:self
>
selector:@selector(getOutput:) name:
>
NSFileHandleReadCompletionNotification
>
object: [[theTask standardOutput] fileHandleForReading]];
>
>
[[[theTask standardOutput] fileHandleForReading]
>
readInBackgroundAndNotify];
>
>
[theTask setLaunchPath:path];
>
>
[theTask launch];
>
}
>
>
- (void)getOutput:(NSNotification *)aNotification
>
{
>
NSFileHandle *fh = (NSFileHandle *)[aNotification object];
>
NSData *data = [[aNotification userInfo]
>
objectForKey:NSFileHandleNotificationDataItem];
>
NSString *theOutput;
>
>
if ([data length]) {
>
>
theOutput = [[NSString alloc] initWithData:data
>
encoding:NSISOLatin1StringEncoding];
>
>
if (fh == [[theTask standardOutput] fileHandleForReading]) {
>
>
NSRunAlertPanel(@"alert", theOutput , @"OK", nil, nil);
>
} else {
>
return;
>
}
>
[theOutput release];
>
>
[[aNotification object] readInBackgroundAndNotify];
>
}
>
}
>
>
>
Seems to work fine, but i get the alert message 5 times whatever i'm
>
doing.
>
>
Anyone knows what I'm doing wrong?
>
>
Thanks
>
David
>
_______________________________________________
>
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.
_______________________________________________
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.