BASIC Scripts and Custom Functions

Subroutines

Although the AutoMate library of available actions provides a robust set of functionality that may be configured visually, occasionally it may be necessary to utilize straight BASIC scripting to access API calls or COM objects or to perform other complex tasks. This can be done using the built-in BASIC scripting engine. These BASIC scripts may be embedded directly into a task file, or alternatively may reference an external BAS file.

An example of this type of usage is shown below. (These task steps may be copied and pasted into the Task Builder.)

<!--- Create the variable --->
<AMVARIABLE NAME="NUMTOSQUARE">1</AMVARIABLE>
<AMVARIABLE NAME="theresult"></AMVARIABLE>
<!--- Ask the user to enter a number --->
<AMINPUTBOX WINDOWTITLE="Enter a number" RESULTVARIABLE="NUMTOSQUARE">Which number would you liked squared?</AMINPUTBOX>
<!--- Perform the calculation and add a string to result using a BASIC Script --->
<AMSCRIPT>Sub Main
  theresult = NUMTOSQUARE * NUMTOSQUARE
  theresult = &quot;The result is&quot; &amp; Str(theresult)
End Sub</AMSCRIPT>
<!--- Display result --->
<AMMESSAGEBOX>%theresult%</AMMESSAGEBOX>

Custom Functions

BASIC scripting can also be used for creating Custom Functions. A custom function performs a calculation, and may be used as many times as needed as an expression in future steps. In fact, the above task would be more efficient if created in this manner.

An example of the same task restructured with a custom function is shown below:

<!--- Create the variable --->
<AMVARIABLE NAME="NUMTOSQUARE">1</AMVARIABLE>
<AMVARIABLE NAME="theresult"></AMVARIABLE>
<!--- Ask the user to enter a number --->
<AMINPUTBOX WINDOWTITLE="Enter a number" RESULTVARIABLE="NUMTOSQUARE">Which number would you liked squared?</AMINPUTBOX>
<!--- Perform the calculation and add a string to result using a BASIC Script --->
<AMSCRIPT>Function SquareNumber(thenumber)
  SquareNumber = thenumber * thenumber
End Function</AMSCRIPT>
<!--- Display result --->
<AMMESSAGEBOX>The result is %SquareNumber(numtosquare)%</AMMESSAGEBOX>
<AMMESSAGEBOX>The result is %SquareNumber(SquareNumber(numtosquare))%</AMMESSAGEBOX>

As you can see, the basic script was converted into a custom function so that it can be called multiple times in the task and so that different values may be passed to it for processing. The function will of course yield a different result depending on the value passed to it.

See Also

BASIC Script Action

Introduction to BASIC scripting

The BASIC Language Grouped by Type

Introduction to Extended Functions