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

删除回忆录丶 提交于 2019-12-10 21:53:32

问题


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?


回答1:


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);
});


来源:https://stackoverflow.com/questions/50935292/argv1-returns-unexpected-value-when-i-open-a-file-on-double-click-in-electron

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!