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?
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 appUTExportedTypeDeclarations
: Tells OS X about custom document type specific to this app, which in this case is .example
filesNote 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With