4

Trying to write a file inside windows temp directory using XUL code:

function writeFile_launch_application(utility_name,utility_path)
{
var data ='tasklist  /nh /fi "imagename eq '+utility_name+'" | find /i "'+utility_name+'">nul && (echo alerady running) || ("'+utility_path+'")';
//alert(data);
var file = Cc["@mozilla.org/file/directory_service;1"].
       getService(Ci.nsIProperties).
       get("TmpD", Ci.nsIFile);
        file.append("launch_application.bat");
        file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);

        // Then, we need an output stream to our output file.
        var ostream = Cc["@mozilla.org/network/file-output-stream;1"].
                      createInstance(Ci.nsIFileOutputStream);
        ostream.init(file, -1, -1, 0);

        // Finally, we need an input stream to take data from.
        const TEST_DATA = data;
        let istream = Cc["@mozilla.org/io/string-input-stream;1"].
                      createInstance(Ci.nsIStringInputStream);
        istream.setData(TEST_DATA, TEST_DATA.length);

        NetUtil.asyncCopy(istream, ostream, function(aResult) {
          if (!Components.isSuccessCode(aResult)) {
            // an error occurred!
          }
        })
}

But getting error:

Timestamp: 11/29/2012 11:03:09 PM
Error: ReferenceError: Cc is not defined
Source File: chrome://myaddon/content/overlay.js
Line: 199

I also tried to add below lines at the top of my code but it didn't solve above error:

Components.utils.import("resource://gre/modules/NetUtil.jsm");

Components.utils.import("resource://gre/modules/FileUtils.jsm");
therealjeffg
  • 5,790
  • 1
  • 23
  • 24
user1862780
  • 51
  • 1
  • 5

2 Answers2

1

Cc and Ci are aliases for Components.classes and Components.interfaces respectively.

Depending on the context they might (or might not) be already defined.

In any case

const Cc = Components.classes;
const Ci = Components.interfaces;
const Cu = Components.utils;
const Cr = Components.results;

(you shouldn't tag your question as firefox-addon-sdk)

paa
  • 5,048
  • 1
  • 18
  • 22
0

When I'm developing my addon, I always try to replace Components.utils, Components.intefaces with Cu and Ci. To do that, first line in my file is:

const { Cc, Ci, Cu } = require('chrome');

Cc is Component.classes. Than you can use now

Cu.import("resource://gre/modules/FileUtils.jsm");
Cu.import("resource://gre/modules/NetUtil.jsm");
Tomasz Dzięcielewski
  • 3,829
  • 4
  • 33
  • 47