Trying to run NSTask but getting an error

2k Views Asked by At

I have a standalone (3rd party) app that I'm trying to launch using a command "kick". I've setup my ~/.bash_profile and /etc/bashrc files so that I can type kick [command] into a terminal window and it works perfectly. So I'm assuming that I have everything setup correctly.

The problem comes when I try and use NSTask.

Essentially what I'm doing is creating two mutable arrays, kickBuild and kickOut. One to assemble the command (it's a string of flags) and one to use with NSTask. I take kickBuild and convert it to a string separated by spaces, then add that as an object into the second array.

So my command should look something like "kick -i /path/to/input/file.ext -as 2 -g 2.2" etc. And if I type this into a terminal window it works great.

kickBuild = [[NSMutableArray alloc] initWithCapacity:100];
kickOut = [[NSMutableArray alloc] initWithCapacity:2]; // Thinking that this should be "kick" and then "-i /path/to/input/file.ext -as 2 -g 2.2"
kickPath = [kickLocationTextField stringValue]; // This is just the path to my kick executable. NOT /bin/sh. Is that correct?
NSString *tempKick = [kickBuild componentsJoinedByString: @" "];
[kickOut addObject:tempKick];
[NSTask launchedTaskWithLaunchPath:kickPath arguments:kickOut];

When I build my app and run this code, I get this error…

dyld: Library not loaded: build/darwin_x86_64/gcc_opt_dynamic/core/libai.dylib

Referenced from: /Users/Gene/Kick/Kick-3.3.4.0/bin/kick

Reason: image not found

This is an NSLog of kickOut… kick -i /Users/Gene/Test_Main.0001.ext -r 960 540 -as 2 -g 2.2 -v 5 -dp

Is this something that I'm doing wrong? Or is this an issue with kick?

How would I test NSTask with some basic terminal task to make sure that I'm using NSTask correctly?

kickBuild = [[NSMutableArray alloc] initWithCapacity:5];
kickOut = [[NSMutableArray alloc] initWithCapacity:2];
kickPath = @"/bin/sh";
[kickBuild addObject:@"-c"]; // Do I need this?
[kickBuild addObject:@"ls"];
[kickBuild addObject:@"~/Desktop"];
NSString *tempKick = [kickBuild componentsJoinedByString: @" "];
[kickOut addObject:tempKick];
[NSTask launchedTaskWithLaunchPath:kickPath arguments:kickOut];

If I run this without the @"-c", I get: /bin/sh: ls ~/Desktop: No such file or directory

Any help is appreciated.

Thanks a million

2

There are 2 best solutions below

0
On BEST ANSWER

Here's a simple NSTask test that should work for you (note: arguments:(NSArray *)arguments):

/*

gcc -Wall -O3 -x objective-c -fobjc-exceptions -framework Foundation -o nstask nstask.m

./nstask

http://stackoverflow.com/questions/5081846/trying-to-run-nstask-but-getting-an-error


launchedTaskWithLaunchPath:arguments:

Creates and launches a task with a specified executable and arguments.

+ (NSTask *)launchedTaskWithLaunchPath:(NSString *)path arguments:(NSArray *)arguments


*/

#import <Foundation/Foundation.h>

int main(int argc, const char *argv[])
{

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

NSTask *task = [NSTask new];

NSMutableArray *kickBuild = [[NSMutableArray alloc] initWithCapacity:5];
//NSMutableArray *kickOut = [[NSMutableArray alloc] initWithCapacity:2];
NSString *kickPath = @"/bin/ls";

//[kickBuild addObject:@"/bin/ls"]; // Do I need this?
[kickBuild addObject: [@"~/Desktop" stringByExpandingTildeInPath]];

task = [NSTask launchedTaskWithLaunchPath: kickPath arguments: kickBuild];
[task waitUntilExit];


[pool release];

return 0;

}
2
On

Your environment settings are not read when executing NSTask.

I asked this question a couple of years ago.