Wednesday, March 30, 2011

AutoIT Example 3. Two-Dimensional Array

Code:
$letter[0][0] = "w"
$letter[0][1] = "x"
$letter[1][0] = "y"
$letter[1][1] = "z"

A visual representation of Example 3 would be a 2×2 matrix as displayed in Figure 1.

AutoIT Constants and Arrays

Constants

 A constant is a variable that never changes. It remains a static value for the entire
script execution. You cannot change the value of a constant, nor can you convert
an existing variable into a constant. Placing Const after Dim, Global or Local makes
the variable a constant. You can also declare a constant variable without explicit
declaration. The following example illustrates how to declare a constant variable
in each scenario:

Code:
Const $example = 0
Dim Const $example1 = 1
Global Const $example2 = 2
Local Const $example3 = 3
Arrays

 An array is a matrix of data in which all the elements are of the same data type and
size. For example, an array of two numbers—“5” and “3”—is declared as follows:

Code:
$num[0] = "5"
$num[1] = "3"



Figure 1. Visual representation of Example 3. Two-Dimensional Array.
Arrays can also be multidimensional, with up to 64 dimensions. Example 3 shows a two-dimensional array.

AutoIT Variable Explicit Declaration Is Not Required

AutoIt does not require the explicit declaration of variables. However, as a debugging measure it is wise to explicitly declare all variables used within a script. If you do not explicitly declare variables, it can become very difficult to find a mistyped variable name that may be causing your script to error on
execution. You should include the following in your scripts to require the explicit declaration of variables in order to avoid bugs:

Code:
Opt("MustDeclareVars", 1)

With this option enabled, all variables must now be explicitly declared using Global, Local, or Dim.

Tuesday, March 29, 2011

AutoIT Variable Types

Dim, Global, and Local
There are three types of variables in AutoItT:

Dim
  Declaring a variable using Dim gives it the scope of its current location within   the script. If the variable is declared outside any functions, its scope is global.

  The following is an example of declaring a Dim variable in the global scope. It
runs setup.exe in the directory where the script is located:

Code:
Dim $variable = @ScriptDir & "\setup.exe"
  Run($variable)

  The next example shows how declaring a Dim variable inside a function allows it only Local scope and how the variable is destroyed once the function is complete.
 The result is a script that errors out when run because $variable is not declared globally:

Code:
function()
  Func function()
    Dim $variable = @ScriptDir & "\setup.exe"
  EndFunc

  Run($variable)

 You should explicitly declare variables as Global or Local to avoid problems. If a Dim variable is declared inside a function but a Global variable already exists, the Global variable is overwritten. The following example shows what happens if a Global variable exists when the same variable is declared as Dim within a
function. The result is that setupbad.exe runs instead of setup.exe; the Global $variable is modified to setupbad.exe because Dim was used to declare the variable locally within the function:

Code:
Global $variable = @ScriptDir & "\setup.exe"
  function()

  Func function()
    Dim $variable = @ScriptDir & "\setupbad.exe"
  EndFunc

  Run($variable)

Global
  This type of variable can be read from or written to from anywhere in the script.
Global variables can be used in functions without being destroyed when the
functions complete. The following is an example of declaring a Global variable:

Code:
  Global $variable = 2

Local
 A Local variable is used in the scope of a function. Once the function is complete,the variable is destroyed. If a Global variable of the same name already exists, the function modifies the Global variable and it is not destroyed when
the function completes. Variables are always checked in the local scope first,
then in the global scope. The following example shows the use of a Local variable
within a function:

Code:
function()
  Func function()
    Local $variable = @ScriptDir & "\setup.exe"
    Run($variable)
  EndFunc

AutoIT: Variables and Includes

A variable is simply a named placeholder for a string or array of data. You can use a variable as many times as you need within a script and it only requires declaration once. This allows you to manage and manipulate data in a centralized location if desired.
Variables are a necessity if you want to write robust scripts that are fairly simple to modify. For example, defining a filename as a variable allows you to change the filename from a single location instead of changing many static entries. (Using static data entries can lead to problems.) Example 1 installs two Windows XP Security updates. Example 2 performs the same operations, but does so using
variables. You may not yet understand everything displayed in the examples; they are only meant to show that replacing filenames with variables is one way to simplify your code.

AutoIT Example 2. Windows Update Automation Using Variables

Code:

Global $admin, $password, $program, $program2
$admin = "Administrator"
$password = "password" 
  ; change password to the password for the
  ; Administrator account

  ; change the following program names to the 
  ; actual filenames of 2 Windows updates
$program = "Windows Update 1.exe /passive /norestart"
$program2 = "Windows Update 2.exe /passive /norestart"

  If @Username <> "Administrator" Then
    RunAsSet($admin,@ComputerName,$password)
    install()
    RunAsSet()
  Else
    install()
  EndIf

  Func install()
    RunWait($program)
    RunWait($program2)
  EndFunc

AutoIT Example 1. Windows Update Automation

Code:

If @Username <> "Administrator" Then
RunAsSet("Administrator",@ComputerName,"password")
    install()
    RunAsSet()
  Else
    install()
  EndIf

Func install()
    RunWait("Windows Update 1.exe /passive /norestart")
    RunWait("Windows Update 2.exe /passive /norestart")
EndFunc

AutoIT: Introduction and History

AutoIt started in late 1998 as a C-compiled program used to automate keystrokes during software installations. In January 1999, the AutoIt team released AutoIt v1, which included the Send, Run, RunWait, WinWait, WinWaitClose, WinWaitActive,WinHide, WinActivate, WinClose, WinRestore, Sleep and SetKeyDelay functions. AutoIt v2 was released in August that same year and included the first version of AutoItX, which offered DLL/COM control. Over the next two years, massive updates to AutoIt v2 added many new functions to the language. In May 2001, the AutoIt source code was completely rewritten in C++ and development installed until 2003, when the first beta version of AutoIt v3 was released. Over 100 beta versions later, the AutoIt developers released AutoIt v3 in February of 2004. February of 2005 marked the release of AutoIt v3.1.0, which added the capability of graphical user interface (GUI) creation. This version was the most notable release because it brought AutoIt to the forefront of the scripting world and made it a rival to Visual Basic Scripting, batch files, and other popular scripting languages. AutoIt is free to use and has a strong and helpful community base.
Download the latest version of AutoIt HERE:
I recommend using the SciTE AutoIt3 Editor for writing scripts. It has an extensive help file and color-codes everything nicely. You can download the latest SciTE AutoIt3 Editor HERE