从Electron主线程运行批处理文件

时间:2022-03-16 02:31:49

I'm attempting to run a simple batch file from my electron application. Here is my code:

我正在尝试从我的电子应用程序运行一个简单的批处理文件。这是我的代码:

globalShortcut.register('Control+B', () => {
    log.info('Batch File Triggered: ' + app.getAppPath() + '\\local\\print.bat')
    require('child_process').exec(app.getAppPath() + '\\local\\print.bat', function (err, stdout, stderr) {
        if (err) {
            // Ooops.
            // console.log(stderr);
            return console.log(err);
        }

        // Done.
        console.log(stdout);
    });
})

The batch file should be triggered when Control+B is pressed by the user, but it does not work. The log entry is made, and I've verified the path is correct, but the file is never actually launched.

当用户按下Control + B时​​,应该触发批处理文件,但它不起作用。日志条目已生成,我已验证路径是否正确,但文件从未实际启动。

I found these questions, which ask the same question, but these are 4 years old at this point and none of the answers have worked for me, there is no display, no error, nothing.

我发现了这些问题,问了同样的问题,但是这些问题已经有4年了,没有一个答案对我有用,没有显示,没有错误,没有。

I've also tried the child_process.spawn but that also did nothing noticeable.

我也尝试了child_process.spawn,但也没有做任何明显的事情。

var ls = spawn('cmd.exe', ['/c', app.getAppPath() + '\\local\\print.bat']);

How can I launch my batch file from my electron application?

如何从我的电子申请中启动我的批处理文件?

2 个解决方案

#1


3  

I've just discovered such an easy way to do this. You can use the electron shell module, like this:

我刚刚发现了这么简单的方法。您可以使用电子外壳模块,如下所示:

const {shell} = require('electron');
// Open a local file in the default app
shell.openItem(app.getAppPath() + '\\local\\print.bat');

#2


1  

try using the code below

尝试使用下面的代码

function Process() {
    const process = require('child_process');   
    var ls = process.spawn('script.bat');
    ls.stdout.on('data', function (data) {
      console.log(data);
    });
    ls.stderr.on('data', function (data) {
      console.log(data);
    });
    ls.on('close', function (code) {
       if (code == 0)
            console.log('Stop');
       else
            console.log('Start');
    });
};

Process();

not forgetting to run

不要忘了跑

npm install child_process

on your terminal

在你的终端上

#1


3  

I've just discovered such an easy way to do this. You can use the electron shell module, like this:

我刚刚发现了这么简单的方法。您可以使用电子外壳模块,如下所示:

const {shell} = require('electron');
// Open a local file in the default app
shell.openItem(app.getAppPath() + '\\local\\print.bat');

#2


1  

try using the code below

尝试使用下面的代码

function Process() {
    const process = require('child_process');   
    var ls = process.spawn('script.bat');
    ls.stdout.on('data', function (data) {
      console.log(data);
    });
    ls.stderr.on('data', function (data) {
      console.log(data);
    });
    ls.on('close', function (code) {
       if (code == 0)
            console.log('Stop');
       else
            console.log('Start');
    });
};

Process();

not forgetting to run

不要忘了跑

npm install child_process

on your terminal

在你的终端上