phcaze
phcaze

Reputation: 1777

system() call in cocoa app

In my cocoa app I have to call system() function to launch an external app. The command I use is:

system("./main &");

If I run the app from Xcode, it works fine, because I know the folder where to put main.

If I create an archive, and distribute my .app application, system() can't find "main". Where I have to put it?? Or otherwise, how can I run an app using "./" when I'm not in the folder the application is?

EDIT: Maybe I solved using NSTask, but how can I run "main" in background? Now it opens in a new terminal window.

NSTask *task = [[NSTask alloc] init]; 
[task setLaunchPath:@"/Applications/Multibo/main"]; 
[task setArguments:[NSArray arrayWithObjects:[NSString stringWithFormat:@"./main &"], nil]]; 
[task launch];

Thanks

Upvotes: 1

Views: 1891

Answers (2)

bbum
bbum

Reputation: 162722

While dirkgently's answer is directly correct, the real answer is more complex.

First, NSTask is not a generic command line invoker. That is why adding & doesn't do what you expect. In fact, all tasks invoked via NSTask are effectively background.

But you really don't want to use NSTask. You should really be using an XPC service. Now, if your goal is something that runs even after your program exits, you should be looking into LaunchServices.

Upvotes: 1

dirkgently
dirkgently

Reputation: 111250

Try appending the full path before the executable's name and use it as an argument to system. Note that system is implementation defined -- its behavior is not guranteed to be the same across systems and its usage is thus not recommended. You should probably look for a suitable alternative such as NSWorkspace.

Upvotes: 1

Related Questions