output data

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
nader
Posts: 4
Joined: Tue Jul 29, 2008 8:36 am

output data

Post by nader » Wed Aug 06, 2008 9:37 am

Hello,

I would like to create a file or write into an existing file using javascript once my macro is done. Is that possible ?

The reason why I want to do so, is I'm running another program in parrallel and that needs the macro to be done in order to do some other task.

Thanks in advance!

cheers
josephconlin
Posts: 190
Joined: Wed Aug 06, 2008 2:38 am

Re: output data

Post by josephconlin » Wed Aug 06, 2008 7:24 pm

I was looking to do something similar, and I found out that javascript can use Java to write a file. I don't know how familiar you are with Java, but here's what I'm doing. I borrowed heavily from another post where someone was reading in a file by using Java, btw.

Code: Select all

//Function to write the file
function writeFile(filename, data)
{
	try
	{
		//write the data
		var out = new java.io.BufferedWriter(new java.io.FileWriter(filename));
		out.write(data);
		out.close();
		out=null;
	}
	catch(e)	//catch and report any errors
	{
		alert(""+e);
	}
}
nader
Posts: 4
Joined: Tue Jul 29, 2008 8:36 am

Re: output data

Post by nader » Thu Aug 07, 2008 7:09 am

Thanks for tip Joseph !
hajo
Posts: 2
Joined: Mon Oct 20, 2008 2:35 pm

Re: output data

Post by hajo » Mon Oct 20, 2008 2:41 pm

If you don't want to rely on Java you can try this:

Code: Select all

/* Code from http://www.captain.at/programming/xul/ */
/* Filename: writeToFile.js */

writeToFile("C:\\myFile.txt","Hello World!!\r\nThis is the second line...\r\n");


function writeToFile(filename, data) {
	try {
		netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
	} catch (e) {
		alert("Permission to save file was denied.");
	}
	var file = Components.classes["@mozilla.org/file/local;1"]
		.createInstance(Components.interfaces.nsILocalFile);
	file.initWithPath( filename );
	if ( file.exists() == false ) {
		//alert( "Creating file... " );
		file.create( Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 420 );
	}
	var outputStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
		.createInstance( Components.interfaces.nsIFileOutputStream );

	// Open flags 
	var PR_RDONLY  =     0x01
	var PR_WRONLY  =     0x02
	var PR_RDWR    =     0x04
	var PR_CREATE_FILE = 0x08
	var PR_APPEND  =     0x10
	var PR_TRUNCATE=     0x20
	var PR_SYNC    =     0x40
	var PR_EXCL    =     0x80

	/*
	** File modes ....
	**
	** CAVEAT: 'mode' is currently only applicable on UNIX platforms.
	** The 'mode' argument may be ignored by PR_Open on other platforms.
	**
	**   00400   Read by owner.
	**   00200   Write by owner.
	**   00100   Execute (search if a directory) by owner.
	**   00040   Read by group.
	**   00020   Write by group.
	**   00010   Execute by group.
	**   00004   Read by others.
	**   00002   Write by others
	**   00001   Execute by others.
	**
	*/

	outputStream.init( file, PR_RDWR | PR_CREATE_FILE | PR_APPEND, 420, 0 );
	var result = outputStream.write( data, data.length );
	outputStream.close();
}
josephconlin
Posts: 190
Joined: Wed Aug 06, 2008 2:38 am

Re: output data

Post by josephconlin » Thu Dec 11, 2008 8:12 pm

Note that the Java solution doesn't work with the new Java Plugin that ships with Java 1.6.0_10 and up (security exception due to permissions for writing a file). For now, they are shipping both the old and new plugin so that you can choose (on Windows, via the Java Control Panel) which plugin to use. It still works in the old plugin. I haven't been able to discover how to make it work with the new plugin yet, although there might be some help coming in Java 1.6.0_12 that might require the iMacros extension to make some changes. How's that for vague? :D

Hajo, I was unable to get your example to work for me when I ran into the Java plugin problem. I copied your function into my existing Javascript file and then tried to call it, but the script would just stop running on either the enablePrivilege function or the createInstance function (sorry I don't recall, it was a few days ago). Has anyone else had any luck using the non-Java approach?
Last edited by josephconlin on Thu Dec 18, 2008 11:19 pm, edited 1 time in total.
crfutu
Posts: 10
Joined: Fri Oct 17, 2008 8:08 am

Re: output data

Post by crfutu » Thu Dec 18, 2008 2:59 am

but how can I read data?
And can I output data to a .ini file.
josephconlin
Posts: 190
Joined: Wed Aug 06, 2008 2:38 am

Re: output data

Post by josephconlin » Thu Dec 18, 2008 11:17 pm

crfutu wrote:but how can I read data?
And can I output data to a .ini file.
Here is the link to the post where I found the Java method of reading in a file that I used to create the Java version of writing out a file shown above. You'll find the Java function in a post by joe_brown.

http://forum.imacros.net/viewtopic.php?f=11&t=3267

So, as long as you're using the older Java plugin to avoid the problem I mention above as well, you could use Java inside a javascript macro to read in any file you want and to write out any file you want as well. Of course, you'll need to know how to use Java if you want to do anything beyond the basic reading and writing that are demoed in these posts.
logrd
Posts: 1
Joined: Wed Dec 17, 2008 9:47 pm

Re: output data

Post by logrd » Thu Dec 18, 2008 11:23 pm

Hello

I have a problem when I run the exemple Javascript :

//Function to write the file
function writeFile(filename, data)
{
try
{
//write the data
var out = new java.io.BufferedWriter(new java.io.FileWriter(filename));
out.write(data);
out.close();
out=null;
}
catch(e) //catch and report any errors
{
alert(""+e);
}
}

The Javascript stop after the first "{" (after the line function writeFile(filename, data))

Why ?

Thanks
hajo
Posts: 2
Joined: Mon Oct 20, 2008 2:35 pm

Re: output data

Post by hajo » Fri Dec 19, 2008 12:06 pm

It seems like Vista has different permissions than XP so I change my example to this:

Code: Select all

/* Code from http://www.captain.at/programming/xul/ */
/* Filename: writeToFile.js */

writeToFile("C:\\Temp\\myFile.txt","Hello World!!\r\nThis is the second line...\r\n");


function writeToFile(filename, data) {
   try {
      netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
   } catch (e) {
      alert("Permission to save file was denied.");
   }
   var file = Components.classes["@mozilla.org/file/local;1"]
      .createInstance(Components.interfaces.nsILocalFile);
   file.initWithPath( filename );
   if ( file.exists() == false ) {
      alert( "Creating file... " + filename );
      file.create( Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 420 );
   }
   var outputStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
      .createInstance( Components.interfaces.nsIFileOutputStream );

   // Open flags
   var PR_RDONLY  =     0x01
   var PR_WRONLY  =     0x02
   var PR_RDWR    =     0x04
   var PR_CREATE_FILE = 0x08
   var PR_APPEND  =     0x10
   var PR_TRUNCATE=     0x20
   var PR_SYNC    =     0x40
   var PR_EXCL    =     0x80

   /*
   ** File modes ....
   **
   ** CAVEAT: 'mode' is currently only applicable on UNIX platforms.
   ** The 'mode' argument may be ignored by PR_Open on other platforms.
   **
   **   00400   Read by owner.
   **   00200   Write by owner.
   **   00100   Execute (search if a directory) by owner.
   **   00040   Read by group.
   **   00020   Write by group.
   **   00010   Execute by group.
   **   00004   Read by others.
   **   00002   Write by others
   **   00001   Execute by others.
   **
   */

   outputStream.init( file, PR_RDWR | PR_CREATE_FILE | PR_APPEND, 420, 0 );
   var result = outputStream.write( data, data.length );
   outputStream.close();
}
josephconlin
Posts: 190
Joined: Wed Aug 06, 2008 2:38 am

Re: output data

Post by josephconlin » Fri Dec 19, 2008 7:45 pm

logrd wrote:Hello

I have a problem when I run the exemple Javascript :

//Function to write the file
function writeFile(filename, data)
{
try
{
//write the data
var out = new java.io.BufferedWriter(new java.io.FileWriter(filename));
out.write(data);
out.close();
out=null;
}
catch(e) //catch and report any errors
{
alert(""+e);
}
}

The Javascript stop after the first "{" (after the line function writeFile(filename, data))

Why ?

Thanks
The example is a javascript function that will call Java to write a file when you give it a filename and some data to write. Your script needs to include some text that calls the function telling it to write something. This is more a basic programming thing than an iMacros thing.

Borrowing from hajo's posts, here's what a macro that calls the function could look like.

Code: Select all

//The next line calls the function to write data with a file name and some data.  The filename and data are separated by a comma.
writeFile("C:\\Temp\\myFile.txt","Hello World!!\r\nThis is the second line...\r\n");

//Function to write the file
function writeFile(filename, data)
{
   try
   {
      //write the data
      var out = new java.io.BufferedWriter(new java.io.FileWriter(filename));
      out.write(data);
      out.close();
      out=null;
   }
   catch(e)   //catch and report any errors
   {
      alert(""+e);
   }
}
josephconlin
Posts: 190
Joined: Wed Aug 06, 2008 2:38 am

Re: output data

Post by josephconlin » Fri Dec 19, 2008 8:12 pm

hajo wrote:It seems like Vista has different permissions than XP so I change my example to this:
Thanks for the update, hajo, although the changes you made (different directory to save to and uncommenting an alert) don't really affect anything. :D I'm using XP, but some of my macros run on Vista machines too.

That being said, I got both of your examples to work for me on the XP machine. My problem was that I was sending in the filename with a path that contained slashes, ie C:/bin/test.txt, and it appears that doing so is not supported. When I changed those to double backslashes, ie C:\\bin\\test.txt, it worked correctly. Thanks for the follow up! :P
klesk
Posts: 2
Joined: Thu Sep 18, 2008 10:26 pm

Re: output data

Post by klesk » Sat Jan 10, 2009 6:03 pm

thanks hajo for your javascript example how to write a file with javascript! But I also need a read example to read this file again. I´ve tried to use the read() part from captain.at (see below), but it didn´t work. With some modifications it will access the file, but I don´t know how to get the file content. Could you or someone else post a short example, how to read a file with javascript?

Code: Select all

function read() {
	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( savefile );
	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() );
	document.getElementById('blog').value = output;
}
att
Posts: 24
Joined: Tue Aug 19, 2008 2:22 pm

Read file with javascript

Post by att » Sat Feb 21, 2009 9:41 am

I am struggling with the same idea !
Can the file be read using this ?
It looks like you can read the file but how to process it afterwards ?

I tried changing
document.getElementById('blog').value = output;
To
data = output;

my code so far
Thanks for and advice :)

=============

Code: Select all

function readFromFile(filename, data) {
	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() );
	data = output;
	//document.getElementById('blog').value = output;
}
How to configure Cisco Routers
www.ittrainingexpert.info
josephconlin
Posts: 190
Joined: Wed Aug 06, 2008 2:38 am

Re: output data

Post by josephconlin » Tue Mar 10, 2009 10:12 pm

I haven't tried this exactly, but here's a guess at what you could do to return the data. An internet search on Javascript function return data would turn up lots of answers I'd guess, although I'm not sure if the code posted before is successful in reading the file as we haven't had a successful example posted yet.

What's happening is that you have a function that reads the data, but then that function doesn't store the data in a variable that is available outside of the function. If you declare the variable outside of the function, can you use it to store the data?

Here's an outline of what I'm saying. This code is not intended to be complete and working, but rather to give an idea of what might help.

First, declare a variable before the function to be used IN the function. Then, either try passing the variable in OR just assigning the read to the variable.

Code: Select all

var data;
function readFromFile(filename, data) {   //or you might try function readFromFile(filename) {
...
data = sis.read( sis.available() );
}
If the example code actually does get the data from the file, this should help get the data out of the function and into the main script.
davygrvy
Posts: 5
Joined: Sun Sep 06, 2009 7:31 am

Re: output data

Post by davygrvy » Thu Sep 10, 2009 4:11 pm

This is something that worked for me (under firefox) and uses the encoding translator to output a file in utf-8 for full unicode support.

Code: Select all

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 );
    outputStream.init( file, 0x02|0x08|0x20, 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
}
Post Reply