双击Mac OS X中的文档文件以打开Java应用程序 [英] Double click document file in Mac OS X to open Java application

查看:157
本文介绍了双击Mac OS X中的文档文件以打开Java应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

例如,如果有文件

foo.example

foo.example

当该文件或带有.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.

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

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

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

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

推荐答案

以下是需要做的事情:

第一部分:

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

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).

此示例向您显示要添加到的内容您的Info.plist文件(请注意,虽然该示例适用于iOS,但它在OS X上的工作方式完全相同)。我不会复制它的内容,但简而言之,你必须添加两个键:

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 :让OS X知道应用程序可以打开的文档类型

  • UTExportedTypeDeclarations :告诉操作系统X关于特定于此应用的自定义文档类型,在本例中为 .example files

  • 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

请注意,有许多键,例如 CFBundleTypeExtensions 与上面的键完全相同,但是自OS 10.5以来它们已被弃用,所以你如果Apple完全删除它们,我们不想使用它们。

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.

如果你添加所有这些并且文件类型关联似乎不起作用,你可以尝试使用lsregister 调试问题,这是一个终端工具,可以让您知道任何问题。如果它没有错误,那么应该设置所有内容。

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.

第二部分:

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

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.

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

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.

这篇关于双击Mac OS X中的文档文件以打开Java应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆