Page 1 of 1

Using (Sun) Java in iMacros for Firefox

Posted: Fri Aug 14, 2009 4:03 pm
by Marcia, Tech Support
The built-in javascript interface in iMacros for Firefox allows to call (Sun) Java methods with iMacros, using LiveConnect.

As long as at least Java 6 update 12 is installed and enabled, the following code, for instance should work:

Code: Select all

function getScreenDimension() {
    alert("Screen Dimension\n" + "  width:" + + " height:" +;
function browse() {
    var frame = new;
    var fd = new, "Browse File",;
    var getDirectory = new;
    var filename = fd.getDirectory() + fd.getFile();
    alert('You selected: ' + filename);
Save it with .js extension in the iMacros Macros folder and play it with iMacros.

Re: Using (Sun) Java in iMacros for Firefox

Posted: Thu Aug 20, 2009 3:16 pm
by ABooth
Thanks for the tip.

Your script has focusing issues when launching the file browse dialog though. I tried the following, but simply could not get the fileDialog focus to be at the front: -

Code: Select all

    var frame = new java.awt.Frame();
    var fileDialog = new java.awt.FileDialog(frame, "Browse File", java.awt.FileDialog.LOAD);


    var getDirectory = new java.awt.FileDialog(frame);
    var filename = fileDialog.getDirectory() + fileDialog.getFile();
    alert('You selected: ' + filename);

Re: Using (Sun) Java in iMacros for Firefox

Posted: Thu Aug 20, 2009 5:15 pm
by ABooth
As this is the only JAVA thread, I thought I'd add a couple of javascript utility functions that use Java to access local files.

Note: These functions should include exception handling. At present, their flow only includes the "happy path".

To load a file

Code: Select all

//Load a file into a string
// filePath example: "C:\\temp\\myFile.txt"
function loadFile(filePath){
	// Load the file
	var stringBuilder = new java.lang.StringBuilder();
	var bufferedReader = new );
	var line = null;
	while( ( line = bufferedReader.readLine()) != null)
		stringBuilder.append(line + java.lang.System.getProperty("line.separator") );

	return stringBuilder.toString();
To load configuration settings

Code: Select all

//Load a properties file (for having a config file for your scripts, for example)
//fileName example: "C:\\config\\"
function loadProperties(fileName) {

	var returnValue = new java.util.Properties();

	var properties = new;
	return returnValue;
Example Usage (C:\config\

Properties file


Code: Select all

var properties	= loadProperties("C:\\config\\");

iimSet("timeout",	  properties.getProperty("timeout") );
iimSet("imageFolder", properties.getProperty("image.folder") );
iimSet("imageType",   properties.getProperty("image.type") );
iimSet("dataSource",  properties.getProperty("data.source") );
iimSet("dataColumns", properties.getProperty("data.columns") );
iimSet("url",			properties.getProperty("url") );

iimPlay( properties.getProperty("") );

iMacro script (MyMacro.iim)

Code: Select all

SET !TIMEOUT {{timeout}}
SET !DATASOURCE {{dataSource}}
URL GOTO={{url}}
SAVEAS TYPE={{imageType}} FOLDER={{imageFolder}} FILE=*

Re: Using (Sun) Java in iMacros for Firefox

Posted: Tue Jan 19, 2010 7:17 am
by ABooth
I just thought I'd add another useful function here, as many people want to know how to write to a file without using the iMacro command SAVEAS

Code: Select all

// Write text to a file.
function writeFile(fileName, data) 
	var userHome = java.lang.System.getProperty("user.home");
	var fileSeparator = java.lang.System.getProperty("file.separator");

	fileName = (fileName || "output.txt");
	fileName = (-1 == fileName.indexOf(fileSeparator) ) ? userHome + fileSeparator + fileName : fileName; // put in home path if no path specified."UniversalFileAccess");
	var fstream = new;
	var output = new;
Example usage:-

Code: Select all

writeFile('WriteTest.txt', 'The quick brown fox jumps over lazy dogs');
This will create "C:\Documents and Settings\<your username>\WriteTest.txt" (Windows) or /home/<your username> (*nix) containing "The quick brown fox jumps over lazy dogs".

This way, you can use Javascript to format your data however you like, then pass it to the function to save it.