• 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 return values from NSTask
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Reading return values from NSTask


  • Subject: Re: Reading return values from NSTask
  • From: KLW <email@hidden>
  • Date: Tue, 30 Dec 2003 00:16:45 -0500

On Dec 28, 2003, at 2:40 PM, KLW wrote:
...
So far so good. The task launches correctly, and the console gets the correct output from the perl script. However, when I try to read the output to Cocoa, I run into trouble. I can't figure out (having looked at the NSTask page about thirty times) how to read ordinary output back into a variable.


Do you wait for the task to complete before trying to read in data? You code sample doesn't show that.

I do notice some strange things in your code snippet above... You allocate and init inData and outString at the beginning yet replace them later on in you code without releasing what you inited. You appear to believe that you have to allocate those before that can be assigned to... you don't. I suggest reading up on Objective-C a little more to better understand the difference between objects and references to objects. Also it is cleaner to use NSArray's arrayWithObject or arrayWithObjects then creating a NSMutableArray to later simply add a single item to it that you had on hand when you created the array in the first place. Finally you need not call terminate on the task, it will exist when it is done running (assuming the script you are running exists).

Anyway this is one example of how I use NSTask in a project of mine...

- (void)initSwapFileRootDirectory
{
NSData* data;

NSTask* ls = [[[NSTask alloc] init] autorelease];
[ls setStandardOutput:[NSPipe pipe]];
[ls setLaunchPath:@"/bin/sh"];
[ls setArguments:[NSArray arrayWithObjects:...]];
[ls launch];
[ls waitUntilExit];

data = [[[ls standardOutput] fileHandleForReading] availableData];
if ((data != nil) && [data length]) {
NSString* tmpString = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
...
}
}

Shawn,

Thanks for your help, as well as for pointing out some of the memory allocation and initialization flaws in my code. I have assigned myself the task on rereading those sections in my two books. I still feel quite lost on that subject.

Your code sample helped be solve the problem, and so I thought I'd share my solution with you and the list, for people searching the archives in the future. Here's what I did:
char returnValue;
NSMutableArray *args = [NSMutableArray array];
NSString * result;
NSData* data;
NSTask* perlControl = [[[NSTask alloc] init] autorelease];
[perlControl setStandardOutput:[NSPipe pipe]];
[perlControl setLaunchPath:pathToXJcc];
switch (cmd) {
case CC_STATUS_CHECK : [args addObject:[NSString stringWithFormat:@"%c",CC_STATUS_CHECK]];
break;
case CC_INITIALIZE_CRON : [args addObject:[NSString stringWithFormat:@"%c",CC_INITIALIZE_CRON]];
[args addObject:pathToXJscript];
[args addObject:@"true"];
break;
case CC_RESTORE_CRON : [args addObject:[NSString stringWithFormat:@"%c",CC_RESTORE_CRON]];
break;
}
[perlControl setArguments:args];
[perlControl launch];
[perlControl waitUntilExit];
int exitStatus = [perlControl terminationStatus];//cronControl.pl defines 0 as successful exit

if (exitStatus != CC_SUCCESS_VALUE) {//If not succesful, post error
NSLog(@"Unable to work with your crontab. Please see documentation for details.");
returnValue = CC_ERROR;
} else {
data = [[[perlControl standardOutput] fileHandleForReading] availableData];
if ((data != nil) && [data length]) {
result = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
}
[data release];
if ([result isEqualToString:@"I"]) {
returnValue = CC_IS_INITIALIZED;
} else if ([result isEqualToString:@"N"]) {
returnValue = CC_IS_NOT_INITIALIZED;
} else if ([result isEqualToString:@"R"]) {
returnValue = CC_IS_RESTORED;
} else {
NSLog(@"An unknown error has occurred in processing the cron.");
returnValue = CC_ERROR;
}
}
NSLog(@"returning: %c",returnValue);
return returnValue;


I'm getting a memory-related crash when I exit my preference pane, but I'm sure I'll figure that out with a little more study. The central problem of getting the return value from the perl script has been solved and everything works great. Again, thanks for your help.

Kristofer
--
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: Reading return values from NSTask
      • From: Shawn Erickson <email@hidden>
References: 
 >genstrings not working (From: David Kocher <email@hidden>)
 >Re: genstrings not working (From: "Louis C. Sacha" <email@hidden>)
 >Reading return values from NSTask (From: KLW <email@hidden>)
 >Re: Reading return values from NSTask (From: Shawn Erickson <email@hidden>)

  • Prev by Date: Re: Making Localizable Help Folder with Xcode
  • Next by Date: Re: Making Localizable Help Folder with Xcode
  • Previous by thread: Re: Reading return values from NSTask
  • Next by thread: Re: Reading return values from NSTask
  • Index(es):
    • Date
    • Thread