Run External Apps using XPCOM Interfaces

I thought it was impossible to run a program outside my XUL app, mainly because of some kind of JavaScript restriction. There are client-side restrictions to protect the file system from being infected from malicious code, however there are some server side solutions, but most of them use ActiveX. Is it possible to run a program in JavaScript? Even a batch file?

Here’s my problem, basically Im using a program called WiX, to create an Application Packager, I create an XML based file (.wxs), and compile it through a program called candle.exe taking it in as an argument.

C:\>candle.exe filename.wxs
C:\>light.exe filename.wixobj

Candle will return a new file with the extension .wixobj, which must then go through a linking program called light.exe in the same fashion. I’m using XUL (Mozilla) to create a program which will pretty much automate most of the tasks, XUL uses JavaScript, can JavaScript run external commands?

After some chatting on IRC, I was directed to the nsIProcess interface, and nsILocalFile.launch(). I actually ended up trying both, however nsIProcess is actually the recommend way to go about this problem, mainly because nsILocalFile.launch() may not work on all platforms.

This is how I used nsIProcess:

function callLight()
var getWorkingDir= Components.classes[“;1”]

.get(“CurProcD”, Components.interfaces.nsIFile);

var lFile = Components.classes[“;1”]

var lPath = getWorkingDir.path + “\\wix-2.0\\light.exe”

var process = Components.classes[“;1”]
var wxsPath = inputPath + “\\” + getAppName() + “.wixobj”;
var args = [wxsPath];, args, args.length);

getWorkingDir – is of type nsIProperties, and the “CurProcD” basically gets my current working directory, which for my reason is where my wix source files reside

lFile – is of type nsILocalFile, and will hold the file, in my case it will be either candle.exe or light.exe

  • lPath – holds the path to the file which lFile will contain
  • lFile.initWithPath(path) – initalized the nsILocalFile with the parameterized path

process – is of type nsIProcess, is the interface which will represent an executable process

  • process.init(nsILocalFile) – initializes the selected nsILocalFile to a process
  •, args, arg_count) – Runs the process
    • blocking – is a boolean qualifier, if true will wait for the process to complete before continuing with the program, and if false will continue and let the process run by itself
    • args – is a string array of arguments which may be passed to the executing process
    • arg_count – is the number of arguments being passed

It worked perfectly, the only reason I used the nsILocalFile.launch() was that I didn’t realize that the output file (.wixobj) would end up in the directory which I was executing my XRap program, and not where the source (.wxs) was located. So I had to create a batch file, and use launch() to execute it. It was more code than needed, and created extra files, nsIProcess is much better to work with.

MDC – Running Applications

…Happy Processing!

4 thoughts on “Run External Apps using XPCOM Interfaces

  1. James Boston

    Do candle.exe or light.exe take parameters that allow you to specify where the output should go? I’m sure they must. That might help you to be certain that your script works on all platforms and cut down on the code. Although, if this is a Windows only thing, then nsILocalFile.launch() is fine. In fact, it’s the most reliable way (only way?) on windows to launch batch files.

    Other than CurProcD, there are some other directories that you can locate and use as an output directory:

  2. Mark Finkle

    Usin strings to build paths is not considered a “best practice” and is not portable between platforms”

    getWorkingDir.path + “\\wix-2.0\\light.exe”

    Typically, you clone a file from the folder and append, like this:

    var file = getWorkingDir.clone();

    Also, using the .path protperty to create a nsILocalFile is not needed in this case, as the file should support it. Just QueryInterface to nsILocalFile:


    Then pass “file” to the “process.init”

  3. Joshua

    Re: James Boston
    David Humphrey
    candle.exe and light.exe only take in one parameter, which is the object they will be compiling or linking. Its true that candle.exe and light.exe will only work on the Windows platform, which is why I used nsIProcess, it does the work of creating a batch file, without having to create one.


Leave a Reply

Your email address will not be published. Required fields are marked *