Loop after Query or Login
(Copy URL in PhpBB Forum Format - Info)From iMacros
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.
Contents |
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
- take screenshot of that page
- 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:
VERSION BUILD=6221002 TAB T=1 TAB CLOSEALLOTHERS URL GOTO=http://www.google.com/webhp? TAG POS=1 TYPE=INPUT:TEXT FORM=NAME:f ATTR=NAME:q CONTENT=test<SP>keyword TAG POS=1 TYPE=INPUT:SUBMIT FORM=NAME:f 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 system behind 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 that:
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.iimInit()
'here comes the code
' tell user we're done
msgbox "End."
' exit iMacros instance and quit script
iret = iim1.iimExit()
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("-var_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("-var_loopNumber", Cstr(recentLineNumber))
iret = iim1.iimPlay("google.follow.link")
loop
The Full Scripting Code
And here we are:
Option Explicit
Dim iim1, iret
'initialize iMacros instance
set iim1 = CreateObject ("imacros")
iret = iim1.iimInit()
'here comes the code
'query
iret = iim1.iimPlay("query.google")
Dim recentLineNumber
recentLineNumber = 0
do while not iret < 0
recentLineNumber = recentLineNumber + 1
iret = iim1.iimSet("-var_loopNumber", Cstr(recentLineNumber))
iret = iim1.iimPlay("google.follow.link")
loop
' tell user we're done
msgbox "End."
' exit iMacros instance and quit script
iret = iim1.iimExit()
Wscript.Quit()
