Re: Save Output of NSTask to file
Re: Save Output of NSTask to file
- Subject: Re: Save Output of NSTask to file
- From: Will Mason <email@hidden>
- Date: Tue, 7 Dec 2004 09:14:33 -0800 (PST)
- Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys
All you do is this. I'm just going to cover the essential steps, not
give you every bit of code. First, connect your task to pipes from
which you will read stdout and stderr:
NSPipe* outPipe = [NSPipe pipe];
NSPipe* errPipe = [NSPipe pipe];
NSTask* task = [[NSTask alloc] init];
[task setStandardOutput: outPipe];
[task setStandardError: errPipe];
Now, you get the file handles from the pipes that are going to feed you
data:
/* out is an instance of NSFileHandle */
out = [outPipe fileHandleForReading];
/* err is an instance of NSFileHandle */
err = [errPipe fileHandleForReading];
Now, you register to be notified when data comes into either of those
pipes:
/* notifier is an instance of NSNotificationCenter */
/* self is the class that defines outDataReceived: and errDataReceived:
*/
[notifier addObserver: self selector: @selector(outDataReceived:) name:
NSFileHandleReadCompletionNotification object: out];
[notifier addObserver: self selector: @selector(errDataReceived:) name:
NSFileHandleReadCompletionNotification object: err];
Make sure that you tell the file handle to notify you:
[out readInBackgroundAndNotify];
[err readInBackgroundAndNotify];
Now, you are ready to launch:
[task launch];
This is a very important part! In the notification methods
outDataReceived: and errDataReceived: you need to send the message to
the file handle readInBackgroundInNotify everytime, otherwise it will
stop notifying you!
What I do at this point is to send a custom notification from the
outDataReceived: or errDataRecieved: messages, so that not every
component of my app has to know about the file handles involved. In my
custom notification I just pass the data that I receive from the file
handles. This way you can register observers for your custom
notification and send the output to a file or a text view or to your
mom or wherever.
That's about it,
Will
--- Bjorn Van Blanckenberg <email@hidden> wrote:
>
> Sorry,
> But I want to append the output of NSTask to a logfile and append it
> also to textview
>
> > How can I save the output of NSTask to a file and show it in
> textview
> --
>
> .·¨)
> ¸.·´¸.·´¨) ¸.·*¨) ¸.·´¨)
> (¸.·´ Thanks .·´ .·´
> (_¸.·* (¸.·* (¸.·*´¯`*·»
>
> Bjorn Van Blanckenberg
>
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Cocoa-dev mailing list (email@hidden)
> Help/Unsubscribe/Update your Subscription:
>
>
> This email sent to email@hidden
>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden