Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
420 views
in Technique[技术] by (71.8m points)

macos - Double click document file in Mac OS X to open Java application

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...