当我在Electron应用程序中双击打开文件时,Argv [1]返回意外值 [英] Argv[1] returns unexpected value when I open a file on double click in Electron app

查看:319
本文介绍了当我在Electron应用程序中双击打开文件时,Argv [1]返回意外值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图双击打开一个文件。该文件是使用电子打包器为Mac App Store构建的。

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.

为argv [0]返回的数据是应用程序路径(如预期的那样) ,对于argv [1]类似于-psn_0_857362。我受到了印象,argv [1]将是我要查找的文件的路径。

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.

我正在使用的代码的简化版本(在main.js中)是:

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;

}
);

为什么不显示路径?这是不可能与mac应用程序商店或我需要做其他事情,使其按预期工作?

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?

推荐答案

开macOS,您可能需要收听 app 事件 open-file

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

这篇关于当我在Electron应用程序中双击打开文件时,Argv [1]返回意外值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆