5

I have created a Mac Java Swing application, and i have set a file extension(*.pkkt) for it in the "Info.plist" file, so when double clicking that file it opens my application.

When i do that the program runs fine. Now i need to load the (*.pkkt) project in the program, but the file path is not passed as an argument to the main(...) method in Mac as happens in Windows Operating System.

After some search i found an Apple handling jar "MRJToolkitStubs" that has the MRJOpenDocumentHandler interface to handle such clicked files. I have tried using it to load that file by implementing that Interface in the main program class, but it is not working. The implemented method is never called at the program start-up.

How does this Interface run ?

------------------------------------------------- Edit: Add a Code Sample

Here is the code i am using :

public static void main( final String[] args ) {         
    .                   
    .         
    .       
        MacOpenHandler macOpenHandler = new MacOpenHandler();        
        String projectFilePath = macOpenHandler.getProjectFilePath();  // Always Empty !!           
    }
class MacOpenHandler implements MRJOpenDocumentHandler {
    private String projectFilePath = ""; 

    public MacOpenHandler () {
        com.apple.mrj.MRJApplicationUtils.registerOpenDocumentHandler(this) ; 
    }

    @Override
    public void handleOpenFile( File projectFile ) { 
        try {
            if( projectFile != null ) {
                projectFilePath = projectFile.getCanonicalPath();
                   System.out.println( projectFilePath );  // Prints the path fine.
            }
        } catch (IOException e) {}  
    }

    public String getProjectFilePath() {
        return projectFilePath;
    }
}

As mentioned in the comment above "getProjectFilePath()" is always Empty !

Brad
  • 4,457
  • 10
  • 56
  • 93
  • Could you please post a bit of your code in question? – Moritz Petersen May 11 '12 at 06:44
  • 1
    [JWS](http://stackoverflow.com/tags/java-web-start/info) can register an interest in a file type. See the [file services demo.](http://pscode.org/jws/api.html#fs) for an example. – Andrew Thompson May 11 '12 at 07:01
  • Possible duplicate of [How do I pass a file as argument to my Java application created using JAR Bundler?](http://stackoverflow.com/questions/7493742/how-do-i-pass-a-file-as-argument-to-my-java-application-created-using-jar-bundle) Please update your question if not. – trashgod May 11 '12 at 14:32
  • trashgod ... I have already checked that post. It was actually helpful, but i am not using OSXAdapter. I am using MRJToolkitStubs. – Brad May 11 '12 at 17:50
  • I do not know if i can answer my question some how ... I have used an Inner class & it worked fine. Thanks everyone :) – Brad May 12 '12 at 04:49
  • Posting an answer will allow you to mark it as your choice, but after some time, 2-3 days, I don't really recall the exact "timeout". – Morfic Jun 13 '12 at 16:07

2 Answers2

1

On Java 9, use Desktop.setOpenFileHandler()

The proprietary com.apple.eawt packages have been removed from recent versions of Java and has been incorporated into various methods in the Desktop class. For your specific example:

import java.awt.desktop.OpenFilesHandler;
import java.awt.desktop.OpenFilesEvent;
import java.io.File;
import java.util.List;

public class MyOpenFileHandler implements OpenFilesHandler {

    @Override
    public void openFiles​(OpenFilesEvent e) {
        for (File file: e.getFiles​()) {
            // Do whatever
        }
    }
}

Then elsewhere, add this:

Desktop.getDesktop().setOpenFileHandler(new MyOpenFileHandler());

The OpenFilesEvent class also has a getSearchTerm() method. Say that a person used Spotlight on macOS to search for the word "StackOverflow", then decided to open up a document. With this method, can you determine that "StackOverflow" was the word they searched for, and choose to do something with that (perhaps highlight the first occurrence of the word).

Thunderforge
  • 19,637
  • 18
  • 83
  • 130
0

You're going to want to use the Apple Java Extensions.

They should be included in any JDK that runs on Mac OS X, but the documentation is kind of hard to get. See this answer for more details.

Specifically, you'll want to make an OpenFilesHandeler.

This code snippet should work:

import com.apple.eawt.event.OpenFilesHandeler;
import com.apple.eawt.event.AppEvent;
import java.io.File;
import java.util.List;

class MacOpenHandler implements OpenFilesHandeler {

    @Override
    public void openFiles(AppEvent.OpenFilesEvent e)  { 
        List<File> files = e.getFiles();
        // do something
    }

}

And somewhere:

import com.apple.eawt.Application;

...

MacOpenHandeler myOpenHandeler = new MacOpenHandeler();
Application.getApplication().setOpenFileHandler(myOpenHandeler);
Community
  • 1
  • 1
Gavin S. Yancey
  • 1,216
  • 1
  • 13
  • 34