【发布时间】:2014-09-25 10:53:10
【问题描述】:
我想在 Objective-C 中执行这个命令行,以便从另一个应用程序启动:
open -n myApp.app --args arg1 arg2
我设法在没有参数的情况下做到了:
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSString *myAppPath=[NSString stringWithFormat:@"%@/myApp.app",[fileMgr currentDirectoryPath] ];
[[NSWorkspace sharedWorkspace] launchApplication:myAppPath];
但我不知道如何使用参数来做到这一点。在Cocoa/ Objective-C Shell Command Line Execution 和Launching an Mac App with Objective-C/Cocoa 之后,我尝试了:
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];
我得到了错误:
*** NSTask: Task create for path '/path/to/app/myApp.app' failed: 22, "Invalid argument". Terminating temporary process.
我确切地说我需要使用 NSTask 为 waitUntilExit 函数启动它。事实上,我需要启动另一个应用程序的应用程序等到启动的应用程序退出。 谢谢!
【问题讨论】:
-
你可以使用 system(Const char *command) API 来实现。做一些研究。
标签: objective-c cocoa command-line command-line-arguments nstask