gpt4 book ai didi

objective-c - 在 Objective C 中启动命令行

转载 作者:行者123 更新时间:2023-12-03 17:39:04 41 4
gpt4 key购买 nike

我想在 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 ExecutionLaunching 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 函数。事实上,我需要启动另一个应用程序的应用程序等待启动的应用程序退出。谢谢!

最佳答案

我找到了解决方案。事实上,它不是:

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

但是:

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

所以最终的代码是:

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];

关于objective-c - 在 Objective C 中启动命令行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26036768/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com