Macro or Script

From iMacros
Jump to: navigation, search

iMacros or iMacros Scripting Interface?

Often iMacros users need to decide whether they need to use pure iMacros or the iMacros Scripting Interface (i.e. iMacros together with a scripting or a programming language). We will try to give you some basic guidelines here.

Basic hints

The rule of thumb is: if the macro needs to

- make decisions based on conditions (if/else)

- take data from external sources other than CSV files (e.g. database)

- have more than one loop

- parse data / perform string manipulations

- save data to a database

- use many variables

- make complex calculations

- communicate with other applications (e.g. Excel)

- easily launch macros in Internet Explorer, Firefox, Chrome, and the iMacros browser

- integrate with other tools/programs (e.g. web testing suites, web server scripts, email servers, custom applications)

- display GUI elements other than a simple prompt dialog

- call another macro

- handle error conditions,

then you need the Scripting Interface.

If on the contrary,

- the macro needs to perform a number of sequential actions

- no conditions should be checked

- no flow control is required,

then you can use pure iMacros (.iim macro files).

Some exceptions

However, there are a few exceptions that let you use pure iMacros code even in some situations which look like decision making is required. Consider these examples:

1. You want your macro to continue if an error occurs. This looks like decision making (if error, then continue), which requires the Scripting Interface, but in reality you can set the !ERRORIGNORE variable to YES, which will make the macro ignore the error and continue.

2. You want the macro to stop execution if a keyword is found. Looks like a decision (if found, then stop), but in reality you can use EVENT:FAIL_IF_FOUND to stop the macro if it finds the keyword.

3. The built-in variables (!VAR0, !VAR1, ... !VAR9) and the ADD function can be used for some basic calculations and remembering values. As of iMacros 7.50, you can now create unlimited user-defined macro variables.

4. The EVAL command provides a powerful way to perform more complex calculations and data manipulation by evaluating Javascript statements directly in your macro.

Why is this not in iMacros?

Why doesn't iMacros support flow control operations (if/else, for, while etc.) or many other features from programming languages? First of all, this is NOT a limitation of iMacros, but rather a deliberate and informed design decision. Why? We do not think that our customers should have to attend week-long seminars just to learn yet another proprietary scripting or programming language. Any programming logic can be put into an external script that then calls iMacros using the Scripting Interface.

This way people who already have some programming skills benefit from the fact that they can use a programming language they know and don't have to learn another one. And those people who do not yet know a programming language can learn any language they prefer and then not only use it with iMacros, but also for any other tasks they might want to program later. I.e. they don't learn a proprietary programming language which they can only use with iMacros, but one that they can then use anywhere.

It may look as though using an external programming language adds complexity, but creating a new programming language to use with iMacros would add just as much complexity.

So we created the very powerful Scripting Interface that allows you to use iMacros with every Windows Scripting or programming language on the planet. Examples are VBS, VB, VBA, VB.NET, Perl, Java, Foxpro, C, C++, C#, ASP, ASP.NET, PHP and many more. These languages are used by millions of computer users, and are reliable and very well documented. We also added a command line interface for use with batch files and the task scheduler.

Many samples are available here: Sample_Code

Some examples

Here are a few examples of the most basic and most frequent operations you can perform with the iMacros Scripting Interface. We use Visual Basic for these samples, but they can easily be translated into any other language.

Running a macro from a Visual Basic script

First of all we create an iMacros object:

 Set iim1 = CreateObject ("imacros")

Then initialize it with iimOpen() (you can add parameters to iimOpen() to have the macro run in different browsers, in silent mode, etc.)


Then we call the macro with iimPlay(). There are 3 ways to do this:

1. Specify the full path to the macro as a parameter for iimPlay()


2. If the macro is located in the "Macro folder" specified in Edit - Options - Paths in iMacros, you can use just the name of the macro as a parameter for iimPlay()


3. Use the code of the macro as a parameter for iimPlay(). In this case the macro should begin with "CODE:" and you should add a line break to the end of each line

 Macro = "CODE:"
 Macro = Macro + "URL GOTO=" + vbNewLine
 Macro = Macro + "WAIT SECONDS=2" + vbNewLine
 Macro = Macro + "URL GOTO=" + vbNewLine

3a. Use iimPlayCode to play dynamically generated macro code. With this method, the macro code need not begin with "CODE:"

 Macro = URL GOTO=" + vbNewLine
 Macro = Macro + "WAIT SECONDS=2" + vbNewLine
 Macro = Macro + "URL GOTO=" + vbNewLine

Sending variables to a macro

If you need to pass some values into the macro, you must use the iimSet() command.

 Macro = "CODE:"
 'Notice that the variable name in the macro is enclosed in "{{}}"
 Macro = Macro + "URL GOTO={{myurl}}" + vbNewLine
 iim1.iimSet("myurl", "")

Please note, that you have to use iimSet() before every iimPlay()! You can't just set it once and for all. That means if you use iimPlay() in a loop, iimSet() should also be in a loop before iimPlay(), because even if the values it sends to the macro don't change, these values are erased between calls to iimPlay().

Extracting data from a macro

Anything that the macro stores in the !EXTRACT variable the script can read with iimGetLastExtract() after the macro is played. You can either assign something to the !EXTRACT variable directly in iMacros (SET !EXTRACT Hello) or by using the extract parameter of the TAG command

 Macro = "URL GOTO=" + vbNewLine
 Macro = Macro + "TAG POS=1 TYPE=TD ATTR=CLASS:bdytxt&&TXT:* EXTRACT=TXT" + vbNewLine

Checking for errors

You can simply check the return value of iimPlay() to make sure the macro ran successfully or you can use iimGetLastError() to see what the last error message was.

 iret = iim1.iimPlay("Demo-FillForm")
 If iret > 0 Then
   s = "Everything OK"
   s = iim1.iimGetLastError()
 End If
 MsgBox s

Exiting iMacros

At the end of your script, when you’re not planning to use the iMacros object anymore, please don’t forget to close iMacros with iimClose()


See Also

Web Scripting, Scripting Interface Command Overview