BinaryMoon
BinaryMoon

Reputation: 115

Argv[1] returns unexpected value when I open a file on double click in Electron app

I am trying to open a file on double click. The file is being built for the Mac App Store using electron-packager.

I have things set up so that my electron app opens when the file is double clicked, however the filename of the double clicked file is not passed to the app in the command line parameters.

The data being returned for argv[0] is the app path (as expected), and for argv[1] is something similar to -psn_0_857362. I was under the impressions argv[1] would be the path to the requested file, which is what I am looking for.

A simplified version of the code I am using (in main.js) is:

ipcMain.on(
'getOpenFile',
function( e ) {

    let data = null;

    if ( process.argv.length >= 2 ) {
        data = process.argv[1];
    }

    e.returnValue = data;

}
);

Why is it not displaying the path? Is this not possible with the mac app store or do I need to do something else to make it work as expected?

Upvotes: 4

Views: 1027

Answers (1)

user8022331
user8022331

Reputation:

On macOS, you may have to listen to the app event open-file from the main process:

app.on('open-file', (event, path) =>
{
    event.preventDefault();
    console.log(path);
});

Upvotes: 4

Related Questions