Home > Net >  Open external application with electron
Open external application with electron

Time:12-16

I want to create a personal app with Electron to manage other apps on my device. The problem is that I try in different ways to create a link that opens my own .exe applications but this is not allowed. Is there a way to do it or is it impossible? I leave my link code below.

<div >
        <h1>"Aplication"</h1>
        <button>
            <a href="options/hollow.bat">Hollow</a>
        </button>
    </div>

This link refers to a .bat file.

START D:/SteamLibrary/steamapps/common/HollowKnight/hollow_knight.exe

CodePudding user response:

There is different ways to do that, but the simplest way to open a file with the default OS application is to use the shell:

const { shell } = require('electron')
shell.openExternal('options/hollow.bat')

CodePudding user response:

There are a few ways you can do this as @LeonardoPF mentioned...

So you have your HTML file as so and link your app.js function (in this case we will call it doSomething()

<!-- FILE: index.html --->
<script type="text/javascript" src="app.js"></script>
<div >
        <h1>Application</h1>
        <button>
            <a onclick="doSomething()">Hollow</a>
        </button>
    </div>

One way you can do this is using shell as follows...

Now, in our script file that we hooked called app.js, we write that function. For this, we will just directly link it to the executable you just linked above.

Method #1

// FILE: app.js
const { shell } = require('electron');
const path = require('path');
const app = path.join("D:/SteamLibrary/steamapps/common/HollowKnight/hollow_knight.exe");
// Open a local file in the default app


function doSomething(){
   shell.openExternal(app);
}

I haven't tested this one but apparently you can also use .openItem().

Method #2

// FILE: app.js
const { shell } = require('electron');
const path = require('path');
const app = path.join("D:/SteamLibrary/steamapps/common/HollowKnight/hollow_knight.exe");


// Open a local file in the default app
function doSomething(){
   shell.openItem(app);
}

Another way is using the child_process library...

Method #1

// FILE: app.js
const spawn = require('child_process').spawn;
const path = require('path');
const app = path.join("D:/SteamLibrary/steamapps/common/HollowKnight/hollow_knight.exe");


// spawn the application process
function doSomething(){
   spawn(app);
}

Method #2

// FILE: app.js
const execFile = require('child_process').execFile;
const path = require('path');
const app = path.join("D:/SteamLibrary/steamapps/common/HollowKnight/hollow_knight.exe");


// Execute the file
function doSomething(){
   execFile(app, function(err, stdout) {
       if(err){
          console.error(err);
          return;
       }
       console.log(stdout);
   });
}
  • Related