如何从Atom电子应用程序调用Shell脚本或python脚本

时间:2022-09-18 20:30:05

I'm trying to use the Atom electron to write a Desktop App for both Mac and Windows.

我正在尝试使用Atom电子为Mac和Windows编写桌面应用程序。

What I need here is :

我需要的是:

A button.

And when the user click the button it runs the following shell (or python script):

当用户单击该按钮时,它将运行以下shell(或python脚本):

ping x.x.x.x

And the result will be displayed in a TextArea.

结果将显示在TextArea中。

I tried to use [shelljs] and [yargs] but it seems like it is not workable with Atom electron.

我尝试使用[shelljs]和[yargs],但似乎它对Atom电子不起作用。

All I want is to use JAVASCRIPT to write Desktop App (with GUI of course) that calls some script (shell && python) to do some automation work.

我想要的只是使用JAVASCRIPT来编写桌面应用程序(当然还有GUI),它调用一些脚本(shell && python)来做一些自动化工作。

Any suggestion will be appreciated, thanks :)

任何建议将不胜感激,谢谢:)

3 个解决方案

#1


18  

It can be done directly with Node, you can use the child_process module. Please notice this is asynchronous.

它可以直接使用Node完成,您可以使用child_process模块​​。请注意这是异步的。

const exec = require('child_process').exec;

function execute(command, callback) {
    exec(command, (error, stdout, stderr) => { 
        callback(stdout); 
    });
};

// call the function
execute('ping -c 4 0.0.0.0', (output) => {
    console.log(output);
});

I encourage you to also have a look at npm, there are tons of modules that could help you to do what you want, without calling a python script.

我鼓励你也看看npm,有很多模块可以帮助你做你想做的事情,而无需调用python脚本。

#2


3  

Try node-powershell npm. You can directly execute shell script commands and display result.

尝试node-powershell npm。您可以直接执行shell脚本命令并显示结果。

var shell = require('node-powershell')
var ps = new shell()
ps.addCommand('ping -c 4 0.0.0.0')
ps.invoke()
.then(function (output) {
    console.log(output)
})
.catch(function (err) {
    console.log(err)
    ps.dispose()
})

See: https://www.npmjs.com/package/node-powershell

#3


0  

you could use child_process to archive what you are trying to do by using the following code

您可以使用child_process通过使用以下代码存档您尝试执行的操作

var exec = require('child_process').exec
function Callback(err, stdout, stderr) {
    if (err) {
        console.log(`exec error: ${err}`);
        return;
    }else{
        console.log(`${stdout}`);
    }
}

res = exec('ping xxx.xxx.xxx', Callback);

#1


18  

It can be done directly with Node, you can use the child_process module. Please notice this is asynchronous.

它可以直接使用Node完成,您可以使用child_process模块​​。请注意这是异步的。

const exec = require('child_process').exec;

function execute(command, callback) {
    exec(command, (error, stdout, stderr) => { 
        callback(stdout); 
    });
};

// call the function
execute('ping -c 4 0.0.0.0', (output) => {
    console.log(output);
});

I encourage you to also have a look at npm, there are tons of modules that could help you to do what you want, without calling a python script.

我鼓励你也看看npm,有很多模块可以帮助你做你想做的事情,而无需调用python脚本。

#2


3  

Try node-powershell npm. You can directly execute shell script commands and display result.

尝试node-powershell npm。您可以直接执行shell脚本命令并显示结果。

var shell = require('node-powershell')
var ps = new shell()
ps.addCommand('ping -c 4 0.0.0.0')
ps.invoke()
.then(function (output) {
    console.log(output)
})
.catch(function (err) {
    console.log(err)
    ps.dispose()
})

See: https://www.npmjs.com/package/node-powershell

#3


0  

you could use child_process to archive what you are trying to do by using the following code

您可以使用child_process通过使用以下代码存档您尝试执行的操作

var exec = require('child_process').exec
function Callback(err, stdout, stderr) {
    if (err) {
        console.log(`exec error: ${err}`);
        return;
    }else{
        console.log(`${stdout}`);
    }
}

res = exec('ping xxx.xxx.xxx', Callback);