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

问题描述:

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

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

我需要的是:

一个按钮。

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

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

ping x.x.x.x

结果将显示在TextArea中。

And the result will be displayed in a TextArea.

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

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

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

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.

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

Any suggestion will be appreciated, thanks :)

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

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

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

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

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.