Reading return values from NSTask
Reading return values from NSTask
- Subject: Reading return values from NSTask
- From: KLW <email@hidden>
- Date: Sun, 28 Dec 2003 17:40:42 -0500
Hello,
I'm trying to execute a perl script from within my Cocoa application
and get its return value (not its exit status). I've tried two
approaches: 1) using a c system() call (which, I believe will only
net me an exit status), or 2) using NSTask, like so:
<ugly code>
NSString * argument = [[NSString alloc] initWithCString: argv[1]];
NSTask *task = [[NSTask alloc] init];
NSFileHandle *readHandle = [[NSFileHandle alloc] init];
NSData *inData = [[NSData alloc] init];
NSMutableString *outString = [[NSMutableString alloc] init];
NSMutableArray *args = [NSMutableArray array];
NSLog(@"Passing: %@",argument);
[args addObject:argument];
[task setStandardOutput:readHandle];
[task setLaunchPath:@"/path/to/my/perl/script.pl"];
[task setArguments:args];
[task launch];
</ugly code>
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. My attempt is here:
<really ugly code>
if ((inData = [readHandle availableData]) && [inData length]) {
outString = [outString initWith
Data:inData
encoding:NSASCIIStringEncoding];
// outString = [inData description];
}
[task terminate];
NSString *result = [[NSString alloc] initWithFormat: @"result:
%@",outString];
NSLog(result);
</really ugly code>
Obviously, this attempt does not work. I just can't see any examples
(having looked at
I've looked at the following threads at mamasam:
http://cocoa.mamasam.com/MACOSXDEV/2003/03/1/58883.php
http://cocoa.mamasam.com/MACOSXDEV/2002/11/2/50558.php
http://cocoa.mamasam.com/COCOADEV/2003/11/2/77558.php
and the following Apple documentation:
http://developer.apple.com/documentation/Cocoa/Reference/Foundation/ObjC_classic/Classes/NSTask.html#//apple_ref/doc/uid/20000317/BJFBJHGF
http://developer.apple.com/documentation/Cocoa/Conceptual/OperatingSystem/index.html
as well as my two books on Cocoa (Hillegass and the O'Reilly Cocoa
with objective C. I can't believe there aren't any real life examples
of getting a string return value when executing a system call. In
perl, or the shell, or php etc., I would just go: variable =
system("put command here"); simple enough.
Again, the task launches fine, and runs correctly, I just can't
figure out how to get the string the Perl script prints out into a
variable for use in Cocoa. The Perl script returns (depending on
params passed to it, one of the following strings to STDOUT):
isInitialized, isRestored, isError.
Thank you for any input, er, output, whatever.
Also, if anyone has any hints on how to launch this task using sudo
(or any other authorization scheme) that would be great, since
ultimately that is what I'll need to do.
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.