Run external applications from Electron


#1

I’m building an Electron app where the user will be able to edit some config files and I would like to allow the user to select the preferred app/editor.

When I do this, I get EACCES error, which stands for “permission denied” even though the owner of the app is the user executing the command.

var child = require('child_process').execFile;

execFile('/Applications/Sublime\ Text.app/', console.log); 

Why does this error occur and how can I overcome it? Also, if I will succeed in opening the editor how can I specify the path of the file I want to open?


#2

Corrected snippet

var execFile = require('child_process').execFile;

execFile('/Applications/Sublime\ Text.app/', console.log);

#3

Use spawn.

import { spawn } from 'child_process'
const cmd = command[0]
const args = command[1]

const p = spawn(cmd, args)
p.stdout.on('data', (data) => {
  console.log('stdout: ' + data)
})

p.stderr.on('data', (data) => {
  console.log('stderr: ' + data)


p.on('close', (code) => {
  console.log('child process exited with code ' + code)
})

#4

As for Sublime, the command would be:
/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl [file]:[line]


#5

Thanks @dperetti!

I was looking for a generic solution but I realize there isn’t one. Each editor receives parameters in it’s own way.