1

I'm working on a file management system built with node.js and electron.

The file management displays a list of files in a folder and allows the user to run custom commands to batch process the files, such as renaming, custom grouping ect...

However I want to implement a feature that if the user clicks on a file it will open with the text editor of their choice.

I can't get the sublime text editor (or any for that matter) to open with node.js code.

I have looked into other questions here on SO such as "Launch an external application from node.js" and "Is it possible to execute an external program from within node.js?" but none of the answers successfully work for me.

Most of the answers open the file with the "default" program of the operating system which is not what I want. I want to be able to choose the program that opens the file.

I found this code below that calls on global path variables to open external software from node:

const exec = require("child_process").exec
exec('yourApp').unref()

But of course it doesn't work if you replace 'yourApp' with a file path pointing to sublime_text.exe like so:

var exec = require('child_process').exec;
exec('C:/Program Files/Sublime Text 3/sublime_text.exe').unref()

Any help would be very very much appreciated. Thank you

YAHsaves
  • 1,697
  • 12
  • 33
  • "C:/Program Files/Sublime Text 3/sublime_text.exe" I think there is problem with space. can you remove space and try again? (Or use escape sequence) – Harsh Patel Oct 30 '17 at 04:47
  • I tried both, using the escape sequence and removing the spaces but neither worked. This was the command run with escape sequences: "C:/Program\ Files/Sublime\ Text\ 3/sublime_text.exe" – YAHsaves Oct 30 '17 at 04:51
  • copy and paste in windows search bar to open sublime_text.exe file using this path "C:/Program Files/Sublime Text 3/sublime_text.exe". Is it works fine? – Harsh Patel Oct 30 '17 at 04:52
  • 1
    No that doesn't work, but that's because the OS uses "\" to locate directories where to my understanding node uses "/". If I replace the "/" with "\" then it works.... hmmm let me try escape sequencing that. – YAHsaves Oct 30 '17 at 04:55
  • Unfortunately still no go, the javascript string I just tried looks like this: "C:\\Program Files\\Sublime Text 3\\sublime_text.exe" but it didn't run either – YAHsaves Oct 30 '17 at 04:58

2 Answers2

0

You need to use this as your path: C:/Program\ Files/Sublime\ Text\ 3/sublime_text.exe

Marco Principio
  • 475
  • 3
  • 9
  • the spaces need to be escaped, and it should work for you – Marco Principio Oct 30 '17 at 05:00
  • That was similar to what Harsh Patel suggested in the comments, but unfortunately it didn't work. I tried your exact path as well, but no go. If it helps I'm running the "exec("C:/Program\ Files/Sublime\ Text\ 3/sublime_text.exe").unref();" line inside of the "appReady" function of electron. I tried the same thing with pure node.js, but same result no response. – YAHsaves Oct 30 '17 at 05:03
0

I figured it out. It turns out it takes a little bit more code than I first posted to open an external file. The full code looks like this:

// Create a child process
var spawn = require('child_process').spawn;
var child = spawn('Path_To_.exe', ['parameters', 'Path_To_File']);

Also just in case anyone is wondering it was not necessary to escape sequence the spaces within the file name.

Thank you for everyone who offered to help!

YAHsaves
  • 1,697
  • 12
  • 33
  • Also because I didn't clarify it in my answer I wanted to add, if you don't know what "parameters" to add, these are application specific. So if I wanted to run node here I could put "-v" in the parameters to have node print out it's version in the console. However there are no parameters that I need for sublime text so I left this blank and it worked great! – YAHsaves Oct 30 '17 at 05:20