Using (Sun) Java in iMacros for Firefox

Forum rules
iMacros EOL - Attention!

The renewal maintenance has officially ended for Progress iMacros effective November 20, 2023 and all versions of iMacros are now considered EOL (End-of-Life). The iMacros products will no longer be supported by Progress (aside from customer license issues), and these forums will also no longer be moderated from the Progress side.

Thank you again for your business and support.

Sincerely,
The Progress Team
Post Reply
Marcia, Tech Support

Using (Sun) Java in iMacros for Firefox

Post by Marcia, Tech Support » Fri Aug 14, 2009 4:03 pm

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.
ABooth
Posts: 223
Joined: Mon Aug 10, 2009 4:25 pm

Re: Using (Sun) Java in iMacros for Firefox

Post by ABooth » Thu Aug 20, 2009 3:16 pm

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);
}
Last edited by ABooth on Thu Aug 20, 2009 6:03 pm, edited 1 time in total.
ABooth
Posts: 223
Joined: Mon Aug 10, 2009 4:25 pm

Re: Using (Sun) Java in iMacros for Firefox

Post by ABooth » Thu Aug 20, 2009 5:15 pm

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=*
...
Last edited by ABooth on Wed Jan 20, 2010 6:19 am, edited 1 time in total.
iMacros for Firefox supports JavaScript Macros (Scripting)
JavaScript supports Java via LiveConnect

Therefore: You can write powerful macros with iMacros for Firefox. Have a look at this one

Post feature requests here. Maybe one day, they'll pin it?
ABooth
Posts: 223
Joined: Mon Aug 10, 2009 4:25 pm

Re: Using (Sun) Java in iMacros for Firefox

Post by ABooth » Tue Jan 19, 2010 7:17 am

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.
iMacros for Firefox supports JavaScript Macros (Scripting)
JavaScript supports Java via LiveConnect

Therefore: You can write powerful macros with iMacros for Firefox. Have a look at this one

Post feature requests here. Maybe one day, they'll pin it?
Post Reply