Madhawa Priyashantha
Madhawa Priyashantha

Reputation: 9872

Firefox open file from add-on instead of local path

I am creating Firefox add-on which run a specific .bat file.

This is code I used.

main.js

const {Components, Cc, Ci,Cu} = require("chrome");

var file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
file.initWithPath("C:\\Users\\Madhawa.se\\Desktop\\snap\\shoot.bat"); // i want to open this bat from xpi .for example from data directory
var args="this is an argument  ";
var process = Cc["@mozilla.org/process/util;1"].createInstance(Ci.nsIProcess);
process.init(file);
process.run(false, args, args.length);

It works fine but I would like to open shoot.bat file from inside of add-on so I can easily distribute add-on as a single xpi.

How can I open this bat file from add-on for example I can put shoot.bat file inside of data directory of my Firefox add-on, but how can I open it ? isn't it possible? is there any better way to achieve this ?

Upvotes: 1

Views: 264

Answers (1)

nmaier
nmaier

Reputation: 33162

While it is reasonably easy to read data from within your add-on XPI, you cannot create processes that way. Windows in general, and the windows executable loader in particular, do not know how to read .xpi.

Executing a batch file, or it's contents, might be achieved by either writing the batch file somewhere on the disk prior to execution, or assembling a cmd /K compatible argument string with the contents of that batch file

Reading the batch file in the first place can be achieved e.g. by using the request module together with a data.url().

const {Request} = require("sdk/request");
const {data} = require("sdk/self");

Request({url: data.url("shoot.bat"), onComplete: function(response) {
  var batch = response.text;
  // ...
}});

Of course, it might be easier just to put the contents of the batch file in the Javascript source itself to avoid the Request stuff altogether.

Now, you can either write the batch file contents to a file, e.g. using io/file and/or OS.File (the latter is preferred at the moment, mainly because io/file is not async), and execute that.

Or you can properly format the contents to be used with cmd /K.

Upvotes: 4

Related Questions