比较不同的'启动Cocoa应用程序与参数'方法 [英] Comparing different 'the launching Cocoa app with parameters' methods
问题描述
我发现至少有三种方法可以从应用程式启动使用Mac OS X的应用程式。
I found there are at least three ways to launch an app with Mac OS X from an application.
- NSTask 。我可以给参数,但它似乎不是一个Cocoa应用程序,而是一个UNIX风格的二进制。
- 系统函数(system())就像C一样。我不知道为什么,但似乎没有人推荐这种方法。
- NSWorkspace ,但我找不到一个方法传递参数到这个函数。
- NSTask. I can give parameters, but it seems that it's not for an Cocoa App, but an UNIX style binary.
- system function (system()) just the same way as C does. I don't know the reason why but it seems that nobody recommends this method.
- NSWorkspace, but I can't find a way to pass parameters to this function.
问题
- Q1:
- Q2:每种方法的优点和缺点是什么?
- Q3:启动应用程序(从应用程序)的首选方式是什么?
- 问题4:使用参数(从应用程序)启动应用程序的首选方式是什么?
- 问题5:打开文档(从应用程序)的首选方式是什么?
- Q1 : Is there any other way to launch an App (from an App) other than three methods?
- Q2 : What's the pros and cons for each method?
- Q3 : What's the preferable way for launching an App (from an App)?
- Q4 : What's the preferable way for launching an App with parameters (from an App)?
- Q5 : What's the preferable way to open a document (from an App)?
-
NSWorkspace openFile:withApplication:
:用于运行TextMate README.txt ,根据 Roadmaster的回答和此代码,我可以做如下。
但是,我无法向应用程序提供参数。 NSWorkspace openFile:withApplication:
: For running "TextMate README.txt", based on Roadmaster's answer and this code, I could make it as follows. But, I can't give the parameters to the App.
Questions
NSString * path = @"/Users/smcho/Desktop/README.txt";
NSURL * fileURL = [NSURL fileURLWithPath: path];
NSWorkspace * ws = [NSWorkspace sharedWorkspace];
[ws openFile:[fileURL path] withApplication:@"TextMate"];
-
NSWorkspace launchApplicationAtURL:options:
:它适用于10.6或更高版本,您可以从此问题 a>。 NSWorkspace launchApplicationAtURL:options:
: It works with 10.6 or later, you can get an example from this question.
NSURL * bURL = [[NSWorkspace sharedWorkspace] URLForApplicationWithBundleIdentifier:@"com.macromates.textmate"];
NSWorkspace * ws = [NSWorkspace sharedWorkspace];
[ws launchApplicationAtURL:bURL options:NSWorkspaceLaunchDefault configuration:nil error:nil];
-
NSTask
:这是工作代码。我需要给出正确的二进制路径,它看起来不像Cocoa的方式,因为它是用于运行二进制,而不是捆绑。虽然,可以提供更多的参数,而不只是一个文件名。 NSTask
: This is the working code. I need to give the correct binary path, and it doesn't look like a Cocoa way, as it's for running binary, not bundle. Though, it's possible to give more parameters than just a file name.
[NSTask launchedTaskWithLaunchPath:@"/Applications/TextMate.app/Contents/MacOS/TextMate" arguments:[NSArray arrayWithObjects:@"hello.txt", nil]];
-
system()
我可以运行system(open -a ABC --args hello.txt),就像我用命令行。 看起来这是最简单的方法。 system()
: With the shell, I could run "system(open -a ABC --args hello.txt)", just like I do with the command line. It seems that this is the easiest way to go.
推荐答案
在10.6及更高版本中,NSWorkspace有一个方法 launchApplicationAtURL:options:configuration:error:
,可用于向应用程序传递参数。
In 10.6 and later, NSWorkspace has a method launchApplicationAtURL:options:configuration:error:
that can be used to pass arguments to the app.
还有一些启动服务功能,例如 LSOpenItemsWithRole
。
There are also Launch Services functions such as LSOpenItemsWithRole
.
AppleEvent到Finder要求它打开一些东西。
You could also send an AppleEvent to the Finder asking it to open something.
编辑添加:best是主观的,但我想说如果你可以使用NSWorkspace,使用它。如果你不能,例如,你需要传递命令行参数,你需要支持Leopard,然后使用启动服务。
EDIT TO ADD: "best" is subjective, but I'd say if you can use NSWorkspace, use it. If you can't, e.g., you need to pass command-line parameters and you need to support Leopard, then use Launch Services.
这篇关于比较不同的'启动Cocoa应用程序与参数'方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!