Fwd: Reading data from an NSTask
Fwd: Reading data from an NSTask
- Subject: Fwd: Reading data from an NSTask
- From: Ken Tozier <email@hidden>
- Date: Mon, 4 Jul 2005 16:35:49 -0400
On Jul 4, 2005, at 3:01 PM, j o a r wrote:
On 4 jul 2005, at 19.47, Ken Tozier wrote:
The problem is probably that you don't empty the buffer in the
queue between the processes.
Not sure I follow. Isn't the newly allocated pipe empty, and
filled up by the launchedTaskWithLaunchPath?
Yes, it will fill up - but it has a max size. You can find a
reference to that in the conceptual documentation of NSTask.
I took a look which resulted in the following rewrite:
Unfortunately you also missed the important point of the sample
code. What you want is to register for the file handle
notifications, so that you can empty the queue piece by piece,
asynchronously, as the data arrives.
I just re-read the NSTask documentation and I still don't get the
impression that NSTasks can only be run asynchronously. One of it's
methods "waitUntilExit" states:
"Suspends your program until the receiver is finished. This method
first checks to see if the receiver is still running using isRunning.
Then it polls the current run loop using NSDefaultRunLoopMode until
the task completes."
Which I'm interpreting as a synchronous way to use NSTasks. Correct?
With that in mind, I rewrote the code like this:
[testTask setArguments: [NSArray arrayWithObjects: @"-a", @"/Users/
kentozier/Desktop/TestNib.nib", nil]];
[testTask setLaunchPath: @"/usr/bin/nibtool"];
[testTask setStandardOutput: [NSPipe pipe]];
[testTask setStandardError: [NSPipe pipe]];
[testTask launch];
[testTask waitUntilExit];
if ([testTask terminationStatus] == 0)
{
// success
NSData *taskData = [[[testTask
standardOutput] fileHandleForReading] readDataToEndOfFile];
NSLog(@"taskData = %@", taskData);
NSString *taskDataString = [[NSString alloc]
initWithData: taskData encoding: NSUnicodeStringEncoding];
NSLog(@"taskDataString = %@", taskDataString);
}
else
{
NSLog(@"task failed");
NSLog(@"error = %@", [[NSString alloc] initWithData: [[[testTask
standardError] fileHandleForReading] readDataToEndOfFile]
encoding: NSUTF8StringEncoding]);
}
If I intentionally introduce an error (like mangling the path name),
the "task failed" message prints to the run log along with the more
helpful message "error = nibtool: could not open '/User/kentozier/
Desktop/TestNib.nib'." from standardError.
If the path is valid though the program stops cold at "[testTask
waitUntilExit];" and requires a force quit.
Backing up a bit and using the terminal to accomplish the same thing,
it works perfectly
> nibtool -a "/Users/kentozier/Desktop/TestNib.nib"
/* Objects */
Objects = {
"Object 1" = {
Class = "NSCustomObject";
CustomClass = "NSObject";
Name = "File's Owner";
className = "NSObject";
};
... etc.
I may have to go the asynchronous route, but it seems like I'm just
making a minor error here. It should work. Any glaring errors jump
out at anyone?
Ken
_______________________________________________
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