• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Reading data from an NSTask
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Reading data from an NSTask


  • Subject: Re: Reading data from an NSTask
  • From: Mark Ackerman <email@hidden>
  • Date: Mon, 4 Jul 2005 17:49:02 -0400

Perhaps this will work:

NSPipe *pipe = [NSPipe pipe];
NSFileHandle *fileHandle = [pipe fileHandleForReading];
NSData *data = nil;
NSMutableData *taskData = [NSMutableData data];

[testTask setArguments: [NSArray arrayWithObjects: @"-a", @"/Users/ kentozier/Desktop/TestNib.nib", nil]];
[testTask setLaunchPath: @"/usr/bin/nibtool"];
[testTask setStandardOutput: pipe];
[testTask setStandardError: pipe]];
[testTask launch];


while ((data = [fileHandle availableData] && [data length])
{
    // do something with the data
    [taskData appendData:data];
}

[testTask waitUntilExit];

NSString *taskDataString = [[NSString alloc] initWithData: taskData encoding: NSUnicodeStringEncoding];

if ([testTask terminationStatus] == 0)
{
    // success
    NSLog(@"taskData = %@", taskDataString);
}
else
{
    // failure
    NSLog(@"task failed");
    NSLog(@"error = %@", taskDataString);
}

[taskDataString release];

This follows the example code at the following link:

http://developer.apple.com/documentation/Cocoa/Conceptual/ OperatingSystem/index.html#//apple_ref/doc/uid/10000058i


On Jul 4, 2005, at 4:35 PM, Ken Tozier wrote:

<...snip>
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.
<snip...>



I believe this is because the task output is filling up the buffer, so the app just sits there waiting for the buffer to be emptied. I just did something similar with grep on a large file, and it took the incremental reading of the task output to make it work.


Mark

_______________________________________________
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


  • Follow-Ups:
    • Re: Reading data from an NSTask [SOLVED]
      • From: Ken Tozier <email@hidden>
    • Re: Reading data from an NSTask
      • From: Ken Tozier <email@hidden>
  • Prev by Date: Re: Reading data from an NSTask
  • Next by Date: Application launch from plugin
  • Previous by thread: Fwd: Reading data from an NSTask
  • Next by thread: Re: Reading data from an NSTask
  • Index(es):
    • Date
    • Thread