open a file with default program in node-webkit

前端 未结 5 982
刺人心
刺人心 2020-12-28 21:59

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

5条回答
  •  自闭症患者
    2020-12-28 22:56

    I am not sure if start used to work as is on earlier windows versions, however on windows 10 it doesn't work as indicated in the answer. It's first argument is the title of the window.

    Furthermore the behavior between windows and linux is different. Windows "start" will exec and exit, under linux, xdg-open will wait.

    This was the function that eventually worked for me on both platforms in a similar manner:

      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(); 
      }
    

提交回复
热议问题