Reputation: 109
I have an Electron app that is capable of editing multiple files in separate tabs, like Atom or VS Code. This works fine when opening files through a dialog, or opening a file through "open with" when the app is not running (by parsing argv).
However, I can't figure out how to "add" a file opened through "open with" to an already running app - by default, a second new instance of the app is created. I'd like to be able to somehow pass the path of the file to the original app instance.
I've looked through the docs and the only marginally promising thing I've found was the 'open-file' event, but that is only available on MacOS.
I am running Electron 9.0.5 and creating the file association using electron-builder.
Upvotes: 1
Views: 1462
Reputation: 3517
[..] by default, a second new instance of the app is created.
You can prevent that from happening using app.requestSingleInstanceLock()
and the second-instance
event.
Your event handler for the second-instance
event will receive as its second parameter all the command line parameters that the new instance received, and you can handle them similarly to you did before.
If your app is intended to run on macOS, you should also make use of open-file
. Do not rely on it exclusively, though, as the second-instance
event may also be fired on macOS (when starting an app not through Finder).
Upvotes: 1