0

I'm using Node Webkit component to wrap my html5 application. In this application I need to run CMD or other (exe) files like "customApplication.exe" and pass some arguments.

I'm really hopeless in this matter. I would be really glad if you could help me on this. It's very important and I don't want to change my component and use Awesomium or other compnents.

Thank you very much in advance.

Hirad Nikoo
  • 1,599
  • 16
  • 26

2 Answers2

7

I hope this example code will help. (It's taken from my recent eBook introduction to node-webkit.) You would need to set the variable "filePath" to the full path of "customApplication.exe"

 var execFile = require 
    ('child_process').execFile, child;

 child = execFile(filePath,
       function(error,stdout,stderr) { 
    if (error) {
      console.log(error.stack); 
      console.log('Error code: '+ error.code); 
      console.log('Signal received: '+ 
             error.signal);
      } 
    console.log('Child Process stdout: '+ stdout);
    console.log('Child Process stderr: '+ stderr);
  }); 
  child.on('exit', function (code) { 
    console.log('Child process exited '+
        'with exit code '+ code);
  });

Best of luck,

Jonathan Dodd

"Windows Desktop App Creation with node-webkit"

Jonathan
  • 239
  • 1
  • 2
  • 6
  • 2
    Thank you very much. This works like a charm but the problem is when I want to pass an command argument this doesn't work. I passed my arguments in the next parameter "execFile(filePath, args, callback)", that didn't work eather. So I tried "exec" instead of "execFile" and put space between the file name and the command arguments and that worked really great. If you could show me how to pass arguments correctly I would choose your answer as the ultimate answer. As you understand I solved my problem with the solution that I told you before but I want more accurate answer here for other people. – Hirad Nikoo Aug 13 '13 at 04:44
  • 1
    Glad you solved your problem. With execFile the arguments are passed as an Array List of strings, whereas exec takes (as the first, "command" argument) the command to run with space-separated arguments. So, you can see why exec with spaced arguments worked, but execFile didn't -- it wanted [args] . The details are shown in the child_process node document at [link]http://nodejs.org/api/child_process.html – Jonathan Aug 13 '13 at 15:12
  • @Jonathan - Where can i grab your book? =) – Havihavi Feb 14 '16 at 11:57
1

I've created a project called UGUI that is all about creating GUI's for CLI applications using NW.js. You can borrow from either my "runcmd" or "runcmdAdvanced" functions to do this very easily, or just use the ugui.js library (which will make your life much easier).

Easy peasy mac'n'cheesy.

UGUI is abstracting out the Node.js way of doing things to make it simpler, for more information on what Node is doing under the hood:

Jaredcheeda
  • 1,657
  • 17
  • 15