在 Mac OS X 中双击文档文件以打开 Java 应用程序

发布于 2024-08-07 20:28:48 字数 240 浏览 7 评论 0原文

我的应用程序包中有一个 Java 应用程序,我想将其与文件类型关联。

例如,如果有一个文件

foo.example

,当双击该文件或任何具有 .example 扩展名的文件时,我希望我的应用程序启动并打开该文件。我还希望这些文件具有我的应用程序的图标。

我想通过编辑 info.plist 文件来做到这一点,但它似乎不起作用。

另外,我的 Java 应用程序如何知道哪个文件传递给它?

I have a Java application in an application bundle that I want to associate a file type with.

For example, if there's a file

foo.example

when that file, or any file with the .example extension, is double-clicked, I want my application to start and open the file. I also want the files to have my application's icon.

I'd like to do this by editing the info.plist file, but it doesn't seem to be working.

Also, how does my Java application know which file is passed to it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

横笛休吹塞上声 2024-08-14 20:28:48

以下是需要完成的操作:

第一部分

首先,您必须进行设置,以便 OS X 知道 .example 扩展名应与您的应用程序关联。这是通过应用程序的 Info.plist 文件完成的,前提是您已经将 Java 代码捆绑到 .app 包中(有关如何执行此操作的信息,请参阅其他问题)。

此示例向您准确展示了要添加到 Info.plist 文件中的内容(请注意,虽然该示例适用于 iOS,但它可以工作在 OS X 上完全相同)。我不会重复它所说的内容,但简而言之,您必须添加两个键:

  • CFBundleDocumentTypes:让 OS X 知道应用程序可以打开的文档类型
  • UTExportedTypeDeclarations code>:告诉 OS X 有关此应用程序特定的自定义文档类型的信息,在本例中为 .example 文件

请注意,有许多键(例如 CFBundleTypeExtensions)可以执行此操作与上面的键大致相同,但自 OS 10.5 起它们已被弃用,因此您不想使用它们,以防 Apple 完全删除它们。

如果您添加了所有这些,但文件类型关联似乎不起作用,您可以尝试使用 lsregister 调试问题,一个终端工具,可以让您知道任何问题。如果返回没有错误,那么一切都应该设置完毕。

第二部分:

现在,当您双击以 .example 结尾的文件时,OS X 将打开您的应用程序,您必须让您的 Java 应用程序知道如何处理正在打开的文件。

您的应用将收到 com.apple.eawt.AppEvent.OpenFilesEvent 类型的事件,您需要处理该事件。您可能会问自己如何处理在 Java 应用程序启动之前触发的事件,但 Java 似乎首先执行应用程序主方法中的所有内容,然后触发该事件。因此,在同一线程的 main 方法中的某个位置,使用以下代码创建侦听器:

//First, check for if we are on OS X so that it doesn't execute on 
//other platforms. Note that we are using contains() because it was 
//called Mac OS X before 10.8 and simply OS X afterwards
if (System.getProperty("os.name").contains("OS X")){
    Application a = Application.getApplication();
    a.setOpenFileHandler(new OpenFilesHandler() {

        @Override
        public void openFiles(OpenFilesEvent e) {
            for (File file : e.getFiles()){
                //Handle your file however you'd like
            }
        }

    });
}

执行此操作后,您的应用程序将处理在应用程序启动之前和应用程序启动之后打开的文档文件。

Here is what needs to be done:

Part One:

First you have to set things up so that OS X knows that the .example extension should be associated with your app. This is done with the Info.plist file of your app, provided that you've already bundled your Java code into a .app package (see other questions for how to do that).

This example shows you exactly what to add to your Info.plist file (note that although the example is for iOS, it works exactly the same on OS X). I won't duplicate what it says, but in short, you have to add two keys:

  • CFBundleDocumentTypes: Lets OS X know the type of documents that can be opened by the app
  • UTExportedTypeDeclarations: Tells OS X about custom document type specific to this app, which in this case is .example files

Note that there are a number of keys such as CFBundleTypeExtensions that do much the same thing as the keys above, but they have been deprecated since OS 10.5, so you don't want to be using them in case Apple removes them completely.

If you add all that and the file type association doesn't seem to be working, you can try to debug the problem using lsregister, a Terminal tool that will let you know of any issues. If it comes back with no errors, then everything should be set up.

Part Two:

Now that OS X will open up your app when you double-click on a file ending with .example, you have to let your Java app know how to handle the file being opened.

Your app will be receiving an event of type com.apple.eawt.AppEvent.OpenFilesEvent, which you will need to handle. You might be asking yourself how you handle an event triggered before the Java app even starts, but it seems that Java first executes everything in the app's main method and then fires the event. So somewhere in the main method on the same thread, create the listener with the following code:

//First, check for if we are on OS X so that it doesn't execute on 
//other platforms. Note that we are using contains() because it was 
//called Mac OS X before 10.8 and simply OS X afterwards
if (System.getProperty("os.name").contains("OS X")){
    Application a = Application.getApplication();
    a.setOpenFileHandler(new OpenFilesHandler() {

        @Override
        public void openFiles(OpenFilesEvent e) {
            for (File file : e.getFiles()){
                //Handle your file however you'd like
            }
        }

    });
}

After you do this, your app will handle both document files that were opened before your app was launched and after your app was launched.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文