The problem was:
You need to [myTask setEnvironment:...]
so that the task knows which are the environment variables (PATH
, etc.)
Domanda
I want to do with an NSTask
what I am able to do in the terminal via
$ myprogram myfile.ext
I know that myprogram
(I don't have any control on this program) launches another program myauxprogram
. Furthermore, the path to myprogram
is path1
and the path to myprogram
is path2
.
If I do
NSTask * myTask = [[NSTask alloc] init];
NSArray * arguments = @[@"myfile.ext"] ;
[myTask setCurrentDirectoryPath:[URLOfTheFolder path]];
[myTask setLaunchPath:@"/path1/myprogram"];
[myTask setArguments:arguments];
[myTask launch] ;
I get the following error sh: myauxprogam: command not found
If I create a symbol link in path1
to myauxprogram
, the problem is the same.
Soluzione 2
The problem was:
You need to [myTask setEnvironment:...]
so that the task knows which are the environment variables (PATH
, etc.)
Altri suggerimenti
It looks like you mostly have the idea, you're just missing a few key elements. NSTask
should have a defined LaunchPath, such as /usr/bin/sh
, /usr/bin
, etc. from there depending on the type of path you're requiring you set up your arguments and options.
NSTask *auxTask;
NSTask *auxPath = @"./path1/myprogram"; // period might be needed
NSTask *auxArgs = @"myfile.ext";
auxTask = [[NSTask alloc] init];
[auxTask setLaunchPath:@"/usr/bin/sh"]; // use shell
[auxTask setArguments:[NSArray arrayWithObjects:
@"-c", // -c will exec as shell cmd
auxPath,
auxArgs,
nil]];
// using `try` is optional
@try
{
[auxTask launch];
}
@catch(id exc)
{
auxTaskSuccess = NO;
}
[auxTask release];
return;
No idea if this works, but this is the way you would likely set things up. If you run into issues with errors or the NSTask
failing you can set up an NSLog
with the auxTask NSArray
to see exactly what it's doing.