Java Swing - How to double-click a project file on a Mac to open an application and download a file?

I created a Mac Java Swing application, and I installed the extension file (* .pkkt) for it in the Info.plist file, so when I double-click on this file it opens my application.

When I do this, the program works fine. Now I need to load the project (* .pkkt) into the program, but the file path is not passed as an argument to the main (...) method on the Mac, as it does on the Windows operating system.

After some searching, I found the Apple processing bank " MRJToolkitStubs ", which has the MRJOpenDocumentHandler interface for handling such click files. I tried to use it to download this file by implementing this interface in the main class of the program, but it does not work. The implemented method is never called when the program starts.

How does this interface work?

----------------------------------------------- - Edit: Add sample code

Here is the code I'm 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!

+5
source share
2 answers

You will want to use Apple Java Extensions .

JDK, Mac OS X, . . .

, OpenFilesHandeler.

:

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
    }

}

-:

import com.apple.eawt.Application;

...

MacOpenHandeler myOpenHandeler = new MacOpenHandeler();
Application.getApplication().setOpenFileHandler(myOpenHandeler);
0

Java 9 Desktop.setOpenFileHandler()

com.apple.eawt Java Desktop. :

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
        }
    }
}

:

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

OpenFilesEvent getSearchTerm(). , Spotlight macOS "StackOverflow", . , "StackOverflow" - , , - (, ).

0

All Articles