Search code examples
firefox-addonfirefox-addon-sdk

How can I locate a file to use with the system/child_process API within a Firefox add-on?


I would like to write a Firefox add-on that communicates with a locally installed program to exchange data. It looks like this can be done using either js-ctypes or the low-level system/child_process API, with the latter being the recommended solution.

The child_process API appeals because it sends and receives data abstractly over a pipe rather than directly at the C interface level. However, to use it you need (it seems) to supply the full path to the executable within your code:

var child_process = require("sdk/system/child_process");
var ls = child_process.spawn('/bin/ls', ['-lh', '/usr']);

In my case, the executable is installed by another application and we don't know it's exact location - it will differ according to OS, the user's drives, and possibly the user's preference. I imagine this problem will be common to most executables that are not built in to the OS. So my question is: what means do I have to locate the full path of the executable I want to use? I will need to support multiple OSes but presumably could have different solutions for each if needed.

Thanks!


Solution

  • Here's the code I used on Windows - the key was being able to read an environment variable to find the location of the appropriate application folder. After that I assume that my application is stored under a well-known subpath (we don't allow customization of it).

    var system = require("sdk/system");
    var iofile = require('sdk/io/file');
    var child_process = require('sdk/system/child_process');
    
    var progFilesFolder = system.env["programfiles(x86)"],
        targetFile = iofile.join(progFilesFolder, 'FolderName', 'Program.exe');
        targetFileExists = iofile.exists(targetFile);
    
    if (targetFileExists) {
        var p = child_process.spawn(targetFile);
    }
    

    I haven't written the code for Mac yet but I expect it to be similar, with the difference being that there are no drive letters to worry about and the system folders in OS X have standard names (even on localized systems).