MozillaZine

Init nsILocalFile How

Talk about add-ons and extension development.
nickhere
 
Posts: 15
Joined: September 29th, 2009, 1:57 pm

Post Posted October 6th, 2018, 11:09 am

I am trying something like this
Code: Select all
Components.utils.import("resource://gre/modules/FileUtils.jsm");
Components.utils.import("resource://gre/modules/NetUtil.jsm");
Components.utils.import("resource://gre/modules/osfile.jsm")


    function writeFile(nsiFile, data, overwrite, callback) {
        //overwrite is true false, if false then it appends
        //nsiFile must be nsiFile
        if (overwrite) {
            var openFlags = FileUtils.MODE_WRONLY | FileUtils.MODE_CREATE | FileUtils.MODE_TRUNCATE;
        } else {
            var openFlags = FileUtils.MODE_WRONLY | FileUtils.MODE_CREATE | FileUtils.MODE_APPEND;
        }
       //data is data you want to write to file
       //if file doesnt exist it is created
       var ostream = FileUtils.openFileOutputStream(nsiFile, openFlags)
     
      var converter = Cc['@mozilla.org/intl/scriptableunicodeconverter'].createInstance(Ci.nsIScriptableUnicodeConverter);
       converter.charset = 'UTF-8';
       var istream = converter.convertToInputStream(data);
       // The last argument (the callback) is optional.
       NetUtil.asyncCopy(istream, ostream, function (status) {
          if (!Components.isSuccessCode(status)) {
             // Handle error!
             Cu.reportError('error on write isSuccessCode = ' + status);
             callback(status);
             return;
          }
          // Data has been written to the file.
          callback(status)
       });
    }

var acctMgr = Components.classes["@mozilla.org/messenger/account-manager;1"].getService(Components.interfaces.nsIMsgAccountManager);
var accounts = acctMgr.accounts;
for (var i = 0; i < accounts.length; i++) {
  var account = accounts.queryElementAt(i, Components.interfaces.nsIMsgAccount);
  //console.log(account.incomingServer.prettyName);
  var localFile = Components.classes["@mozilla.org/file/local;1"]createInstance(Components.interfaces.nsILocalFile);
  localFile.init("d:\\ttt.txt");
  writeFile(localFile,"textit",true);
     }




morat
 
Posts: 2867
Joined: February 3rd, 2009, 6:29 pm

Post Posted October 6th, 2018, 5:49 pm

Removed interfaces in Thunderbird 57:

* nsILocalFile -- replacement: nsIFile

Addons Guide for Thunderbird 57 to Thunderbird 60
http://wiki.mozilla.org/Thunderbird/Add-ons_Guide_57

e.g.

Code: Select all
var file = Components.classes["@mozilla.org/file/local;1"].
  createInstance(Components.interfaces.nsIFile);
file.initWithPath("C:\\Windows\\System32\\notepad.exe");
file.launch();

Return to Extension Development


Who is online

Users browsing this forum: No registered users and 2 guests