Related example batch: Examples\Batch Files
The iMacros Browser (included in PRO and Scripting Edition) supports several command line options that control the behaviour of the browser. This browser can be called from batch files. Batch files are very simple programs that allow structures the iMacro macro language does not support directly. Such structures include conditional programming (if...then...else...), loops (for...do...) and file input/output. They consist of a series of commands, can be created and edited by any editor (e.g. Notepad) and have the file ending .bat. Batch files can be started the standard way, by double-clicking on the file, but also through the Scheduler, which is a part of the Windows operating system by default.
Please note that file shortcuts can be looked at as nothing else than a batch file that consists of only one line, the command line calling the program.
The most important part of a batch file that starts iMacros is the command line which starts the browser. As mentioned before, this command supports several command line options that control the behaviour of the browser. The general command line syntax is
imacros -command_line_switch1 switch_value1 -command_line_switch2 switch_value2 ...
A command line that starts the iMacros Browser, executes the macro called YourMacroName and passes a value to the built-in variable !VAR1 look like this:
imacros -macro YourMacroName -var1 Hello[SP]World!
After completing the macro, the iMacros Browser automatically stops and closes. Note that in command lines you need to use the square brackets instead of the usual pointy brackets (< and >) to escape a whitespace ([SP]) or linebreak ([BR]). This is because > and < are used for file input/output operations on the Windows command line and thus the command does not execute correctly.
You can now use batch files to conveniently execute several iMacros in a sequence:
echo Start iMacros batch file
imacros -macro FormExampleMacro
imacros -macro Check_Altavista
imacros -macro Buy_Now!
echo Done!
In the above macro the batch command echo is used to print messages to the standard output.
Other important command line switches are those setting variables. You can set the built-in variables !VAR1, !VAR2 and !VAR3 using the following switch:
imacros -var1 iOpus[SP]iMacros
With the command line switch -var_varname you can create your own custom variable. Imagine, you want to call a macro (searchEngine.iim) that enters the content of the variable SEARCHSTRING into a search engine and presses submit. You can set the search value via the command line:
imacros -macro searchEngine -var_SEARCHSTRING iOpus[SP]iMacros
If you want iMacros to read a value from the command line use the CMDLINE command in your macro.
CMDLINE !VAR1 http://www.iopus.com/imacros.htm
SET !VAR2 nobody@nospam.iOpus.com
TAG TYPE=INPUT:TEXT FORM=NAME:f1 ATTR=NAME:name CONTENT=Tom Tester!VAR1
Check on the return value at batch level
The iMacros executable imacros.exe sets the predefined batch file variable ERRORLEVEL. The value is either 1 if the macro was completed successfully or negative if an error was encountered. ERRORLEVEL is used almost exclusively with a conditional construct, e.g.:
imacros -macro searchEngine -var_SEARCHSTRING iOpus[SP]iMacros
IF NOT %ERRORLEVEL% == 1 ECHO Problem encountered
For more details please see the included example batch file "Example-Errorlevel.bat".
Page URL http://www.iopus.com/imacros/help/batch_files.htm