Skip to content

How to open a program by using the "Open with..." context menu item.

Notifications You must be signed in to change notification settings

wjenkins92/JavaOpenWith

Repository files navigation

How to get files used with "Open With" on a Mac in Java?

Step 1: Add an OpenFileHandler Start by creating and setting an OpenFileHandler using Desktop.getDesktop().setOpenFileHandler().

import java.awt.desktop.OpenFilesEvent;
import java.awt.desktop.OpenFilesHandler;

public class FileHandler implements OpenFilesHandler
{
	...
	public void openFiles (OpenFilesEvent e)
	{
		// Do something with the files using e.getFiles().
		...
	}
	...
}
import java.awt.Desktop;

public class SampleProgram
{
	public static void main (String[] args)
	{
		...
		FileHandler fh = new FileHandler();
		Desktop.getDesktop().setOpenFileHandler(fh);
		...
	}
}

Step 2: Compile Compile your source code into a .app executable. At the terminal execute:

javapackager -makeall -appclass SampleProgram

Step 3: Modify Info.plist You must modify your application's Info.plist and add a key for each file type you want your application to modify. To do this, right click on your .app executable and select "Show Package Contents".

alt text

Navigate to /Contents/ and open Info.plist with a text editor. At the end of this file, add a CFBundleDocumentTypes key and an array holding each of the file types you want your application to be able to open. For more information, see the Apple Developer's documentation for CFBundleDocumentTypes, Uniform Type Identifiers, and System-Declared Uniform Type Identifiers.

Because I want my application to read text (.txt) and HTML (.htm, .html) files, my application's Info.plist looks like this:

<?xml version="1.0" ?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
	<dict>
		<key>LSMinimumSystemVersion</key>
		<string>10.9</string>
		<key>CFBundleDevelopmentRegion</key>
		<string>English</string>
		...
		<key>CFBundleDocumentTypes</key>
		<array>
			<dict>
				<key>CFBundleTypeExtensions</key>
				<array>
					<string>txt</string>
				</array>
				<key>CFBundleTypeRole</key>
				<string>Editor</string>
				<key>LSItemContentTypes</key>
				<array>
					<string>public.plain-text</string>
				</array>
			</dict>
			<dict>
				<key>CFBundleTypeExtensions</key>
				<array>
					<string>htm</string>
				</array>
				<key>CFBundleTypeRole</key>
				<string>Editor</string>
				<key>LSItemContentTypes</key>
				<array>
					<string>public.html</string>
				</array>
			</dict>
			<dict>
				<key>CFBundleTypeExtensions</key>
				<array>
					<string>html</string>
				</array>
				<key>CFBundleTypeRole</key>
				<string>Editor</string>
				<key>LSItemContentTypes</key>
				<array>
					<string>public.html</string>
				</array>
			</dict>
		</array>
	</dict>
</plist>

Don't forget to save the modified Info.plist!

Step 5: Open With Now you can open files with the system's context menu item "Open With". The file or files used with "Open With" can be accessed with the OpenFilesEvent.getFiles() method.

Right click the file you want to edit. Select Open With → Other. alt text

Navigate to where your .app is stored. Select it, then click Open. alt text

The file was successfully passed into our program. alt text

About

How to open a program by using the "Open with..." context menu item.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages