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['@mozilla.org/network/io-service;1'].getService(Components.interfaces["nsIIOService"]); var uri = ios.newURI(aPath, "UTF-8", null); var cr = Components.classes['@mozilla.org/chrome/chrome-registry;1'].getService(Components.interfaces["nsIChromeRegistry"]); rv = cr.convertChromeURL(uri).spec; if (/^file:/.test(rv)) rv = this.urlToPath(rv); else 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["@mozilla.org/network/protocol;1?name=file"] .createInstance(Components.interfaces.nsIFileProtocolHandler); 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["@mozilla.org/network/protocol;1?name=file"] .createInstance(Components.interfaces.nsIFileProtocolHandler); rv = ph.getURLSpecFromFile(aFile); return rv; }
Check if file exists :
function fileExists(aPath){ try { netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect'); var file = Components.classes["@mozilla.org/file/local;1"] .createInstance(Components.interfaces.nsILocalFile); file.initWithPath(aPath); return file.exists(); } catch(ex) { return false; } }
Quit function :
function quit (aForceQuit) { netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect'); var appStartup = Components.classes['@mozilla.org/toolkit/app-startup;1']. getService(Components.interfaces.nsIAppStartup); // 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 : Components.interfaces.nsIAppStartup.eAttemptQuit; appStartup.quit(quitSeverity); }
Commentaires
Ecrire un commentaire Trackback