Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File association with electron builder

I’m making a multi platform application with Electron and I’m trying to make the file association using electron-builder.

I’ve added it to the configuration and that works fine, when I double click on a file, it opens the app, which is expected, but I have no idea how to receive that file on my electron app, I’ve googled, looked issues on the electron-builder repo, but haven’t found anything. The only thing I’ve found so far is that you are suppose to handle that as a custom protocol, and makes sense to me if I want to open the file from a path or url, but I don’t understand how double clicking a file would trigger a custom protocol, does electron use a defined custom protocol when you double click a file associated with your app?

I haven’t found anything on the official docs neither, any help?

Thank you in advance.

like image 507
Javis Perez Avatar asked Apr 03 '18 03:04

Javis Perez


1 Answers

File associations with Electron works the same as for regular Node.js applications: you get parameters from the caller in process.argv array.

However, there is trick: when your application is packaged (that is, in an asar file), argv does not have the same number as arguments as when you run it in "dev" mode.

You can leverage app.isPackage() (doc) to make the difference:

if (app.isPackaged) {
  // workaround for missing executable argument)
  process.argv.unshift(null)
}
// parameters is now an array containing any files/folders that your OS will pass to your application
const parameters = process.argv.slice(2)

More details on this here.

like image 93
Feugy Avatar answered Sep 23 '22 13:09

Feugy