Wednesday, August 3, 2011

iMacros Tutorials Part 1 [Java]

Preliminaries:

1) Download Jawin (http://jawinproject.sourceforge.net/, version 1.0.19, released under this license:http://jawinproject.sourceforge.net/LICENSE.txt), extract to local folder 
[Update] Version 2.0-alpha1 also works fine.[End of update]

2) Start Eclipse

Start new project (usign jawin) in Eclipse:

1) File --> New --> Project

Image

2) Java --> Java Project --> Next

Image

3) Enter name: "iMacrosScriptingInterface" --> Next

4) Libraries --> Add External Jars" --> select jawin.jar from the Jawin\lib directory --> Finish

Image

5) Right-click iMacrosScriptingInterface in Package Explorer tab

Image

6) Import --> General --> File System --> Next

Image

7) Browse --> jawin-dir\bin\jawin.dll --> Finish

New Class in project

1) Select iMacrosScriptingInterface in the Package Explorer tab

2) File --> New --> Class

Image

3) Name: iMacrosTest --> Finish

Writing/Runnign an example

1) Open iMacrosTest.java

2) Copy the following code to iMacrosTest.java:

CODE: SELECT ALL
//This code makes use of jawin from http://jawinproject.sourceforge.net/
//jawin license: http://jawinproject.sourceforge.net/LICENSE.txt

//two imports from jawin.jar
import org.jawin.DispatchPtr;
import org.jawin.win32.Ole32;

public class iMacrosTest {

   public static void main(String[] args) {
      try {
         Ole32.CoInitialize();

         DispatchPtr app = new DispatchPtr("imacros");

         //Calling iMacros methods
         app.invoke("iimInit", null);
         app.invoke("iimPlay", "CODE:URL GOTO=http://www.iopus.com");
         app.invoke("iimPlay", "CODE:TAG POS=1 TYPE=A ATTR=TXT:iMacros EXTRACT=TXT");

         //manually cast return values to correct type
         String iret = app.invoke("iimGetLastExtract").toString();

         app.invoke("iimExit");
                        System.out.println(iret);
                       
         Ole32.CoUninitialize();
      }

      catch (Exception e){
         e.printStackTrace();
      }

   }
}
[Edit: changed "URL GOTO=www.iopus.com" to "URL GOTO=http://www.iopus.com]
[Update]Changed TAG command to comply with web page layout.[End of update]

3) You might want to select the full code and have it automatically be indented with "CTRL-i"

4) Menu: Run --> Run...

Image

5) Rightclick Java-Application --> New

Image

6) Under "Classpath" you should find the jawin.jar

Image

7) Under "Common" check both Debug and Run

Image

8) Click Run 
this can later be accessed by the Run-Button:

Image

The Java class will now be compiled and started. It will open an iMacros browser window that visits http://www.iopus.com

No comments:

Post a Comment