How to handle file when dragged on to application icon


#1

Hi there,

How can I handle files that are dragged on to an atom-shell app?

I was hoping the following might do the trick, but no luck:

app.on('finish-launching', function() {
	app.on('open-file', function(event, path) {
		console.log(path);
		event.preventDefault();
	});
});

Thanks!


#2

Files dragged on to the icon are handled exactly the same as files opened from File > Open. You should use atom.workspace.addOpener to register a function that will be called when files are opened. Your function can then check the path and return a view if it wants to handle it, or nothing to continue looking for openers.

atom.workspace.addOpener(function(uri) {
  console.log(uri);
});

Note the docs appear to be incorrect atm, it says atom.project.addOpener but it should be atom.workspace.addOpener.


#3

Thanks for your answer, but I am talking about Atom-Shell, not Atom itself…


#4

Atom-shell should be a different forum. I’ve made this mistake several times.


Atom Shell should be a different forum?
#5

oops… my apologies!

So, if this is on OS X, you also need to specify the file types your application accepts in the Info.plist. See the plist for atom here: https://github.com/atom/atom/blob/master/resources/mac/atom-Info.plist

The key is CFBundleDocumentTypes. e.g. to accept jpg files, add this to your Info.plist:

<key>CFBundleDocumentTypes</key>
	<array>
		<dict>
			<key>CFBundleTypeExtensions</key>
			<array>
				<string>jpeg</string>
				<string>jpg</string>
			</array>
			<key>CFBundleTypeIconFile</key>
			<string></string>
			<key>CFBundleTypeName</key>
			<string>JPEG</string>
			<key>CFBundleTypeRole</key>
			<string>Editor</string>
			<key>LSHandlerRank</key>
			<string>Alternate</string>
		</dict>
	</array>

Once I added this dropping onto the application icon worked and logged the filename. OS X may cache the plist, you can clear the cache by moving the application somewhere else and back again.


#6

The following solved it for me in combination with postcasio’s answer (thanks!):

var app = require('app');
app.on('open-file', function(event, pathToOpen) {
	event.preventDefault();
	console.log(pathToOpen);
});