Re: NSTask output and input
Re: NSTask output and input
- Subject: Re: NSTask output and input
- From: Mai Bui <email@hidden>
- Date: Tue, 18 Jan 2005 22:22:08 -0800
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];
*/
[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];
*/
[task release];
[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