Assigning the value of a JS variable to an iMacros variable

Discussions and Tech Support specific to the iMacros Firefox add-on.
Forum rules
Before asking a question or reporting an issue:
1. Please review the list of FAQ's.
2. Use the search box (at the top of each forum page) to see if a similar problem or question has already been addressed.
3. Try searching the iMacros Wiki - it contains the complete iMacros reference as well as plenty of samples and tutorials.
4. We can respond much faster to your posts if you include the following information: CLICK HERE FOR IMPORTANT INFORMATION TO INCLUDE IN YOUR POST
Post Reply
chawin
Posts: 7
Joined: Wed Oct 13, 2010 8:57 am

Assigning the value of a JS variable to an iMacros variable

Post by chawin » Wed Dec 15, 2010 10:38 am

Hello,

I have a problem that there's no method to save data to local by JS, so I want to assign the value of a JS variable to an iMacros variable,then save data to local by iMacros command.I know the command iimSet(),but its syntax :
int ret_code = iimSet ( String VARNAME, String VARVALUE )
e.g.
iimSet("ABC", "hello")

If there are lots of characters included in the value of a variable, need I type them one by one manually when I use iimSet()?And if data generated dynamically, @#$%^& :shock: HELP!!!

Any suggestions will be appreciated!
siniy
Posts: 118
Joined: Sat Nov 07, 2009 7:44 pm

Re: Assigning the value of a JS variable to an iMacros varia

Post by siniy » Thu Dec 16, 2010 4:59 pm

You can read and write files using JS in firefox.

Code: Select all


writeToFile("c:\\test.txt", "Hi");

function writeToFile(filename, data) {
    try {
        netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
    } catch (e) {
        alert("Permission to save file was denied.");
        return 0;
    }
    var file = Components.classes["@mozilla.org/file/local;1"].
               createInstance(Components.interfaces.nsILocalFile);
    var outputStream = Components.classes["@mozilla.org/network/file-output-stream;1"].
                       createInstance(Components.interfaces.nsIFileOutputStream);
    var converter = Components.classes["@mozilla.org/intl/converter-output-stream;1"].
                    createInstance(Components.interfaces.nsIConverterOutputStream);
    file.initWithPath( filename );
    if (!file.exists())
        file.create(0x00, 0644);
		//create,truncate, write
        //outputStream.init( file,  0x02 | 0x08 | 0x20, 0666, 0 );
		//append to file
        outputStream.init( file, 0x02|0x10, 0644, 0 );
    // write BOM first, then converted data.
    //outputStream.write('\u00EF\u00BB\u00BF', 3);
    converter.init(outputStream, "UTF-8", 0, 0);
    converter.writeString(data);
    converter.close(); // this closes outputStream also
}
//--------------------------------

//Saving file content to array "lines";
lines=readFromFile("c:\\test.txt").split("\r\n");
alert(lines);

function readFromFile(filename) {
    try {
        netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
    } catch (e) {
        alert("Permission to read file was denied.");
    }
    var file = Components.classes["@mozilla.org/file/local;1"]
               .createInstance(Components.interfaces.nsILocalFile);
    file.initWithPath( filename );
    if ( file.exists() == false ) {
        alert("File does not exist");
    }
    var is = Components.classes["@mozilla.org/network/file-input-stream;1"]
             .createInstance( Components.interfaces.nsIFileInputStream );
    is.init( file,0x01, 00004, null);
    var sis = Components.classes["@mozilla.org/scriptableinputstream;1"]
              .createInstance( Components.interfaces.nsIScriptableInputStream );
    sis.init( is );
    var output = sis.read( sis.available() );
    return output;
}
chawin
Posts: 7
Joined: Wed Oct 13, 2010 8:57 am

Re: Assigning the value of a JS variable to an iMacros varia

Post by chawin » Fri Dec 17, 2010 10:10 am

Thanks,siniy.You did me a big favor! :D
Post Reply