Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open external file with Electron

Tags:

electron

I have a running Electron app and is working great so far. For context, I need to run/open a external file which is a Go-lang binary that will do some background tasks. Basically it will act as a backend and exposing an API that the Electron app will consume.

So far this is what i get into:

  • I tried to open the file with the "node way" using child_process but i have fail opening the a sample txt file probably due to path issues.

  • The Electron API expose a open-file event but it lacks of documentation/example and i don't know if it could be useful.

That's it. How i open an external file in Electron ?

like image 425
Javier Cadiz Avatar asked May 21 '15 18:05

Javier Cadiz


1 Answers

There are a couple api's you may want to study up on and see which helps you.

fs

The fs module allows you to open files for reading and writing directly.

var fs = require('fs'); fs.readFile(p, 'utf8', function (err, data) {   if (err) return console.log(err);   // data is the contents of the text file we just read }); 

path

The path module allows you to build and parse paths in a platform agnostic way.

var path = require('path'); var p = path.join(__dirname, '..', 'game.config'); 

shell

The shell api is an electron only api that you can use to shell execute a file at a given path, which will use the OS default application to open the file.

const {shell} = require('electron'); // Open a local file in the default app shell.openItem('c:\\example.txt');  // Open a URL in the default way shell.openExternal('https://github.com'); 

child_process

Assuming that your golang binary is an executable then you would use child_process.spawn to call it and communicate with it. This is a node api.

var path = require('path'); var spawn = require('child_process').spawn;  var child = spawn(path.join(__dirname, '..', 'mygoap.exe'), ['game.config', '--debug']); // attach events, etc. 

addon

If your golang binary isn't an executable then you will need to make a native addon wrapper.

like image 148
justin.m.chase Avatar answered Oct 10 '22 05:10

justin.m.chase