Here are some useful functions I wrote or found on the web.
Suggestions are welcome :)

Chrome url to file path :

function chromeToPath (aChromePath) {
   if (!aChromePath) || !(/^chrome:/.test(aChromePath))))
      return; //not a chrome url
   var rv;
   var ios = Components.classes[';1'].getService(Components.interfaces["nsIIOService"]);
   var uri = ios.newURI(aPath, "UTF-8", null);
   var cr = Components.classes[';1'].getService(Components.interfaces["nsIChromeRegistry"]);
   rv = cr.convertChromeURL(uri).spec;
   if (/^file:/.test(rv))
      rv = this.urlToPath(rv);
      rv = this.urlToPath("file://"+rv);
   return rv;

URL to file path :

function urlToPath (aURL) {
   if (!aURL || !/^file:/.test(aURL))
      return ;
   var rv;
   var ph = Components.classes[";1?name=file"]
    rv = ph.getFileFromURLSpec(aPath).path;
    return rv;

Get a nsILocalFile’s URL :

function pathToUrl (aFile) {
   //aFile must be an instance of nsILocalFile
   var rv;
   var ph = Components.classes[";1?name=file"]
    rv = ph.getURLSpecFromFile(aFile);
    return rv;

Check if file exists :

function fileExists(aPath){
  try {'UniversalXPConnect');
    var file = Components.classes[";1"]
    return file.exists();
  } catch(ex) {
    return false;

Quit function :

function quit (aForceQuit) {'UniversalXPConnect');
  var appStartup = Components.classes[';1'].
  // eAttemptQuit will try to close each XUL window, but the XUL window can cancel the quit
  // process if there is unsaved data. eForceQuit will quit no matter what.
  var quitSeverity = aForceQuit ? Components.interfaces.nsIAppStartup.eForceQuit :