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

2022-01-10 00:00:00 python shell node.js electron javascript

我正在尝试使用 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 electron.

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.

相关文章