Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

open a file with default program in node-webkit

I want to give the user any option he want to edit a file, how can I open a file with the default program of the specific file type? I need it to work with Windows and Linux but Mac option would be great too.

like image 723
InvisibleUn1corn Avatar asked Apr 27 '15 17:04

InvisibleUn1corn


People also ask

How do I run a program in node JS?

The usual way to run a Node. js program is to run the globally available node command (once you install Node. js) and pass the name of the file you want to execute. While running the command, make sure you are in the same directory which contains the app.


2 Answers

as PSkocik said, first detect the platform and get the command line :

function getCommandLine() {
   switch (process.platform) { 
      case 'darwin' : return 'open';
      case 'win32' : return 'start';
      case 'win64' : return 'start';
      default : return 'xdg-open';
   }
}

second , execute the command line followed by the path

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

exec(getCommandLine() + ' ' + filePath);
like image 71
Khalid Avatar answered Oct 10 '22 01:10

Khalid


For file on a disk:

var nwGui = require('nw.gui');
nwGui.Shell.openItem("/path/to/my/file");

For remote files (eg web page):

var nwGui = require('nw.gui');
nwGui.Shell.openExternal("http://google.com/");
like image 31
Pavel Evstigneev Avatar answered Oct 10 '22 01:10

Pavel Evstigneev