The renewal maintenance has officially ended for Progress iMacros effective November 30, 2023.
This Wiki site will also no longer be moderated from the Progress side.
Thank you again for your business and support.
Sincerely, The Progress Team
Loop after Query or Login
Sometimes you do not want to loop a full macro, but only parts of it. E.g. if you need to login or to run a query before looping through the results.
To tackle this issue, you need to use the Scripting Interface, which means that you will need to write a small script that controls the loop and calls the macros. In the example below, we use VBS which is available on almost any Windows system.
Example
Our aim, here, is the following:
- Query Google.com for some keyword (Please note that we are not affiliated with Google, but just use their website for this example)
- Loop through the Google results performing the following steps
- follow each result's link
- save a copy of the page to the local computer
- return to the Google result page
Creating the macros
Recording the Google Query
(In case recording a macro is new to you, please cf. our "First Steps" tutorials for an introduction)
- Open the Google URL in the TAB
- Start the recording
- Enter a dummy keyword to run the query on
- Start the Google query
- Stop the recording
- Save the macro as "query.google"
Here's the macro's code:
TAB T=1 TAB CLOSEALLOTHERS URL GOTO=http://www.google.com/ TAG POS=1 TYPE=INPUT:TEXT FORM=NAME:gbqf ATTR=NAME:q CONTENT=iOpus TAG POS=1 TYPE=BUTTON:SUBMIT FORM=NAME:gbqf ATTR=NAME:btnG
Recording the macro to be looped
- Start recording
- Use HTML click mode (if you already know that the POS attribute is what changes between links, you can stick with the "Auto" mode)
- Follow the first link
- Save the resulting page
- Go back to Google result
- Do the same for links 2 and 3 (in order to find the pattern of attributes used by the links)
With the URLs removed we get essentially this:
TAG POS=1 TYPE=A ATTR=CLASS:l&&TXT:*&&HREF:* SAVEAS TYPE=CPL FOLDER=* FILE=+_{{!NOW:yyyymmdd_hhnnss}} BACK TAG POS=2 TYPE=A ATTR=CLASS:l&&TXT:*&&HREF:* SAVEAS TYPE=CPL FOLDER=* FILE=+_{{!NOW:yyyymmdd_hhnnss}} BACK TAG POS=3 TYPE=A ATTR=CLASS:l&&TXT:*&&HREF:* SAVEAS TYPE=CPL FOLDER=* FILE=+_{{!NOW:yyyymmdd_hhnnss}} BACK
So the looped macro (with the POS value being replaced by the variable "loopNumber") would look like this:
TAG POS={{loopNumber}} TYPE=A ATTR=CLASS:l&&TXT:*&&HREF:* SAVEAS TYPE=CPL FOLDER=* FILE=+_{{!NOW:yyyymmdd_hhnnss}} BACK
We save it as "google.follow.link".
The VBS script
The Skeleton
Here's how a plain VBS script looks like that simply opens a single iMacros instance.
Option Explicit Dim iim1, iret 'initialize iMacros instance set iim1= CreateObject ("imacros") iret = iim1.iimOpen("-ng") 'here comes the code ' tell user we're done msgbox "End." ' exit iMacros instance and quit script iret = iim1.iimClose() Wscript.Quit()
Adding the Query
Just add
iret = iim1.iimPlay("query.google")
to make the script run the "query.google" macro.
Adding the loop
For calling the "google.follow.link" macro with a specific POS value, iimSet() is needed to set the variable "loopNumber" before calling iimPlay(). For using loopNumber with the value "2" we would use:
iret = iim1.iimSet("loopNumber", "2") iret = iim1.iimPlay("google.follow.link")
As we want to loop through the positions, we put these commands into a VBS loop construct performing the following additional changes:
- We added a loop counter "recentLineNumber"
- We made the iimSet() use the "recentLineNumber" counter
- We made the loop repeat until the macro fails (i.e. until the end of the Google result links is used)
Dim recentLineNumber recentLineNumber = 0 do while not iret < 0 recentLineNumber = recentLineNumber + 1 iret = iim1.iimSet("loopNumber", Cstr(recentLineNumber)) iret = iim1.iimPlay("google.follow.link") loop
The Full Scripting Code
Instead of playing the macro file, we can also embed the macro code in the script and use iimPlayCode(). So, the final script containing the macro code reads:
Option Explicit Dim iim1, iret 'initialize iMacros instance set iim1 = CreateObject ("imacros") iret = iim1.iimOpen("-ng") 'here comes the code 'query Dim query query = "TAB T=1" & vbNewLine query = query & "TAB CLOSEALLOTHERS" & vbNewLine query = query & "URL GOTO=http://www.google.com/" & vbNewLine query = query & "TAG POS=1 TYPE=INPUT:TEXT FORM=NAME:gbqf ATTR=NAME:q CONTENT=iOpus" & vbNewLine query = query & "TAG POS=1 TYPE=BUTTON:SUBMIT FORM=NAME:gbqf ATTR=NAME:btnG" & vbNewLine iret = iim1.iimPlayCode(query) 'follow link Dim FollowLink FollowLink = "TAG POS={{loopNumber}} TYPE=A ATTR=CLASS:l&&TXT:*&&HREF:*" & vbNewLine FollowLink = FollowLink & "SAVEAS TYPE=CPL FOLDER=* FILE=+_{{!NOW:yyyymmdd_hhnnss}}" & vbNewLine FollowLink = FollowLink & "BACK" & vbNewLine Dim recentLineNumber recentLineNumber = 0 do while not iret < 0 recentLineNumber = recentLineNumber + 1 iret = iim1.iimSet("loopNumber", Cstr(recentLineNumber)) iret = iim1.iimPlayCode(FollowLink) loop ' tell user we're done msgbox CStr((recentLineNumber-1)) & " pages saved. End." ' exit iMacros instance and quit script iret = iim1.iimClose() Wscript.Quit(iret)