Visit Mozilla.org

Code snippets:Running applications

From MDC


This page describes how to run other programs from your chrome JavaScript code, using Mozilla XPCOM interfaces. There are two ways to run programs. The first is to use nsILocalFile:launch method, the second is to use nsIProcess interface.

[edit] Using nsILocalFile.launch()

This method has the same effect as if you double-clicked the file, so for executable files—it will just run the file without any parameters. It may not be implemented on some platforms, so make sure your this method is implemented on your target platforms.

For more information on nsIFile/nsILocalFile, see File I/O.

var file = Components.classes["@mozilla.org/file/local;1"]
                     .createInstance(Components.interfaces.nsILocalFile);
file.initWithPath("c:\\myapp.exe");
file.launch();

[edit] Using nsIProcess

The recommended way is to use the nsIProcess interface:

// create an nsILocalFile for the executable
var file = Components.classes["@mozilla.org/file/local;1"]
                     .createInstance(Components.interfaces.nsILocalFile);
file.initWithPath("c:\\myapp.exe");

// create an nsIProcess
var process = Components.classes["@mozilla.org/process/util;1"]
                        .createInstance(Components.interfaces.nsIProcess);
process.init(file);

// Run the process.
// If first param is true, calling thread will be blocked until
// called process terminates.
// Second and third params are used to pass command-line arguments
// to the process.
var args = ["argument1", "argument2"];
process.run(false, args, args.length);

[edit] References