5

I am building an Electron app for editing .txt files on a windows computer. I have used electron builders fileAssociations to open .txt files however I cannot get the path of the file. When I looked in the Electron docs I found this which is exactly what I want except it is mac-only... is there a way to get the same functionality on Windows?

Alex Hawking
  • 895
  • 5
  • 13
  • 32

4 Answers4

2

As Lucas Roland mentioned, you have to parse process.argv to get the filepath

if (process.argv.length >= 2) { // or electron.remote.process.argv
    let filePath = process.argv[1];
    //open, read, handle file
}
Saleem
  • 1,049
  • 12
  • 27
0

On Windows, you have to parse process.argv (in the main process) to get the filepath.

from https://github.com/electron/electron/blob/master/docs/api/app.md#event-open-file-macos

Intradeus
  • 134
  • 4
  • Could you explain how to do this a little more thouroughly? – Alex Hawking Jun 23 '20 at 04:15
  • 1
    i think the authors problem is not parsing the process.argv, the problem is to knwo when he should parse the process.argv. i cant find an event for this too – Tobias Jun 23 '20 at 08:43
0

On Windows, you need to use process.argv in the main process to read the file path. According to this answer, you can use the fs package to open, read & write files. There are a few more ways described to do the same thing.

Also, the following snippet from this blog post might be helpful.

How to configure your app to open linked files in Windows

On Windows, you have to parse process.argv to get the file path. Then, you can use the IPC module to handle messages from the renderer process (web page) and retrieve a datastore from a file. This is how we did it:

In the main process:

var ipc = require('ipc');
var fs = require('fs');

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

I am not very well versed with electron, else I would have tried to give you a better answer, but this is what I could find with my understanding about it. I hope this helps!

Siddhesh T
  • 346
  • 1
  • 3
  • 13
  • The blog post link above might also answer your other [question](https://stackoverflow.com/q/62420427/3070768) – Siddhesh T Jun 26 '20 at 10:39
0

For windows, parsing process.argv on app ready worked for me. So in the main process somewhere:

const { app } = require('electron');
const devEnv = /electron/.test(process.argv[0]);

app.on('ready', () => {
   if (process.platform.startsWith('win') && !devEnv && process.argv.length >= 2) {
      const filePath = process.argv[1];

      // In my app, I initialise a new window here and send filePath through 
      // to the renderer process so it can be read and displayed.

   } else {

      // Create whatever your default window is (in my case, an empty document)      

   }
});

On windows, this event is fired every time you open a new file even if another file is already open in the application.

Remember also that during development, we launch the application with electron . or electron . --debug or something similar. Because of this, I also test whether the first argument is electron and skip over file opening logic in this case (the case that devEnv evaluates to 'true').

bsigma1
  • 203
  • 4
  • 8