将文件扩展名与Mac的Java JRE7应用程序关联

Plu*_*ter 5 java macos file-io plist

我想通过在Mac上双击来打开与我的应用程序相关的文件。这个问题已经被问过很多次了,例如 Mac OS X中的双击文档文件以打开Java应用程序

但是,自2009年以来(发布解决方案时)似乎发生了更改。该解决方案有效地基于2004年的文章 https://today.java.net/pub/a/today/2004/01/05/swing.html, 并且依赖于两个难题:
1. Java程序应该注册文件打开事件并采取适当的行动。这是使用com.apple.eawt应用程序和应用程序适配器完成的。2.操作系统需要熟悉扩展,这是通过使用软件包.plist中的CFBundleDocumentTypes键完成的。

如几个讨论中所述,2004 com.apple.eawt中使用的构造已被贬值,包括 在Mac上的Java Swing应用程序中使用现已弃用的com.apple.eawt.ApplicationAdapter的替代方案是什么? (2月11日)在那里注意到OpenFilesHandler取代了折旧的构造。答案中提到的指向API文档的链接不再起作用,因此尚不清楚如何使用此构造。我发现以下参考资料解释了折旧的原因,但是该文档的链接也已过时:http : //lists.apple.com/archives/java-dev/2012/Jan/msg00101.html

我找不到在线更新的API的任何文档。Apple Developer网站还引用了我所拥有的版本(5.0.2)中不存在的X代码示例。

在(2012)中的讨论中 ,在从jdk6切换的上下文中提到了/sf/ask/620020201/归因于jdk7-beta,结论是归因于jdk7-beta的一些错误。

最后,也是在2012年,提出了以下问题,即OpenFilesHandler的用法是否相似。

在MacOSX上获取openFileEvent(无法获取文件名)

这是我能够找到的最新帖子,并且我想再次访问那里发布的问题。

该线程有两个答案:第一个建议切换到Java Web Start,这不是我的选择。第二个答案集中在难题的第二部分-使用.plist注册适当的信息。

具体来说,指出了对CFBundleDocumentTypes和UTExportedTypeDeclarations的需求。需要后者来注册自定义文件扩展名。

使用应用程序捆绑程序无法提供将这些键包含在.plist中的灵活性,因此我尝试在通过编辑.plist生成软件包后将它们包含在内。按照原始帖子的建议,我包括了info.plist info.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-  1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleExecutable</key>
<string>JavaAppLauncher</string>
<key>CFBundleIconFile</key>
<string>myIcons.icns</string>
<key>CFBundleIdentifier</key>
<string>mypackage.MainClass</string>
<key>CFBundleDisplayName</key>
<string>MyProgram</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>ProgramName</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>NSHumanReadableCopyright</key>
<string>NOTICES.txt</string>
<key>JVMRuntime</key>
<string>jdk1.7.0_21.jdk</string>
<key>JVMMainClassName</key>
<string>mypackage.MainClass</string>
<key>JVMOptions</key>
<array/>
<key>JVMArguments</key>
<array/>
<key>UTExportedTypeDeclarations</key>
<dict>
            <key>UTTypeIdentifier</key>
    <string>com.myCompany.xxx</string>
    <key>UTTypeReferenceURL</key>
    <string>http://myCompany.com/xxx.html</string>
    <key>UTTypeDescription</key>
    <string>My program file</string>
    <key>UTTypeIconFile</key>
            <array>
    <string>myIcons.icns</string>
            </array>
    <key>UTTypeConformsTo</key>
            <array>
    <string>com.apple.package</string>
             </array>
    <key>UTTypeTagSpecification</key>
    <dict>
        <key>public.filename-extension</key>
        <string>apn</string>
    </dict>
</dict>
<key>CFBundleDocumentTypes</key>
<dict>
    <key>CFBundleTypeName</key>
    <string>y program file</string>
    <key>CFBundleTypeIconFiles</key>
            <array>
    <string>myIcons.icns</string>
             </array>
            <key>CFBundleTypeRole</key>
             <string>Editor</string>
    <key>LSHandlerRank</key>
    <string>Owner</string>
    <key>LSItemContentTypes</key>
    <string>com.myCompany.xxx</string>
</dict>
</dict>
</plist>
Run Code Online (Sandbox Code Playgroud)

这个info.plist的工作原理是:文件确实与该应用程序相关联,我可以双击打开它们。

唯一剩下的问题是文件不显示与文件关联的图标(任何大小)(无论我选择如何查看文件)。应用程序本身显示图标。我使用iconutil从带有png文件的文件夹中生成了完整的图标集,如https://apple.stackexchange.com/questions/59561/where-did-icon-composer-go-from-xcode所述 。有趣的是,在有时这些图标出现了,然后停了下来。如果我右键单击该文件,则我的应用程序将显示一个图像,但是文件本身没有图像。对于如何获得这些图像的任何建议将不胜感激。

该问题似乎类似于 OSX自定义扩展图标关联 。建议使用Jar Bundler,据我所知,它已被AppBundler取代。

我也尝试刷新启动服务-http: //www.tekrevue.com/tip/rebuild-launchservices-fix-duplicate-entries-os-xs-open-menu/ 这没有帮助。

总之, 通过使用OpenFilesHandler成功解决了MacOSX上的openFileEvent(无法获取文件名)中描述的问题 ,由于引入了自定义扩展名,因此需要UTExportedTypeDeclarations。感谢whiskeyspider确认我快要放弃了,所以我确认自己处于正确的轨道上。唯一未解决的问题是与文件关联的实际图像。

Plu*_*ter 2

这个答案重点关注 OpenFilesHandler 的使用,它假设 CFBundleDocumentTypes 和 UTExportedTypeDeclarations 定义包含在 Info.plist 中(请参阅问题的文本)。

\n\n

需要记住的是,在 Mac 和 PC 中打开 Java 应用程序的方法之间存在基本差异。对于 PC,我们依赖于要打开的文件的直接命令行规范。文件名是从命令行传递给 Java 程序的参数。因此,需要编写 Java 程序来接受该参数并进行适当的处​​理。PC 包装器(例如 Inno Setup)提供了一种通过单击文件来使用所需参数调用 Java 程序的方法。

\n\n

相反,对于 Mac,我们可以利用 OpenFilesHandler 来传递有关需要打开的文件的信息。具体用法大致如下:\n在 MacOSX 上抓取 openFileEvent(无法获取文件名)

\n\n

在我的实现中,我有一个 FileSave 类,它有一个 readFile 方法,该方法将 File 类的实例作为参数,并执行读取文件的实际工作。在程序的主要部分中,我有以下相关的代码片段:

\n\n
public final static boolean SYSTEMISMAC=(System.getProperty("os.name")).startsWith("M"); //   treating Mac separately\nstatic protected FileSaving initialFiling;\n........\ninitialFiling = new FileSaving(); // Initializing  \nif(SYSTEMISMAC){\n     Application a = Application.getApplication();\n     a.setOpenFileHandler(new MacFiles(initialFiling) );\n }\n \xe2\x80\xa6\xe2\x80\xa6\xe2\x80\xa6\xe2\x80\xa6\xe2\x80\xa6\n
Run Code Online (Sandbox Code Playgroud)\n\n

这里我们调用实现 OpenFilesHandler 的 MacFiles 类:

\n\n
import java.io.File;\nimport java.util.List;\nimport com.apple.eawt.AppEvent.OpenFilesEvent;\nimport com.apple.eawt.Application;\nimport com.apple.eawt.OpenFilesHandler;\npublic class MacFiles implements OpenFilesHandler{\nprivate List<File> files;\nprivate FileSaving myFiling;\npublic MacFiles(FileSaving filing) {//\n    Application.getApplication().setOpenFileHandler(this);\n    myFiling=filing;\n}\npublic List<File> getFiles() {\n    return files;\n}\n@Override\npublic void openFiles(OpenFilesEvent event) {\n  files = event.getFiles();\n  File myFile=new File(files.get(0).getAbsolutePath());\n  myFiling.readFile(myFile,true);  \n}\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

从某种意义上说,双击文件可以将其打开,但在浏览文件时显示自定义图标的问题(如问题中所述)仍未解决。

\n