Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

electron js execute command

//Uses node.js process manager
const electron = require('electron');
const child_process = require('child_process');
const dialog = electron.dialog;

// This function will output the lines from the script 
// and will return the full combined output
// as well as exit code when it's done (using the callback).
function run_script(command, args, callback) {
    var child = child_process.spawn(command, args, {
        encoding: 'utf8',
        shell: true
    });
    // You can also use a variable to save the output for when the script closes later
    child.on('error', (error) => {
        dialog.showMessageBox({
            title: 'Title',
            type: 'warning',
            message: 'Error occured.
' + error
        });
    });

    child.stdout.setEncoding('utf8');
    child.stdout.on('data', (data) => {
        //Here is the output
        data=data.toString();   
        console.log(data);      
    });

    child.stderr.setEncoding('utf8');
    child.stderr.on('data', (data) => {
        // Return some data to the renderer process with the mainprocess-response ID
        mainWindow.webContents.send('mainprocess-response', data);
        //Here is the output from the command
        console.log(data);  
    });

    child.on('close', (code) => {
        //Here you can get the exit code of the script  
        switch (code) {
            case 0:
                dialog.showMessageBox({
                    title: 'Title',
                    type: 'info',
                    message: 'End process.
'
                });
                break;
        }

    });
    if (typeof callback === 'function')
        callback();
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: es6 spread 
Javascript :: Conditionally pass props to react component 
Javascript :: how to stop canvas resizing from resizing images 
Javascript :: first letter string uppercase javascript 
Javascript :: nodemon install locally json file 
Javascript :: convert a date range into an array of date in js 
Javascript :: datepicker react native 
Javascript :: Uncaught TypeError: Data.filter is not a function 
Javascript :: sequelize get all data 
Javascript :: js convert order to char 
Javascript :: javascript audio play on click 
Javascript :: javascript get object where 
Javascript :: node.js brotli 
Javascript :: reduce to calculate sum react 
Javascript :: js clear map 
Javascript :: ruby on rails test if all parameters in json request are here 
Javascript :: no routes matched location / react router 
Javascript :: remove script in react js 
Javascript :: submit form with ajax 
Javascript :: how to do an isogram in javascript 
Javascript :: push values to data object in vue 
Javascript :: Prevent Double tap in React native 
Javascript :: how to make a alert popup message in javascript 
Javascript :: ejemplo archivo json 
Javascript :: Using vuejs-datepicker with Nuxt JS 
Javascript :: [Homepage] is not a <Route component. All component children of <Routes must be a <Route or <React.Fragment 
Javascript :: npm koa 
Javascript :: carbon to moment js conversion 
Javascript :: implement singleton javascript 
Javascript :: django ajax redirect to a view on success 
ADD CONTENT
Topic
Content
Source link
Name
5+7 =