如何在不先启动的情况下将文件类型与 MacOS X App 关联?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/435175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to associate file type with MacOS X App without launching it first?
提问by Carl Seleborg
I would like to associate the last installedversion of my Mac OS X application with a certain type of file. A little experimentation shows that the info.plistfile seems to be read and interpreted by the operating system when launching the application, not when dragging the application bundle to the disk. Apple's documentation on run-time configurationdoes not say much on that topic.
我想将我的 Mac OS X 应用程序的最后安装版本与某种类型的文件相关联。一些实验表明info.plist文件似乎在启动应用程序时被操作系统读取和解释,而不是在将应用程序包拖到磁盘时。Apple 的关于运行时配置的文档并没有对这个主题进行太多说明。
Is there any way, using a simple .dmg
image as installation medium, to make sure the system associates that particular file type with my newly installed app without having to launch itfirst?
有什么方法可以使用简单的.dmg
图像作为安装介质来确保系统将该特定文件类型与我新安装的应用程序相关联,而无需先启动它?
I should clarify that I want this to be done automatically during the installation (I don't want my users to have to do it themselves).
我应该澄清一下,我希望在安装过程中自动完成(我不希望我的用户必须自己完成)。
采纳答案by Tony
When a user drags an application into the Applications folder, the system should register it with Launch Services automatically. See Application Registrationin the Launch Services Guide.
当用户将应用程序拖入 Applications 文件夹时,系统应自动将其注册到 Launch Services。请参阅启动服务指南中的应用程序注册。
回答by tresf
Although Tony's information is correct -- also explained here-- (that dragging the MyApp.app
to Applications
will register with Launch Services automatically using information from Info.plist
xml file and setup file associations), it doesn't fully answer the question about file association.
尽管 Tony 的信息是正确的——这里也有解释——(拖动MyApp.app
toApplications
将使用来自Info.plist
xml 文件和设置文件关联的信息自动向 Launch Services 注册),但它并没有完全回答有关文件关联的问题。
If people follow these instructions, they'll have their application open, but no specific file opened.
如果人们遵循这些说明,他们将打开他们的应用程序,但不会打开特定文件。
[...] make sure the system associates that particular file typewith my newly installed app
[...] 确保系统将该特定文件类型与我新安装的应用程序相关联
File association is a bit different on Mac as when compared to other platforms. Most platforms, when *.foo
is registered with myapp
, when MyFile.foo
is double-clicked, it dispatches something along the lines of:
与其他平台相比,Mac 上的文件关联略有不同。大多数平台,当*.foo
注册时myapp
,MyFile.foo
双击时,它会按照以下方式调度一些东西:
/path/to/myapp MyFile.foo
And although you're free to use this technique on command line on a Mac with success, it simply won't work through Finderand also won't work through double-clicking the associated file on the desktop.
尽管您可以在 Mac 上的命令行上成功地自由使用此技术,但它根本无法通过 Finder 工作,也无法通过双击桌面上的关联文件来工作。
Asked and answered here:
在这里提问和回答:
https://stackoverflow.com/a/19702342/3196753
https://stackoverflow.com/a/19702342/3196753
Some claim is this approach is advantageous over conservative document-open-file-handling in main()
because it "only opens one instance of the application". Regardless of the reasoning behind this decision, it further complicates matters from a C++ perspective.
有人声称这种方法优于保守的文档打开文件处理,main()
因为它“仅打开应用程序的一个实例”。不管这个决定背后的原因是什么,从 C++ 的角度来看,它使问题进一步复杂化。
- The 108 page PDF available here(warning, this is marked as "legacy", linkrot may occur).
- Relevant documentation from apple's developer portal available here.
- Qt's approach is documented here.
From qt.io:
来自 qt.io:
When a user double clicks on a file in the Finder, Finder sends an Apple event to the application associated with the file and asks it to open the file. If the application is not running, it is launched and then the request is made. The advantage of this approach is that there is only one instance of the application running.
On Windows, this usually is done by launching the application and passing the name of the file as a command line argument. Looking at many files results in launching many instances of the same application. (The QtSingleApplication component, available as a Qt Solution, addresses this issue.)
Qt 3 does not provide an abstraction for handling Apple events, but it is straightforward to add support for them in your application using Mac's Carbon API. Let's assume we have an application called XpmViewer that displays XPM files (an X11 image format). The main window class declaration follows: [code snippet removed]
当用户双击 Finder 中的文件时,Finder 会向与该文件关联的应用程序发送一个 Apple 事件并要求它打开该文件。如果应用程序未运行,则启动它,然后发出请求。这种方法的优点是只有一个应用程序实例在运行。
在 Windows 上,这通常是通过启动应用程序并将文件名作为命令行参数传递来完成的。查看多个文件会导致启动同一应用程序的多个实例。(作为 Qt 解决方案提供的 QtSingleApplication 组件解决了这个问题。)
Qt 3 没有提供处理 Apple 事件的抽象,但使用 Mac 的 Carbon API 在应用程序中添加对它们的支持很简单。假设我们有一个名为 XpmViewer 的应用程序,它显示 XPM 文件(X11 图像格式)。主窗口类声明如下:[代码片段已删除]
(BOOL)application:(NSApplication *)theApplication
openFile:(NSString *)filename
Handling Mac OS X file open event BEFORE C++ main() executesAEInstallEventHandler handler not being called on startup
在 C++ main() 执行AEInstallEventHandler 处理程序之前处理 Mac OS X 文件打开事件未在启动时调用
回答by Keltia
You can do that by typing apple-I on the file you want to associate the application with, use the little box called "Open with..." and select your app. You can check the little box below "Change All..." to make the change for all similar files.
您可以通过在要关联应用程序的文件上键入 apple-I 来实现,使用名为“打开方式...”的小框并选择您的应用程序。您可以选中“全部更改...”下方的小框以对所有类似文件进行更改。