18

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

What I need here is :

A button.

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

ping x.x.x.x

And the result will be displayed in a TextArea.

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

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 :)

3 Answers 3

33

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

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm using electron and I get runtime-core.esm-bundler.js?9e79:217 Uncaught TypeError: exec is not a function when
5

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

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

Comments

4

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.