Electron - Executing a program on button click


#1

Hey,

I am new to Electron and i want to build a small app for myself where i can execute programs/games etc on a button click.

If i open the electron app it automatically starts the program/game.

var child = require('child_process').execFile;
var executablePath = "D:\\Games\\GAMEFOLDER\\GAMENAME.exe";
child(executablePath, function(err, data) {
if(err){
console.error(err);
return;
 }
console.log(data.toString());
});

How do i code it so that it only starts the exe when i click a button?


#2

What file is this code from? Is it your main file?


#3

Yes, this is my main.js but i already found a way to do it using scripts in html with electron.shell


#4

I would suggest that you write your apps in a more conventional fashion for Electron: have all of the app code live in a folder with an index.html just like you would if you were making it available on the Internet, then use main.js to create a BrowserWindow that you can point at this HTML file. You can see this pattern exhibited in the Electron quick start.


#6

I have the same problem like VeepZy, But I think he described it wrong,

I have an small Electron app, I have an Textfield inside where an other XYZ.js outputs text inside the textfield, everything works fine… the only problem is the XYZ.js Start with th Electron app and give me output on the app Start. How I can bind it to an Button so I can run it by Clicking on the Button? XYZ.js is an Async arrow Function with a Try, Catch block inside. Any Ideas? Ps. the XYZ.js is already binded inside my Index.html like this
<script src="./XYZ.js"></script>


#7

You want to use the addEventListener() method. If you share your code, I can tell you the best way to integrate it.


#10

I have now solved it with $(document).ready(function(){ I think it is Jquery I saw it from a YouTube Video, Is this a good method?

$(document).ready(function(){

('#btntest').click(function() { (’#myTextarea1’).val(‘Test Running’);
console.log(‘Test Running’);

(async () =&gt; {

const browser = await puppeteer.launch({ headless: false, args: ['--disable-infobars']})
    const page = await browser.newPage();
    await page.waitFor(9000);
    var htag = $('#myTextarea0').val();

try {
    await page.goto(htag);
    await page.waitForSelector('._dyez');

    await page.$('._dyez');
    $('#myTextarea').val('SomeText1');(function() {
        console.log('Text');
    });
} catch(e) {
    $('#myTextarea').val('SomeText3');(function() {
        console.log('Text', e);
    });
}
finally {
    browser.close()
}

})();

});

#11

If it works and you want to use jQuery, then absolutely.