Open the default program file in node -webkit

I want to give the user any parameter that he wants to edit, how to open a file using the default program for a specific file type? I need this to work with Windows and Linux, but the Mac version will be great too.

+7
javascript linux windows node-webkit
source share
4 answers

as PSkocik said, first define 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, run the command line followed by the path

 var sys = require('sys'); var exec = require('child_process').exec; exec(getCommandLine() + ' ' + filePath); 
+9
source share

For a file on disk:

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

For deleted files (e.g. web pages):

 var nwGui = require('nw.gui'); nwGui.Shell.openExternal("http://google.com/"); 
+5
source share

Platform Discovery and Usage:

  • 'start' on windows
  • 'open' on mac
  • 'xdg-open' on Linux
+2
source share

I'm not sure that it started to work, as on previous versions of Windows, however, in Windows 10 it does not work, as indicated in the answer. The first argument is the name of the window.

In addition, the behavior between windows and linux is different. Windows "start" will execute and exit, under Linux, xdg-open will wait.

It was a feature that ended up working for me on both platforms in a similar way:

  function getCommandLine() { switch(process.platform) { case 'darwin' : return 'open'; default: return 'xdg-open'; } } function openFileWithDefaultApp(file) { /^win/.test(process.platform) ? require("child_process").exec('start "" "' + file + '"') : require("child_process").spawn(getCommandLine(), [file], {detached: true, stdio: 'ignore'}).unref(); } 
0
source share

All Articles