Open a file with electron app



I want certain file types to open with my electron app. I’ve associate the filetype with my electron in macOS, and when I double click them the app loads fine. However the problem I’m having is that the app loads without loading the file that was clicked on.

Say for example I click on example.txt, this loads my electron app but does not load example.txt in it, just my default app. How do I tell my app to load the file that was clicked on? process.argv seems to be empty, how do I get the filename that triggered the opening?



I compiled an application and checked where the path to the file we opened the application with is and it’s quite simple . The important thing is that you have the use the “process” variable provided by electron and not in the BrowserWindow. You can access the file path like this
const {app} = require(“electron”);
Here is argv[0] contains the path to your app and the second element in that array contains the path to the file the app was opened with.


Found this snippet online, forgot where but I’ve been using it ever since. This might be useful for someone out there so I’m just gonna leave it here.

Add this in Main process :

// read the file and send data to the render process
ipcMain.on('get-file-data', function(event) {
  var data = null
  if (process.platform == 'win32' && process.argv.length >= 2) {
    var openFilePath = process.argv[1]
    data = openFilePath
  event.returnValue = data

and this in Renderer process :

var data = ipcRenderer.sendSync('get-file-data')
if (data ===  null) {
    console.log("There is no file")
} else {
    // Do something with the file.


@panos how did you associate specific file types with your app? would love to do something similar.