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:" +
    Packages.java.awt.Toolkit.getDefaultToolkit().getScreenSize().width + " height:" +
    Packages.java.awt.Toolkit.getDefaultToolkit().getScreenSize().height);
}
function browse() {
    var frame = new Packages.java.awt.Frame();
    var fd = new Packages.java.awt.FileDialog(frame, "Browse File", Packages.java.awt.FileDialog.LOAD);
    fd.toFront();
    fd.show();
    var getDirectory = new Packages.java.awt.FileDialog(frame);
    var filename = fd.getDirectory() + fd.getFile();
    alert('You selected: ' + filename);
}
getScreenDimension();
browse();
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);

	frame.setAlwaysOnTop(true);
	frame.setFocusableWindowState(true);

	fileDialog.toBack();
	fileDialog.toFront();
	fileDialog.requestFocusInWindow();
		
	fileDialog.setVisible(true);
    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 java.io.BufferedReader(new java.io.FileReader(filePath) );
	
	var line = null;
	while( ( line = bufferedReader.readLine()) != null)
		stringBuilder.append(line + java.lang.System.getProperty("line.separator") );

	bufferedReader.close();
	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\\MyMacro.properties"
function loadProperties(fileName) {

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

	var properties = new java.io.FileInputStream(fileName);
	returnValue.load(properties);
	properties.close();
	
	return returnValue;
}
Example Usage (C:\config\MyMacro.properties)

Properties file
timeout=20
image.folder=C:\screen<SP>shots
image.type=PNG
data.source=C:\Data.csv
data.columns=20
url=http://www.google.com
macro.name=MyMacro


Javascript
...

Code: Select all

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

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("macro.name") );
...


iMacro script (MyMacro.iim)
...

Code: Select all

SET !TIMEOUT {{timeout}}
SET !DATASOURCE {{dataSource}}
SET !DATASOURCE_COLUMNS {{dataColumns}}
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) 
{
	netscape.security.PrivilegeManager.enablePrivilege("UniversalPropertyRead");
	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.

	netscape.security.PrivilegeManager.enablePrivilege("UniversalFileAccess");
	var fstream = new java.io.FileWriter(fileName);
	var output = new java.io.BufferedWriter(fstream);
	output.write(data);
	output.close();
}
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.