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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…