Re: NSTask output and input
Re: NSTask output and input
- Subject: Re: NSTask output and input
- From: Mai Bui <email@hidden>
- Date: Thu, 20 Jan 2005 07:25:56 -0800
Thanks Jerome,
Thanks all of you in advance.
On Jan 19, 2005, at 1:06 AM, Jérôme Laurens wrote:
Le 19 janv. 05, à 07:22, Mai Bui a écrit :
Hi all,
Thanks for your suggestions. Now, my code is following, but it does not work :(
// Append data
- (void)appendData:(NSData *)dt
{
NSRange endRange = NSMakeRange([[outText string] length],0);
NSString *string = [[NSString alloc] initWithData:dt
encoding: NSASCIIStringEncoding];
[outText replaceCharactersInRange:endRange withString:string];
[string release];
}
- (void)outData:(NSNotification *)note
{
NSData *data = [[note userInfo]
valueForKey:NSFileHandleNotificationDataItem];
if (data)
[self appendData:data];
// Must restart reding in background after each notification
[[outPipe fileHandleForReading] readInBackgroundAndNotify];
}
- (id)sender
{
NSData *data;
NSString *aString;
task = [[NSTask alloc] init];
inPipe = [[NSPipe alloc] init];
outPipe = [[NSPipe alloc] init];
// Connect task to pipes
[task setStandardOutput: outPipe];
[task setStandardInput: inPipe];
// [task setLaunchPath:@"/Users/mai/Documents/WorkProjects/CTD/2004/InProccess/CocoaGUI/Test/build/Test"];
[task setLaunchPath:@"~/Test/build/Test"];
[task setArguments:[NSArray arrayWithObject:@"2"]];
// Register to be notified when data comes into either of those pipes
// I use like this, but it does not print out anything
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(outData:)
name:NSFileHandleReadCompletionNotification
object:[outPipe fileHandleForReading]];
/* I want to get input from user while the application is running, but it does not work!!!
[[inPipe fileHandleForReading] writeData: [[inText string] dataUsingEncoding: NSASCIIStringEncoding]];
[[inPipe fileHandleForReading] closeFile];
[[inPipe fileHandleForReading] readInBackgroundAndNotify];
Don't comment this, unless you will never get notified of a change
But it does not do anything when I comment out!
*/
[task launch];
/* With the following this code, it prints out the strings in outPipe textView after the task finishes
I want it print out in real time if the "Test" has a while(1) loop.
data = [[outPipe fileHandleForReading] readDataToEndOfFile];
aString = [[NSString alloc] initWithData: data
encoding: NSASCIIStringEncoding];
[outText setString: aString];
[aString release];
*/
The codes above, the text only print out when the application done, but I want it print out while the the app running. It is not correct code, isn't it?
[task release];
Don't release your task yet! this will cause a stop!
Instead, register for a tesk did terminate and only then you will release your task.
How can I register? Where will I release the task?
[inPipe release];
[outPipe release];
}
inPipe and outPipe are NSTextView, task is NSTask.
Test's code:
for( i=0; i< 100; i++){
printf("Hello, World! %d: %c\n", i,*argv[1]);
}
Is some thing I missing?
Thanks for any idea.
Regards,
Mai.
On Jan 18, 2005, at 3:18 PM, Will Mason wrote:
Here's a link to a set of instructions I gave on this topic not long
ago:
http://www.cocoabuilder.com/archive/message/2004/12/7/123271
You can also access the rest of that thread by way of the above link,
Will
--- Mai Bui <email@hidden> wrote:
Hi all,
I have one application using NSTask to call another my application,
now
I want this application also:
1. Printout on the NSTextView window the display strings from the
other
my app(use printf) in real time not wait for to the end of the called
app. (because my app run in loop)
2. Get input from the user and send to my app because my app need to
get input from user to stop (i.e: "s" character).
How can I do it?
thanks in advance.
Mai.
_______________________________________________
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
_______________________________________________
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
Mai Bui N.
MPL - SIO
email@hidden
(858)534-4733
_______________________________________________
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