XPCOM create file and write to file in current profile directory

Hello. How do I get path to profile directory using OS.File?
https://developer.mozilla.org/pl/docs/Mozilla/JavaScript_code_modules/OSFile.jsm/OS.File_for_the_main_thread#OS.File.open

Currently:

let file = Components.classes["@mozilla.org/file/directory_service;1"].
           getService(Components.interfaces.nsIProperties).
           get("ProfD", Components.interfaces.nsIFile);

How can I test XPCOM interfaces outside of my addon? I tried using firebug/firefox console but it doesn’t work there. There is something like xpcom shell… but I have to compile firefox on my own.

With OS.File you can get it like this:

Cu.import('resource://gre/modules/osfile.jsm');
var profDir = OS.Path.Constants.profileDir

Easy :slight_smile:

I would avoid XPCOM as much as possible.As that is marked for deprecation.

Stick with osfile.jsm and other stuff you can do from within Workers

1 Like

Thanks that’s easier than I thought. Is it possible to use chrome module outside my extension? For example in firefox console? For example if I’d like to test OS.File directly from the console it would speed up development a lot. Is there an option in firefox that enables it ? Or maybe some console addon.

Yep enable developer preferences -
https://developer.mozilla.org/en-US/Add-ons/Overlay_Extensions/XUL_School/Setting_Up_a_Development_Environment

Then hit ctrl + shift + j (or on mac use command key) then you will see a bar at the bottom. There you can run code in the window context.

You can also use scractchpad to run code in the window context:

https://www.youtube.com/watch?v=oo4STWceGTM

(this video shows you how to enable just the related prefs rather then all the developer preferences as the article above)

1 Like