6

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.

Javis Perez
  • 3,513
  • 3
  • 18
  • 25
  • You can create your custom protocol to open your app like `myapp: --params` in your terminal and/or exec, is that what you want? – Marcelo Formentão Apr 03 '18 at 13:21
  • Well, not really, I want to be able to open an associated file (e.g. test.myapp) with just a double click, I've used file associations for that but I don't see a way to receive the file path on my app. I mean, i don't want to have to type the protocol to open a file, each time, i want to be able to do it by just double clicking on the file. – Javis Perez Apr 03 '18 at 13:36
  • 1
    I'm currently also looking for an answer, and though I'm not certain, my _guess_ is that the answer lies within the NodeJS API which Electron is built upon. I would imagine that starting an app through file type association would be the same as launching the application while passing some arguments, so the file path(s) should show up in [`process.argv`](https://nodejs.org/docs/latest/api/process.html#process_process_argv). I just haven't gotten around to test this theory out yet. – klesus Nov 18 '18 at 00:12
  • Came across this [related question](https://stackoverflow.com/questions/50935292/argv1-returns-unexpected-value-when-i-open-a-file-on-double-click-in-electron?rq=1), and looking at the comments it seems my suspicion was correct. [`open-file`](https://electronjs.org/docs/api/app#event-open-file-macos)(macOS specific) in the NodeJS documentation mentions using `process.argv` for windows. – klesus Nov 18 '18 at 00:58

1 Answers1

1

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.

Feugy
  • 1,778
  • 13
  • 16