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 :: hello world js 
Javascript :: calculator program in javascript 
Javascript :: && condition in javascript 
Javascript :: how to validate multiple input field in javascript 
Javascript :: switch 
Javascript :: react how to get checkbox value on click 
Javascript :: how to use npm package in javascript 
Javascript :: javascript number with commas 
Javascript :: how to make a discord bot delete messages after time js 
Javascript :: javascript buffer to file 
Javascript :: react native get screen height and width 
Javascript :: creating the find method javascript 
Javascript :: run function periodically with setInterval 
Javascript :: js how to get n fibonacci number 
Javascript :: js add obj prop dynamically 
Javascript :: eleventy filter newlines 
Javascript :: javascript Benefit of Using Symbols in Object 
Javascript :: creating js classes 
Javascript :: post css nesting nuxt 
Javascript :: find the missing number in js 
Javascript :: hide loader if datatable data loaded jquery 
Javascript :: divide array in chunks 
Javascript :: calculate age from date of birth javascript 
Javascript :: phaser animation repeat event 
Javascript :: iterate cy.get(') elements 
Javascript :: white when respons show code 
Javascript :: navbar permanently in react router dom v6 
Javascript :: js if animation infinity end 
Javascript :: TypeError: expressValidator is not a function 
Javascript :: 404 responses in express 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =