tinker
tinker

Reputation: 925

Getting your extension's folder

I am building a firefox add-on. But i am unable to get the extension's folder using nsIFile .

I tried

var MY_ID = "[email protected]";
var em = Components.classes["@mozilla.org/extensions/manager;1"].
         getService(Components.interfaces.nsIExtensionManager);
// the path may use forward slash ("/") as the delimiter
// returns nsIFile for the extension's install.rdf
var file = em.getInstallLocation(MY_ID).getItemFile(MY_ID, "install.rdf");
var filestring = file.path;

and

var componentFile = __LOCATION__;
var componentsDir = componentFile.parent;
var extensionDir = componentsDir.parent;

but either of them are not working. Is there any other method to read the extension's directory that returns nsIFile ??

Upvotes: 1

Views: 225

Answers (1)

Noitidart
Noitidart

Reputation: 37228

The install path of your extension in a bootstrap addon is held in aData on startup.

https://gist.github.com/Noitidart/9026493

var globalPathHolder;
function startup(aData, aReason) {
globalPathHolder = aData.installPath.path
}

now if you aren't doing bootstrap addon you can use directory service like this:

alert(Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties).get("CurProcD", Ci.nsIFile).path)

where CurProcD is the one for addon path i forget what it is but you can find it in the "Getting special files" area here: https://developer.mozilla.org/en-US/Add-ons/Code_snippets/File_I_O?redirectlocale=en-US&redirectslug=Code_snippets%2FFile_I_O#Getting_special_files

click on the mxr links it has more key words.

You can use the same keyword with FileUtils:

Components.utils.import("resource://gre/modules/FileUtils.jsm");
var exefile = FileUtils.getFile("CurProcD", [])
alert(exefile.path)

Upvotes: 1

Related Questions