0

I would like to exec this command line in objective-C in order to launch an app from another :

open -n myApp.app --args arg1 arg2

I managed to do it without arguments with :

    NSFileManager *fileMgr = [NSFileManager defaultManager];
    NSString *myAppPath=[NSString stringWithFormat:@"%@/myApp.app",[fileMgr currentDirectoryPath] ];
    [[NSWorkspace sharedWorkspace] launchApplication:myAppPath];

But I can't figure out how to do it with arguments. Following Cocoa/ Objective-C Shell Command Line Execution and Launching an Mac App with Objective-C/Cocoa , I tried :

    NSFileManager *fileMgr = [NSFileManager defaultManager];
    NSString *path=[NSString stringWithFormat:@"%@/myApp.app",[fileMgr currentDirectoryPath]];
    NSArray *args = [NSArray arrayWithObjects:@"arg1",@"arg2", nil];
    [[NSTask launchedTaskWithLaunchPath:path arguments:args] waitUntilExit];

I get the error :

*** NSTask: Task create for path '/path/to/app/myApp.app' failed: 22, "Invalid argument".  Terminating temporary process.

I precise that I need to launch it with NSTask for the waitUntilExit function. Indeed, I need the app which launches the other app to wait until the launched app exit. Thanks!

Community
  • 1
  • 1
SteveTJS
  • 635
  • 17
  • 32

1 Answers1

2

I found the solution. In fact, it's not :

NSString *path=[NSString stringWithFormat:@"%@/myApp.app",[fileMgr currentDirectoryPath]];

but :

NSString *path=[NSString stringWithFormat:@"%@/myApp.app/Contents/MacOS/myApp",[fileMgr currentDirectoryPath]];

So the final code is :

NSFileManager *fileMgr = [NSFileManager defaultManager];
NSString *path=[NSString stringWithFormat:@"%@/myApp.app/Contents/MacOS/myApp",[fileMgr currentDirectoryPath]];
NSArray *args = [NSArray arrayWithObjects:@"arg1",@"arg2", nil];
[[NSTask launchedTaskWithLaunchPath:path arguments:args] waitUntilExit];
SteveTJS
  • 635
  • 17
  • 32