Package will not be available at compile time in Java 9. Use java.awt.Desktop instead. See JEP 272 'Platform-Specific Desktop Features': http://openjdk.java.net/jeps/272
public class Launcher { public static void main(String[] args) { if (System.getProperty("os.name").contains("OS X")){ com.apple.eawt.Application a = com.apple.eawt.Application.getApplication(); Handler_OpenFile h_open = new Handler_OpenFile(); a.setOpenFileHandler(h_open); Application.launch(Main.class, args); } } }
public static void handleStartupArguments() { // All Mac EAWT logic // Intercept session file double-clicked on Mac OS, passed by file association set by install4j Application application = Application.getApplication(); application.setOpenFileHandler(new OpenFilesHandler() { @Override public void openFiles(OpenFilesEvent e) { if (Launcher.startupArguments.length > 0 || e.getFiles().size()>1){ return; } String fileName = e.getFiles().get(0).getAbsolutePath(); if (!fileName.endsWith(".cys")){ return; } String[] argsArray = {fileName}; Launcher.startupArguments = argsArray; } }); } }
import java.io.File; import java.util.List; import com.apple.eawt.AppEvent.OpenFilesEvent; import com.apple.eawt.Application; import com.apple.eawt.OpenFilesHandler; public class MacFiles implements OpenFilesHandler{ private List<File> files; private FileSaving myFiling; public MacFiles(FileSaving filing) {// Application.getApplication().setOpenFileHandler(this); myFiling=filing; } public List<File> getFiles() { return files; } @Override public void openFiles(OpenFilesEvent event) { files = event.getFiles(); File myFile=new File(files.get(0).getAbsolutePath()); myFiling.readFile(myFile,true); } }
import com.apple.eawt.*; public class MyMainClass { private static boolean listenerRegistered = false; public static void main(String[] args) throws Exception { if(!listenerRegistered) { Application.getApplication().setOpenFileHandler(new OpenFilesHandler() { public void openFiles(AppEvent.OpenFilesEvent evt) { List<String> filenames = new ArrayList<String>(); for(File f : evt.getFiles()) { filenames.add(f.getAbsolutePath()); } MyMainClass.main(filenames.toArray(new String[filenames.size()])); } }); listenerRegistered = true; } // rest of main goes here } }
if (System.getProperty("os.name").contains("OS X")){ com.apple.eawt.Application a = com.apple.eawt.Application.getApplication(); a.setOpenFileHandler(new com.apple.eawt.OpenFilesHandler() { @Override public void openFiles(com.apple.eawt.AppEvent.OpenFilesEvent e) { for (Object oFile : e.getFiles()){ if (oFile instanceof File) { File file = (File) oFile; // do the actual open logic here } else { __l.warn("The OS just told us to open a file but handed us "+oFile.toString()); } } } });
public MacOSAppEventDispatcher(final Application app, final EventService eventService) { this.eventService = eventService; app.setAboutHandler(this); app.setPreferencesHandler(this); app.setPrintFileHandler(this); app.setQuitHandler(this); app.addAppEventListener(this); app.setOpenFileHandler(this); }
application.setOpenFileHandler(new OpenFilesHandler() { @Override public void openFiles(AppEvent.OpenFilesEvent ofe) {
app.setPreferencesHandler(this); app.setQuitHandler(this); app.setOpenFileHandler(this); app.setOpenURIHandler(this);