Open external file with Electron
Asked Answered
R

5

46

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 ?

Rayraya answered 21/5, 2015 at 18:18 Comment(0)
D
83

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.

Defector answered 29/10, 2015 at 22:18 Comment(1)
It seems shell.openItem has been changed to shell.openPathGauleiter
S
1

Maybe you are looking for this ?

dialog.showOpenDialog refer to: https://www.electronjs.org/docs/api/dialog

If using [email protected], you can do like this:

const { dialog } = require('electron')
console.log(dialog.showOpenDialog({ properties: ['openFile', 'multiSelections'] }))

dialog.showOpenDialog(function(file_paths){
  console.info(file_paths)  // => this gives the absolute path of selected files.
})

when the above code is triggered, you can see an "open file dialog" like this (diffrent view style for win/mac/linux)

enter image description here

Structuralism answered 6/6, 2021 at 0:49 Comment(0)
M
0

Electron allows the use of nodejs packages.

In other words, import node packages as if you were in node, e.g.:

var fs = require('fs');

To run the golang binary, you can make use of the child_process module. The documentation is thorough.

Edit: You have to solve the path differences. The open-file event is a client-side event, triggered by the window. Not what you want here.

Miamiami answered 15/10, 2015 at 17:16 Comment(0)
A
0

I was also totally struggling with this issue, and almost seven years later the documentation is quite not clear what's the case with Linux.

So, on Linux it falls under Windows treatment in this regard, which means you have to look into process.argv global in the main processor, the first value in the array is the path that fired the app. The second argument, if one exist, is holding the path that requested the app to be opened. For example, here is the output for my test case:

Array(2)
0: "/opt/Blueprint/b-test"
1: "/home/husayngonzalez/2022-01-20.md"
length: 2

So, when you're creating a new window, you check for the length of process.argv and then if it was more than 1, i.e. = 2 it means you have a path that requested to be opened with your app.

Assuming you got your application packaged with the ability to process those files, and also you set the operating system to request your application to open those.

Acrylyl answered 20/1, 2022 at 21:23 Comment(0)
B
-2

I know this doesn't exactly meet your specification, but it does cleanly separate your golang binary and Electron application.

The way I have done it is to expose the golang binary as a web service. Like this

package main

import (
    "fmt"
    "net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
    //TODO: put your call here instead of the Fprintf
    fmt.Fprintf(w, "HI there from Go Web Svc. %s", r.URL.Path[1:])
}

func main() {
    http.HandleFunc("/api/someMethod", handler)
    http.ListenAndServe(":8080", nil)
}

Then from Electron just make ajax calls to the web service with a javascript function. Like this (you could use jQuery, but I find this pure js works fine)

function get(url, responseType) {
    return new Promise(function(resolve, reject) {
      var request = new XMLHttpRequest();
      request.open('GET', url);
      request.responseType = responseType;

    request.onload = function() {
    if (request.status == 200) {
      resolve(request.response);
    } else {
      reject(Error(request.statusText));
    }
  };

  request.onerror = function() {
    reject(Error("Network Error"));
  };

  request.send();
});

With that method you could do something like

get('localhost/api/somemethod', 'text')
  .then(function(x){
    console.log(x);
  }
Bataan answered 22/5, 2015 at 3:19 Comment(4)
It's an interesting approach. But how do we start this binary when we start Electron? Add an example with child_process. It's an interesting idea.Miamiami
The question states "it will act as a backend and exposing an API that the Electron app will consume". My solution is to have a web service that's running. It doesn't get started by the Electron app. It may not even be on the same machine. There wasn't anything in the original question that I thought implied the Electron app had to start the back end.Bataan
OP stated that he needs to "run/open a external file which is a Golang binary".Miamiami
Despite the downvotes this is a legitimate approach, its common in vscode for example, to launch part of your plugin as a service which then has an electron side plugin which communicates with the other portion via http.Defector

© 2022 - 2024 — McMap. All rights reserved.