1

I am using NodeJS/Electron for a desktop app.

I want to open a file with default OS program. For example open a word doc with ms word. But I need a callback when ms word closes so I can consider changes done in doc.

I can open the word document alright but I am having problem with callback which I need when ms word is closed.

var filePath = 'C:\Users\test.doc';
//
function getCommandLine() {
   switch (process.platform) { 
      case 'darwin' : return 'open';
      case 'win32' : return 'start';
      case 'win64' : return 'start';
      default : return 'xdg-open';
   }
}

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

 var child = exec(getCommandLine() + ' ' + filePath, function (error, stdout, stderr) {
   if (error) {
     console.error(`exec error: ${error}`);
     return;
   } 
 });
 child.on('close', function (code) {
   console.log(code);
   // this just triggers as soon as ms word is opened
 });
django
  • 2,809
  • 5
  • 47
  • 80
  • 1
    For Windows, have you tried adding the `/wait` argument for `start`? – mscdex Aug 04 '16 at 17:22
  • @mscdex : thank you for the suggestion. It works. But can you please explain what I can do for other platforms specially linux ? Is there more preferred way of doing it as Electron does provide ```shell.openItem(filePath);``` – django Aug 04 '16 at 18:04
  • 1
    I believe `xdg-open` should wait by default. For OS X, you can add `-W` to wait (or `-Wn` to both wait and force a new instance to open, even if there is an existing window open for that same app). – mscdex Aug 04 '16 at 19:35
  • duplicate: https://stackoverflow.com/questions/30381450/open-external-file-with-electron – lacy May 13 '19 at 21:12

0 Answers0