How to run an app with arguments in Mac OS X using Firefox Addon SDK Javascript?

I’m trying to write a code that if a certain button is pressed the “Finder” app in Mac OS X is opened in a specific path. I already have the code that works in Windows.
let file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
file.initWithPath(“C:\Windows\explorer.exe”);
let process = Cc["@mozilla.org/process/util;1"].createInstance(Ci.nsIProcess);
process.init(file);
let parameter = “/root,” + MY_SPECIFIC_PATH;
process.run(false, [parameter], 1);

So I tried to write one for Mac OS X.
let file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
file.initWithPath("/System/Library/CoreServices/Finder.app");
let process = Cc["@mozilla.org/process/util;1"].createInstance(Ci.nsIProcess);
process.init(file);
let parameter = “/MY/SPECIFIC/PATH”;
process.run(false, [parameter], 1);

I thought this should work, because the very similar code works for Windows and if you type command below in Mac’s Terminal it will work.
open /system/library/coreservices/Finder.app /MY/SPECIFIC/PATH

But the code doesn’t work in Mac OS X.
Can you help me with this?
Thank you very much in advance.
Also I’m trying to figure out the same thing for Linux Mint.

I would recommend trying child_process instead of nsIProcess and nsIFiles, see here -

https://developer.mozilla.org/en-US/Add-ons/SDK/Low-Level_APIs/system_child_process

I have used open myself, but I used it via popen here - https://github.com/Noitidart/Profilist/blob/master/modules/workers/MainWorker.js#L5419-L5447

But re your actual code. I think what you need to do is this:

file.initWithPath("/bin/sh"); // or you can try `/bin/bash`

Then run that with arguments of:

var pathToLaunch = '/System/Library/CoreServices/Finder.app';
var argsToLaunchWith = ['open', '"' + pathToLaunch + '"'];
process.run(false, argsToLaunchWith, argsToLaunchWith.length);

I’m pretty certain that open needs to be run with bash or sh. If you use child_process you would have to do the same.

The only time you don’t have to use bash or sh is if the file is a binary executable like /Applications/Firefox/MacOS/firefox. A .app is not a binary executable.

Thank you very much noitidart.
I tried this and it didn’t work.
let file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
file.initWithPath("/bin/sh");
let process = Cc["@mozilla.org/process/util;1"].createInstance(Ci.nsIProcess);
process.init(file);
let parameter = [‘open’, ‘"’ + mypath + ‘"’];
process.run(false, [parameter], parameter.length);

Then I tried this and it didn’t work.
let child_process = require(“sdk/system/child_process”);
let cmd = child_process.spawn(’/bin/open’, [mypath]);
cmd.stdout.on(‘data’, function (data) {
console.log('stdout: ’ + data);
});
cmd.stderr.on(‘data’, function (data) {
console.log('stderr: ’ + data);
});
cmd.on(‘close’, function (code) {
console.log('child process exited with code ’ + code);
});

Any idea where I’m going wrong?

With nsIProcess this worked for me:

var file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
file.initWithPath("/bin/bash");
console.log('file:', file);



var exe = file;
var process = Cc['@mozilla.org/process/util;1'].createInstance(Ci.nsIProcess);
process.init(exe);

var obsToLaunchProfAfterCreate = {
    observe: function (aSubject, aTopic, aData) {
        console.info('incoming obsToLaunchProfAfterCreate: aSubject = ' + aSubject + ' | aTopic = ' + aTopic + ' | aData = ' + aData, {
            aSubject: aSubject,
            aTopic: aTopic,
            aData: aData
        });
    }
};

var mypath = '/system/library/coreservices/Finder.app';
var args = ['-c', 'open "' + mypath + '"'];
var a = process.runAsync(args, args.length, obsToLaunchProfAfterCreate);
console.log('a', a)

And with child_process this worked for me:

var child_process = require('sdk/system/child_process');

var mypath = '/system/library/coreservices/Finder.app';
var ls = child_process.spawn('/bin/bash', ['-c', 'open "' + mypath + '"']);
1 Like

Thank you very much noitidart! You are a genius, sir!
Both of the codes you provided works for Mac OS X, and with your help I also figured it out for Linux OS.
Thank you very much again.

1 Like

My pleasure keep up the great cross-platform work! :slight_smile: