我有一个应用程序:TexturePacker。 如果我单击应用程序文件夹中的应用程序图标,它将启动 gui。 如果我在终端中输入“texturepacker”,它将运行命令行版本。
我想以编程方式启动命令行版本!当我使用下面的代码时,它会启动 GUI。我应该使用什么 shell 命令,以便应用程序(命令行版本)启动,就像我在终端中输入“texture packer”一样。
NSTask *theProcess = [[NSTask alloc] init];
[theProcess setLaunchPath:@"/usr/bin/open"];
[theProcess setArguments:[NSArray arrayWithObjects:
@"-a",
@"/Applications/TexturePacker.app",
nil]];
// Arguments to the command: the name of the
// Applications directory
[theProcess launch];
// Run the command
[theProcess release];
如果这是一个菜鸟问题。我道歉。我很傻。 :S
编辑:弄清楚其中的一部分。我需要指定应用程序内二进制文件的路径才能启动它。但是我如何传递参数呢?如果我向数组添加更多参数,shell 会假定它是“open”命令的参数。如果我将其添加到带有纹理打包程序路径的字符串中,shell 会说找不到该应用程序。 :S
最佳答案
要启动可执行程序,无需使用 open
。您可以将 NSTask 启动路径设置为您的texturepacker二进制文件,然后您可以将Arguments设置为包含texturepacker参数的数组:
NSTask *theProcess = [[NSTask alloc] init];
[theProcess setLaunchPath:@"/path/to/texturepacker"];
// Set arguments for invoking texturepacker
[theProcess setArguments:[NSArray arrayWithObjects:
@"-x",
@"-y",
@"-z",
nil]];
// Run the task
[theProcess launch];
[theProcess release];
关于objective-c - 如何以编程方式从命令行启动应用程序的命令行版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9823395/