File association with electron builder
Asked Answered
S

1

9

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.

Speaking answered 3/4, 2018 at 3:32 Comment(4)
You can create your custom protocol to open your app like myapp: --params in your terminal and/or exec, is that what you want?Coulombe
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.Speaking
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. I just haven't gotten around to test this theory out yet.Crawler
Came across this related question, and looking at the comments it seems my suspicion was correct. open-file(macOS specific) in the NodeJS documentation mentions using process.argv for windows.Crawler
A
4

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.

Afc answered 10/10, 2020 at 6:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.