SlideShare a Scribd company logo
Page No.1
AUTO IT
1
PROJECT REPORT ON
AUTO IT(Automation and Scripting
Language)
AUTOMATA & FORMAL LANGUAGES
PROFF: DR.GORAN
VIRGINIA INTERNATIONAL
UNIVERSITY
Page No.2
AUTO IT
2
TEAM MEMBERS:
SAI DEERAJ PATURI
VIJAY NARAYAN
SUNNIKANTH BORRA
Page No.3
AUTO IT
3
Original Document: Alex Peters (LxP)
http://yallara.cs.rmit.edu.au/~apeters/
Updated By: Brett Francis (BrettF)
http://signa5.com
Page No.4
AUTO IT
4
Table of Contents
Acknowledgements.....................................................................................................................7
Introduction.................................................................................................................................8
What Is AutoIt?......................................................................................................................8
Features of AutoIt..................................................................................................................8
1 Starting Off.............................................................................................................................8
1.1 Download and Install AutoIt V3.....................................................................................8
1.2 Install SciTE4AutoIt3 ......................................................................................................9
1.3 Know how to access the help file.....................................................................................10
1.4 Know what you want to achieve......................................................................................10
1.5 What is contained in this tutorial?....................................................................................10
2 Your First Steps ...................................................................................................................11
2.1 Creating and Organizing Script Files ............................................................................11
2.2.1 Organization of Scripts ...........................................................................................11
2.1.2 Creating a Script from Explorer..............................................................................11
2.1.3 Creating a Script from SciTE..................................................................................11
..........................................................................................................................................
2.2 Hello World Example.....................................................................................................11
2.2. 1 The Steps ...............................................................................................................11
2.2.2 Explanation..............................................................................................................12
2.2.3 Extended Work.......................................................................................................12
2.2.4 The Code ................................................................................................................13
..........................................................................................................................................
..........................................................................................................................................
2.3 Simple Notepad Automation..........................................................................................13
Page No.5
AUTO IT
5
2.4. 1 The Steps ...............................................................................................................14
2.3.2 Explanation..............................................................................................................16
2.3.3 Extended Work.......................................................................................................16
2.3.4 The Code ................................................................................................................15
2.5 Exercies..........................................................................................................................16
3 Programming—it’s all about values.....................................................................................16
3.1 How values are used......................................................................................................16
3.2 Different types of values................................................................................................18
3.3 Values in Depth.............................................................................................................18
3.3.1 Numeric Values.......................................................................................................18
3.3.2 Strings......................................................................................................................18
3.3.3 Booleans..................................................................................................................19
3.4 Exercises ........................................................................................................................19
4 Arrays of Variables...............................................................................................................19
4.1 Declaring and naming variables.....................................................................................20
4.2 The Naming Convention................................................................................................22
4.3 Assigning variable values ..............................................................................................22
4.4 Accessing variable values..............................................................................................23
4.5 Constants........................................................................................................................22
4.6 Macros...........................................................................................................................23
4.7 Arrays ............................................................................................................................25
4.8 Exercises ........................................................................................................................25
5 Functions...............................................................................................................................26
5.1 Example function calls...................................................................................................27
5.1.1 Run() and RunWait()...............................................................................................27
5.1.2 WinWait()................................................................................................................28
5.1.3 Send()......................................................................................................................28
5.2 Exercises.....................................................................................................................28
Page No.6
AUTO IT
6
6 Operators..............................................................................................................................28
6.1 True or False?................................................................................................................31
True...................................................................................................................................31
False..................................................................................................................................31
6.2 Exercises ........................................................................................................................31
7 Branching..............................................................................................................................32
7.1 Conditional Functions....................................................................................................32
7.1.1 If…Then…Else........................................................................................................33
7.1.2 Select Case..............................................................................................................33
7.1.3 Switch Case ............................................................................................................33
7.1.4 ContinueCase...........................................................................................................33
7.2 Exercises ........................................................................................................................33
8 Loops....................................................................................................................................35
8.1 Types of Loops..............................................................................................................35
8.1.1 For…Next...............................................................................................................35
8.1.2 While…Wend..........................................................................................................36
8.1.3 Do…Until................................................................................................................36
8.1.4 ExitLoop..................................................................................................................37
8.1.5 ContinueLoop..........................................................................................................37
8.2 Exercises ........................................................................................................................37
9 The Graphical User Interface (GUI) ....................................................................................37
9.1 Creating the GUI...........................................................................................................38
10. Conclusion………………………………………………………………………………….39
11.Bibilography ……………………………………………………………………………….39
Page No.7
AUTO IT
7
Acknowledgements
Alex Peters (LxP): Thank you for all the hard work put into the original tutorial! Without
you, this probably would never exist!
Introduction
What Is AutoIt?
AutoIt v3 is a freeware BASIC-like scripting language designed for automating the
Windows GUI and general scripting.
Features of AutoIt
• Easy to learn BASIC-like syntax
• Simulate keystrokes and mouse movements
• Manipulate windows and processes
• Interact with all standard windows controls
• Scripts can be compiled into standalone executables
• Create Graphical User Interfaces (GUIs)
• COM support
• Regular expressions
• Directly call external DLL and Windows API functions
• Scriptable RunAs functions
• Detailed help file and large community-based support forums
• Compatible with Windows 95 / 98 / ME / NT4 / 2000 / XP / 2003 / Vista / 2008
• Unicode and x64 support
• Digitally signed for peace of mind
• Works with Windows Vista's User Account Control (UAC)
Page No.8
AUTO IT
8
1 Starting Off
1.1 Download and Install AutoIt V3
First of you will need to download and install the latest stable release of AutoIt. Although
you have the option of downloading a Compressed .ZIP distribution, it is advised to
download the installer, as it has many advantages, such as easier access to the help file, the
ability to double-click AutoIt scripts to start them or to edit them, and the painless use of the
Includes code library (A large library of various UDFs [we’ll get into that later]) in your
scripts.
Download Latest Installer for AutoIt:
http://www.autoitscript.com/cgi-bin/getfile.pl?autoit3/autoit-v3-setup.exe
1.2 Install SciTE4AutoIt3
" SciTE is a Scintilla based Text Editor. Originally built to demonstrate Scintilla, it has
grown to be a generally useful editor with facilities for building and running programs." –
Niel Hodgson
SciTE4AutoIt3 contains SciTE, wrapped into a single installer with all needed
configuration settings and lots of utility programs like Tidy, Obfuscator, AU3Check,
ScriptWriter, AutoIt3Wrapper to enhanced SciTE for use with AutoIt3. It contains around
10 tools for use and ease of scripting.
Page No.9
AUTO IT
9
I recommend that you download SciTE, as scripting using this is much easier, especially
with all the tools available for quick and easy use.
1.3 Know how to access the help file
The help file that is included with every installation is very comprehensive, and contains
examples for every almost every function. You can easily access the help file in one of
two ways:
Through All Programs:
Start > All Programs > AutoIt V3 > AutoIt Help File
Through SciTE:
If you have installed SciTE, using the help file when scripting is much easier. When you
press <F1>, the help file will open displaying the keyword that the cursor was on.
It is very important to get comfortable with the help file, both with what it contains, and how
to use it because it will be your primary source on how to use any specific function. This
tutorial covers the basic use of functions, but does not go into great depth of every function.
1.4 Know what you want to achieve
This is very important for any project you undertake. Knowing what you want to achieve
is the starting point in your script, and in turn will make what you want to achieve much
easier. A good idea is to sit down with a piece of paper, and just brainstorm ideas.
A common topic that I see in the forums is “what to make next”. Some responses to that
age old question are:
• Start multiple programs and arrange them nicely on the screen.
• Start a single program and adjust some values on the window, then hide it.
• Periodically perform an action in some open program (without interrupting any work
taking place at that time)
• Shut down the computer after some lengthy operation (like a backup or
perhaps a download)
• Automate some ‘housekeeping’ chores such as backing up important files and
emptying the Recycle Bin
1.6 What is contained in this tutorial?
This tutorial teaches you basic knowledge with scripting in AutoIt. At the end of every
chapter, there are some Chapter Review questions, and exercises for you to complete.
Answers for the chapter review and some exercises can be found at on page 39. A list of
Frequently Asked Questions (a must read after this tutorial!) can be found on page 72.
Page No.10
AUTO IT
10
2 Your First Steps
Your first steps are important, with how to code, and what practices you should pick up
early, to make you a more productive coder. To start off though, we will work through
some examples, explaining what we are doing, and what everything means.
2.1 Creating and Organizing Script Files
Creating a script file is the first step in starting to create your scripts.
2.2.1 Organization of Scripts
Organization is one of the many keys to being able to create scripts (especially large
projects) efficiently. It is always a good idea to keep your scripts contained in one folder, so
that you always know where files are, and in some cases, what they are. For an example of
how you could go about organizing this folder, we will look at what I have done. Basically
in “My Documents” I have an “AutoIt” folder. The folders inside the “AutoIt” folder house
my projects, scripts and programs from the Example Scripts Forum, that I like, or may want
to build upon and study later, and a special folder which I dedicate to solutions of where I or
others have helped people on the forums. Having it set up this way means I can easily find
my scripts, examples for support in the forums or for personal use, and easy offline access to
a large range of UDF libraries.
2.1.2 Creating a Script from Explorer
1. Navigate to any folder in Windows Explorer (I am in “My
DocumentsAutoItTutorials”)
2. Right Click and select New > AutoIt Script
3. Type your filename in. Leave “.au3” intact if it is visible.
Page No.11
AUTO IT
11
2.1.3 Creating a Script from SciTE
SciTE is the editor you will be using for most if not all of your scripting, so it is
important that you can0020at least create a script using it.
1. Open SciTE from Start > All Programs > AutoIt v3 > SciTE > SciTE
2. Go File > New or press <CTRL> + <N> to create a new script.
3. Save your script by File > Save As or <CTRL> + <SHIFT> + <S>.
When saving a file in SciTE, it is very important that you make sure the file has .au3 (the
file extension for AutoIt V3 scripts) on the end, as not having it will causing it to be saved
as a plain file.
2.2 Hello World Example
This is a modified version of the ‘Hello World’ tutorial available in the AutoIt V3 Help
file (Under Tutorials). Like the tutorial it assumes that you have AutoIt V3 installed, and
SciTE.
This tutorial will teach you how to display a message box by use of native function,
explain parameters of functions, the use of numeric flags, and basic use of strings and
comments. It also discusses basic use of the help file.
2.2. 1 The Steps
1. Create a new script using the previously discussed explorer method and
open it for editing in SciTE.
2. The code you can see in SciTE is comments. These are ignored by the
interpreter. The character to make comments is a semi-colon (;). These can be
placed at the start of lines, or after functions.
3. Now we want to tell AutoIt to display a message box. To do that, we have to
use the MsgBox function. After the last comment, add the following line:
MsgBox(0, "Tutorial", "Hello World!")
Page No.12
AUTO IT
12
4. Save the script.
5. Run the script by either
a. Right Click > Run Script on the saved .au3 file in explorer
b. In SciTE, going to Tools > Go or pressing <F5>
6. Congratulations! You have just created your first script.
2.2.2 Explanation
Almost every function in AutoIt will take a parameter. Parameters define what is to be done
in a function, and changes things, such as text and how certain things are displayed. For this
example, the MsgBox takes three parameters - a flag, a title and a message. The first
parameter, the flag is a number that changes the way MsgBox displays. In this example we
used 0. The next 2 parameters, the title and message are both strings. When using strings in
AutoIt you are able to surround the text in double or single quotes. Both "This is some text"
and 'This is some text' will work fine. Later on in this book we will discuss strings in more
depth.
2.2.3 Extended Work
So we used the function MsgBox, gave it a title and a message, and ran it. But we can do
more to make the message box gain more attention, or to make it look different to standard.
To change the effect that our message box created, we can alter the flag parameter. With the
flag we can change the icon, some display options, what buttons are displayed, and much
more. So open the help file and find the entry for MsgBox.
As you can see, there is the title of the function, a short abstract of what the function does, the
parameters of the function, return values, the remarks section, containing the flags to alter our
message box and some more information that may help the reader, related functions, and an
example.
Page No.13
AUTO IT
13
Now to change how our message box displays. As you can see by the picture above we can
change the icon displayed, and the buttons. So how can we do that? In the remarks section
the help file, there is a table containing the flags for changing the message box. To use more
than one flag add them together. Experiment with different values and see what you get.
Remember, add the flag values together, and if you run your script and nothing happens,
there is no need for alarm. It is more than likely that you have made a mistake in adding the
values together. An invalid flag will not display a message box, so be careful when adding
values together. One of the simplest methods for combining flags is using a calculator, and it
is also fairly foolproof.
2.2.4 The Code
This is the full code for this example and some extended examples.
; Script Start - Add your code below here
;Basic Example
MsgBox(0, "Tutorial", "Hello World!")
;Extended Work
MsgBox(18, "Tutorial", "Hello World!"); Abort, Retry, Ignore with
Stop Icon MsgBox(32, "Tutorial", "Hello World!"); Normal message
box with exclamation icon.
MsgBox(12345, "Tutorial", "Hello World!") ; A message box with and invalid
flag. This will NOT display.
2.3 Simple Notepad Automation
This tutorial is about the automation of notepad.
This tutorial will teach you basic use of the AutoIt v3 Window Info Tool, give you an
introduction to Send, to window titles, to the Run function, to some Win* functions, and
show you some basic automation. In the extended task, window states and Sleep function
Page No.14
AUTO IT
14
are also explored, as well as some basic macros.
2.4. 1 The Steps
1. Create a new script and open it for editing.
2. The first thing we need to do is run Notepad. To do that we get the Notepad
executable (notepad.exe). We got that from looking at the short cut to notepad in
the Start Menu. So to execute notepad.exe, we must use the run function. And the
following line into your script.
Run(“notepad.exe”)
3. Save the script and run it. If all goes well, we should have a new instance of
notepad appear. Don’t close this instance just yet, we still need to work with it
in the next few steps.
4. When automating applications AutoIt can check for window title so that it knows
which window it should work with. With Notepad the window title is Untitled -
Notepad. AutoIt is case-sensitive when using window titles so you must get the
title exactly right - the best way to do this is to use the AutoIt Window
Information Tool. Run the Information Tool from Start Menu > AutoIt v3 >
AutoIt Window Info, or since we are in SciTE, Tools > AU3Info, or use the
hotkey <CTRL> + <F6>.
5. With the Info Tool open click on the newly opened Notepad window to activate
it; the Info Tool will give you information about it. The information we are
interested in is the window title. While in the notepad window, press <CTRL> +
<ALT> + <F> to freeze the Info tool. That way we can access the information we
just gathered about notepad.
6. Highlight the title in the AutoIt Info Tool window and press CTRL-C to
copy it to the clipboard - we can then paste the title into our script without
fear of misspelling it.
Page No.15
AUTO IT
15
7. After running a copy of Notepad we need to wait for it to appear and become
active before we send any keystrokes. We can wait for a window using the
WinWaitActive function. Most window-related functions in AutoIt take a
window title as a parameter. Enter the following as the second line in the script
(use <CTRL> + <V> or Edit > Paste to paste our window title from the
clipboard). Add the following line into the script:
WinWaitActive(“Untitled – Notepad”)
1. After we are sure the Notepad window is visible we want to type in some text.
This is done with the Send function. Now we will add this line to our script.
Send("This is some text.")
2. Close the copy of Notepad that we previously opened (you will need to do this
every time you run the script otherwise you will end up with lots of copies
running!). Run the script.
3. You should see Notepad open, and then some text will magically appear!
8. Next we want to close notepad, we can do this with the WinClose function.
WinClose("Untitled - Notepad")
4. When Notepad tries to close you will get a message asking you to save the
changes. Use the Window Info Tool to get details of the dialog that has popped
up so that we can
respond to it :)
9. So, we add a line to wait for this dialog to become active (we will also use the
window text to make the function more reliable and to distinguish this new
window from the original Notepad window):
WinWaitActive("Notepad", "Do you want to save")
10. Next we want to automatically press Alt-N to select the No/Don't save button
(Underlined letters in windows usually indicate that you can use the ALT key
and that letter as a keyboard shortcut). In the Send function to send an ALT
key we use an exclamation point (!).
11.
Page No.16
AUTO IT
16
Send("!n")
5. Run the script and you will see Notepad open, some text appears, then it closes!
2.3.2 Explanation
Basically what we did in this script was run notepad, using the function Run. The only
parameter we had was the path. Then we waited for the window to open, sent text, and then
closed it. It was pretty much all explained as we went.
2.3.3 Extended Work
So what if we wanted to send new lines, or stop the user from interfering from the text being
sent?
First off, we will start with making the sent text have new lines. Find the following line in
your script:
Send("This is some text.")
And replace it with the following:
Send("This is some text." & @CRLF & "This is on a new line! ")
Notice we added the & (a string concentrator, meaning we can join 2 values together. For
example $var = “one” & “two” will have $var equal “onetwo”. ‘&’ is an operator, which is
a character which AutoIt interprets to assign variables, perform mathematical operations,
comparison of values and logical operations. For a complete list, look in the help file under
‘Operators’. We also used the macro @CRLF, which is a carriage return and a line feed.
Now how about stopping the user from interfering with our automation? For that we have a
simple command, BlockInput. It is very simple to achieve this. We just have to add the
following line at the start of the script.
BlockInput (1)
And this line at the end, to enable input for the user again.
BlockInput (0)
Have a look in the help file for BlockInput, and read more about it.
Page No.17
AUTO IT
17
3 Programming—it’s all about values
3.1 How values are used
This is Alex’s theory of values. I think it is very effective in describing what they are in
relation to AutoIt. “In my opinion the most essential thing to realize when it comes to
programming is this: it’s not much more than a game of throwing values around”
Justification for that includes:
The script made as part of the 2.2 Hello World Example instructs AutoIt to display a
message box. To do this three values are needed: one dictating the style of the box (like icon
and buttons), one for the title and one for the message.
Suppose that you wanted to write a simple number guessing game. You would have a value
for the number to guess and a value for what the user has entered as their guess. You would
compare those two values to determine if the user has made a correct guess, and then you
would probably display one of two human-readable messages informing them of the
outcome, which are also values.
On the very other end of the spectrum, perhaps you want to write a utility that updates the
Windows operating system. You would have values to indicate which files are to be
manipulated. You would open the files specified by those values and search for certain
values in each file to determine whether you need to make adjustments. If you do, you
would overwrite some part of each file with a new set of values.
It can be seen rather clearly from the above scenarios that values can take many forms:
numbers, characters, strings of characters, binary data or even a combination. For
instance, these are all potential values:
12
3
Ab
c
0.5
ZM
Z
. - = _ ? + `
This is a
sentence!
Page 17
Page No.18
AUTO IT
18
In my opinion, understanding and thinking about things in terms of values should make any
coding task much easier. Consider what values might be involved for any task that you plan
to undertake; by knowing what values will be needed, you’ll be better prepared to know
exactly how these values must be manipulated to get the job done. (You’ll also know
exactly what to look for in the help file since AutoIt instructions are named according to
what they do.)
In the next few sections we will be looking at exactly what these values are, and different
methods of storing the data.
3.2 Different types of values
There are many different types of values. We have already encountered two- “Numeric
Values”, “String Values”. Those values are probably the most important, and the most
encountered value type. Common values you will encounter in AutoIt include:
• Strings
• Numerical Values
• Booleans
We will go through them in detail in the next section.
3.3 Values in Depth
In the previous section we briefly discussed different types of values. Now we will go in
depth on what they are, and then in the next section, how to store them.
3.3.1 Numeric Values
Numeric values are entered into a script in the same fashion as one would enter numbers into
spreadsheet software—that is, digit grouping symbols (also known as thousand separators)
are not used, and numbers are made negative by placing a minus sign (-) to the front.
Regardless of the standard notation for your region, a period is used to represent a decimal
point (e.g. 124.45 and not 123,45).
-1 ; Negative Number
10 ; Integer
10000 ; Ten Thousand
1.5 ; One and a half or One point five
3.3.2 Strings
Since the value of a string can be virtually anything, AutoIt needs to be told where strings
Page No.19
AUTO IT
19
start and end. This is necessary to prevent symbols belonging to the string from being
interpreted by AutoIt in another way, and is done by placing a single quote (') or double
quote (") at both ends of the string. (You may have noticed that the script from the tutorials
uses double quotes.)
When a string doesn’t contain either type of quote, whichever one you choose to surround
the string makes no difference If a string should contain one type of quote but not the other
then it makes sense to use that other type of quote to surround the string. What happens
though if the string should contain both types of quote? In such a case you must nominate
which quote you will use to surround the string, and then double up on any occurrences of
that quote within the string. Here’s an example of a line of text containing both types of
quote:
I'm a string with single and "double" quotes
If we wanted to surround the string with double quotes then we would double up on the
double quotes within the string, giving us this:
"I'm a string with single and ""double"" quotes."
If we wanted to instead use single quotes then we would double up on the single quote
and this would be the result:
'I''m a string with single and "double" quotes.'
Both of these are complete strings, and should give you an understanding on how to use
strings, as they are, or with single and double quotation marks.
3.3.3 Booleans
Booleans have 2 basic values- True or False. They are used in Logical
expressions. A basic script showing use of Booleans is below:
$bool= False
If NOT $bool = True Then Msgbox(0,"Bool comparison", "OK")
; Basically the above line tests if the variable $bool is not true (in other words, false).
4 Arrays of Variables
Variables are areas in computer memory, identified by name, that hold values. The value
Page No.20
AUTO IT
20
contained within a variable could come from the script itself, from a function (through its
return value), from some other calculation (potentially involving other variables) or from
any combination of these during the running lifetime of a script. A variable’s value can be
read any number of times and it can be overwritten with some new value as often as desired.
4.1 Declaring and naming variables
A variable’s name consists of a dollar sign ($) followed by any combination of one or more
letters, numbers and the underscore character (_). Technically the $ is not part of the variable
name itself, but it is used to indicate to AutoIt that it is a variable. Here are some examples of
valid (although not necessarily advisable) variable names:
$MyVar
$Msg
$WAIT_TIME
$Answer
$_ ABC_ 123
$MsgBox
$19283
It should be noted that variable names are case-insensitive, which means that the names
below would all point to the same variable in memory:
$A
BC
$A
Bc
$Ab
C
$Ab
c
$aB
C
$aB
c
$ab
C
$ab
c
When declaring variables you have the option of declaring it in 3 scopes-
Page No.21
AUTO IT
21
o Local scope if the variable name doesn't already exist globally, where it
is then global.
• Global
o Forces creation of the variable in the Global scope
• Local
o Forces creation of the variable in the Local/Function scope.
You should use Local or Global, instead of Dim, to explicitly state which scope is
desired for a variable/constant/array. Examples of declaring variables are shown
below:
Dim $x, $y, $z ; Notice we can declare variables of the same scope on the
same line if desired. Global $RADIUS ; Global Variable
Local $_daysWorking ; Local Variable
4.2 The Naming Convention
This is a naming standard for variables, used in UDFs (User Defined Functions)
The first set of characters after the dollar sign (“$”) should be used to specify the type of
data that will be held in it. The following list signifies the different prefixes and their data
type significance.
$a<letter> - Array (the following letter describes the data type taken from the
rest of the data types below)
$b - Binary data
$h - File or window
handle $i - Integer
$f - Boolean
$n - Floating point
number $s - String
$v - Variant (unknown/variable type of data)
The rest of the name uses capitalized words to describe the function of the variable.
Names like “$iC” are unacceptable. "$aiWeekDayNames" or "$iCounter" are much
preferable.
All variables must be declared at the beginning of the UDF with a “Local” scope and before
they are used for the first time.
The “Dim” or “Global” keywords are ambiguous inside of a UDF and as such should be
avoided, all variables should be transferred via the Function Parameters using ByRef when
Page No.22
AUTO IT
22
the updated value needs to be returned.
4.3 Assigning variable values
A variable should have a value assigned to it before its value is requested. To do this the =
symbol is used. Here are some examples of assigning values to variables:
$number = 123 ; Some Number
$string = “Some String with “”double”” and ‘single’
quotes” ; A string $Return = SomeFunction (); The return
value of a function
Since a variable’s name can be given wherever a value is expected, it is possible to assign
the value of one variable to another.
$Var1 = 'West Richmond'
$Var2 = $Var1
After running the above code, both $Var1 and $Var2 will contain the value West Richmond.
If the value of one of these variables is subsequently changed, the other variable will still
contain the value West Richmond.
4.4 Accessing variable values
To use the value contained by a variable in place of some hard-coded value, simply
give the variable’s name wherever a value is expected. A simple example is below.
$MyFlag = 0
$MyTitle = "This is a
Title" $MyMsg =
"Hello there!"
MsgBox($MyFlag, $MyTitle, $MyMsg)
The value contained by $MyTitle will be used for the title of the resultant message box
(since the second parameter to MsgBox() sets the title) and the value contained by
$MyMsg will be displayed as the message, where the overall style of the message box is set
by $MyFlag.
4.5 Constants
One clever use of variables is to save unnecessary repetition. Consider the following code:
MsgBox(0, 'Example Script from Section 4', 'This is the first message box.')
MsgBox(0, 'Example Script from Section 4', 'This is the second message box.')
MsgBox(0, 'Example Script from Section 4', 'This is the third message box.')
Page No.23
AUTO IT
23
Here the string value
Example Script from Section 4 is used in multiple places.
Imagine this string value being used hundreds of times and then imagine the work that
would be required if the section number needed to be changed. By preparing a variable to
hold this data we can define it once (so that if it ever needs changing, it will only need
changing in one location) and improve the overall size (and readability) of the script.
Here’s a functionally identical script embracing this idea:
$Title = 'Example Script from Section 4'
MsgBox(0, $Title, 'This is the first
message box.')
MsgBox(0, $Title, 'This is the second
message box.') MsgBox(0, $Title, 'This is
the third message box.')
Notice how the value of $Title is set once and not changed again. Variables of this nature
are called constants as they are used for reading only (their values are constant). It is good
programming practice to explicitly declare variables as constants when that is their intended
use, because AutoIt can then protect the value of such a variable from being accidentally
changed later. This is done by adding the word Const before the variable name when its
value is initially declared, e.g.—
Const $Title = 'Example Script from Section 4'
Constants can be set in the same scopes as variables. See the following example:
Dim Const $Debugging = True
Global Const $FLAG1 = 543210
Local Const $PI = 3.14159
4.6 Macros
Macros are like predefined constants in that they can be used wherever a value is expected.
Refer to the help file for a complete list of macros. An example of use that includes Macros,
Variables is on the next page.
Example of Use:
Dim $version = @AutoItVersion ; AutoIt Version
Dim $computername = @ComputerName ;
Page No.24
AUTO IT
24
Computer Name Dim $OS = @OSVersion ; OS
Version
Dim $text = ""; Output text for message box.
Switch $OS ; A Switch statement. We are going to make the value returned
by @OSVERSION more readable.
Case "WIN_2008"
$OS = "Windows Server
2008" Case "WIN_VISTA"
$OS = "Windows
Vista" Case "WIN_2003"
$OS = "Windows Server
2003" Case "WIN_XP"
$OS =
"Windows XP" Case
"WIN_2000"
$OS = "Windows
2000" Case "WIN_NT4"
$OS = "Windows
NT 4.0" Case "WIN_ME"
$OS = "Windows
ME" Case "WIN_98"
$OS =
"Windows 98" Case
"WIN_95"
$OS =
"Windows 95" Case
Else
$OS = "Unknown!"
EndSwitch
$text = "AutoIt Version = " & $version & @CRLF ; The & character
concentrates (joins) two values together.
$text &= "Computer Name = " & $computername & @CRLF ; The &= adds to
the variable. The same as doing $var = $var & "extra stuff"
$text &= "Operating System = " &
$OS MsgBox (0, "Computer
Information", $text)
For me the script returns the following:
Page No.25
AUTO IT
25
In that script I also stuck you in the deep end, using Switch and Concentration
operators. Don’t worry if it doesn’t fully make sense now, we will go over them in a
later chapter.
4.7 Arrays
Arrays are variables that have the ability to store multiple values of data elements of the
same type and size. Each element in this variable can be accessed by an index number.
A common situation where arrays would be the easiest is as follows:
Say you want to store a list of people’s names, but you don’t know how many people’s
names you need to store. The most efficient (and the only “sensible” method) is to use
arrays. To store names such as, “Alex”, “Brett”, “Nicholas”, “Jos”, “Michael”, “George”, we
use an array. First off we must declare the array, and to do that we need to know how many
elements to create. So in my previous list we had 6. So now we can declare the array.
Dim $names[6]
It should be noted that the index for arrays start at 0. Now we can set the values of each
element.
$names[0] =
“Alex” $names[1]
= “Brett”
$names[2] =
“Nicholas”
$names[3] = “Jos”
$names[4] =
“Michael”
$names[5] =
“George”
Page No.26
AUTO IT
26
To access the variable names, it is much the same as normal variables, like so:
MsgBox (0, “Name”, $names[2])
Another method to declaring arrays like above is like so. Personally I prefer the above
method, as it makes it easier to see exactly what is being declared, and where.
Dim $names[6] = ["Alex", "Brett", "Nicholas", "Jos", "Michael", "George"]
MsgBox (0, "Name", $names[2])
This skims only the surface of arrays, as there is much more to them, such as multi
dimensional arrays, but as this is a basic tutorial, we won’t go into them.
5 Functions
When you get around to writing your own AutoIt scripts—which is soon—you will find
that the majority of any work is done by calling functions. A function accepts zero or more
parameters (i.e. input values), does its processing and then gives a return value (i.e. an
output value) as a result.
Here’s an example function call (which should look familiar):
MsgBox(64, 'Tutorial', 'Hello world!')
The name of the function is written to the left of the brackets. The parameters are entered
within the brackets and are separated by commas. Here, we are passing three values to the
MsgBox() function.
Functions may or may not have a set number of parameters—for instance, there is actually a
fourth argument to MsgBox() which assigns a timeout to the display of the message box. As
it is an optional parameter, we have not used it. The help file states which parameters are
optional for each function and what will be assumed if you do not specify them (in the case
of MsgBox(), if a timeout isn’t specified then the box will show indefinitely). If a function
doesn’t want any parameters at all then the brackets remain there, but are left empty:
FunctionWithoutInputs()
Page No.27
AUTO IT
27
The return value of the MsgBox() function is a number representing which button was
pressed, but in the above examples we don’t actually catch any return values to use them;
they are lost as a result. This is for two reasons:
1. It demonstrates that return values can be ignored. (We don’t need to know what
button the user pressed since we only offered one.)
2. We have no other choice as we have not yet discussed variables.
However, it is still possible to write useful scripts even if the output of every function call is
ignored. At this point you have been given the knowledge to be able to write scripts that:
• open, close, resize and focus windows;
• adjust controls within them;
• manipulate folders, files and their locations;
• send mouse clicks and keyboard input;
• shut down or restart the computer;
• start programs;
• play sounds through the computer’s speakers; or
• Take any linear sequence of the above actions.
All you need to do is refer to the AutoIt help file, find the functions you’d like to experiment
with and provide them with the right values.
5.1 Example function calls
5.1.1 Run() and RunWait()
Syntax:
Run ( "filename" [, "workingdir" [, flag[, standard_i/o_flag]]] )
RunWait ( "filename" [, "workingdir" [, flag]] )
These functions are used to start programs. Using Run() instructs AutoIt to continue with
the next instruction in the script immediately after the specified program starts, while
RunWait() forces AutoIt to wait until the specified program ends before continuing.
Try creating a new script (the tutorial taught you how to do this), pasting the following
lines into it and running it:
Run('Calc')
Run('Notepad')
MsgBox(64, 'Functions', 'I started Calculator and Notepad.')
Notice how both Calculator and Notepad open on your screen, and the message box appears
around the same time (possibly beneath the other windows). After closing them all, try
changing the Run() calls to RunWait() and running the script again—you will find this time
Page No.28
AUTO IT
28
that Notepad only appears after Calculator is closed, and the message box only appears after
Notepad is closed. Just in case your edit of the above code didn’t work, here an edited
version.
RunWait('Calc')
RunWait('Notepad')
MsgBox(64, 'Functions', 'I started Calculator and then Notepad.')
5.1.2 WinWait()
This function instructs AutoIt to wait until a window with a specific title exists. You pass this
function a string indicating what the title is (or how it starts).
Try this script:
Run('Calc')
MsgBox(64, 'Functions', 'Your Calculator is showing.')
In its current form it can’t be guaranteed that the message box will appear after Calculator
appears (especially on slower computers). This can be remedied by adding a WinWait() call
like so:
Run('Calc')
WinWait('Calculator')
MsgBox(64, 'Functions', 'Your Calculator is showing.')
Try changing the window title in the script above to Calculators and running it. You will
find that the message box never shows. This is because the titles have to be fairly exact for
the script to work properly (default behavior is for the script to match partial titles from the
start of the title). We will work in depth with titles in a later section.
5.1.3 Send()
This function will simulate keyboard input to the window that currently has focus. Try this
script (you may need to adjust the value passed to WinWait() if Notepad starts with a
different title on your system):
Run('Notepad')
WinWait('Untitled - Notepad')
Send('AutoIt is typing text{ENTER}into this window.')
Notice when you run this that the {ENTER} token is replaced with an actual press of the
Enter key. The appropriate help page will discuss other tokens that give this behavior.
6 Operators
In a basic sense, an operator is a function which operates on (or modifies) another function or
value.
Page No.29
AUTO IT
29
AutoIt has the following assignment, mathematical, comparison and logical operators.
Assignment
Operator Description
=
Assignment. E.G $VAR = “String” (Assigns “string” to
$VAR)
=
Addition assignment. E.G $VAR += 1 (adds 1 to
$VAR)
-= Subtraction assignment.
*= Multiplication assignment.
/= Division assignment.
&=
Concentration assignment. E.G $var = “one” and then $VAR &= “two”.
$VAR
becomes “onetwo”
Mathematica
l
Operator Description
+ Adds two numbers.
- Subtracts two numbers.
* Multiplies two numbers.
/ Divides two numbers.
& Concentrates/Joins two strings.
^ Raises a number to the power of another number.
Comparison
Operator Description
= Tests if two values are equal (String case-insensitive).
== Tests if two values are equal (String case-sensitive).
<> Tests if two values are not equal.
> Tests if the first value is greater than the second.
>= Tests if the first value is greater or equal to the second.
< Tests if the first value is less than the second.
<= Tests if the first value is less or equal to the second.
Logic
Operator Description
AND
Logical AND operation. e.g. If $var = 5 AND $var2 > 6
Then (True if $var equals 5
and $var2 is greater than 6)
OR
Logical OR operation. e.g. If $var = 5 OR $var2 > 6
Then (True if $var equals 5 or
$var2 is greater than 6)
NOT
Logical NOT operation. e.g. NOT
1 (False)
Page No.30
AUTO IT
30
These are used as described in the description of each. Here is a script showing example
of using operators:
;Assigning
$number_one = 20
$number_two = 10
$number_three = 5
$string1 = "String"
$string2 = "String
Two"
$number_three += $number_one
MsgBox (0, "Assignment",
$number_three) $number_three -=
$number_one
MsgBox (0, "Assignment",
$number_three) $number_three *=
$number_one
MsgBox (0, "Assignment",
$number_three) $number_three /=
$number_one
MsgBox (0, "Assignment",
$number_three) $number_three &=
$number_one MsgBox (0,
"Assignment", $number_three)
;Maths
$number_three = $number_one +
$number_two MsgBox (0, "Maths",
$number_three) $number_three =
$number_one - $number_two MsgBox
(0, "Maths", $number_three)
$number_three = $number_one *
$number_two MsgBox (0, "Maths",
$number_three) $number_three =
$number_one / $number_two MsgBox
Page No.31
AUTO IT
31
(0, "Maths", $number_three)
$number_three = $number_one &
$number_two MsgBox (0, "Maths",
$number_three) $number_three =
$number_one ^ 2
MsgBox (0, "Maths", $number_three)
;Comparison
If $number_one = 20 Then MsgBox (0, "Comparison", "Number 1 is 20!")
If $string1 == $string2 Then MsgBox (0, "Comparison", "Both strings
are the same!") If $number_two <> 20 Then MsgBox (0,
"Comparison", "Number 2 is not 20!")
If $number_two > 5 Then MsgBox (0, "Comparison", "Number 2 is greater than 5!")
If $number_two >= 10 Then MsgBox (0, "Comparison", "Number 2 is greater
or equal to 10!") If $number_one < 25 Then MsgBox (0, "Comparison",
"Number 1 is less than 25!")
If $number_one <= 20 Then MsgBox (0, "Comparison", "Number 1 is less or equal to
20!")
6.1 True or False?
It is worth mentioning the way AutoIt interprets True and False. True and false are
basically different values, which are outlined below:
True
• A string that is not null
• Numeric value that is not equal to zero.
False
• A null string ("")
• A numeric value that is equal to zero.
Here are some examples to AutoIt’s interpretation of the true and false operators.
$i_Value1 = 0
$i_Value2 = 0.0
$i_Value3 =
0.001 $i_Value4
Page No.32
AUTO IT
32
= 1000
$i_Value5 = 20
- 25 $s_Value1
= "" $s_Value2
= "0" $s_Value3
= "0.00"
$s_Value4 =
"toast"
If $i_Value1 Then
MsgBox (0, "$i_Value1", $i_Value1 & "=TRUE" & @CRLF)
Else
MsgBox (0, "$i_Value1", $i_Value1 & "=FALSE" & @CRLF)
EndIf
If $i_Value2 Then
MsgBox (0, "$i_Value2", $i_Value2 & "=TRUE" & @CRLF)
Else
MsgBox (0, "$i_Value2", $i_Value2 & "=FALSE" & @CRLF)
EndIf
If $i_Value3 Then MsgBox (0, "$i_Value3", $i_Value3 & "=TRUE" & @CRLF)
If $i_Value4 Then MsgBox (0, "$i_Value4", $i_Value4 & "=TRUE" & @CRLF)
If $i_Value5 Then MsgBox (0, "$i_Value5", $i_Value5 & "=TRUE" & @CRLF)
If $s_Value1 Then
MsgBox (0, "$s_Value1", $s_Value1 & "=TRUE" & @CRLF)
Else
MsgBox (0, "$s_Value1", $s_Value1 & "=FALSE" & @CRLF)
EndIf
If $s_Value2 Then MsgBox (0, "$s_Value2", $s_Value2 & "=TRUE" & @CRLF)
If $s_Value3 Then MsgBox (0, "$s_Value3", $s_Value3 & "=TRUE" & @CRLF)
If $s_Value4 Then MsgBox (0, "$s_Value4", $s_Value4 & "=TRUE" & @CRLF)
7 Branching
Branching is the process of creating scripts that carry out tasks conditionally. We can
then make scripts that are essentially failsafe. To create scripts with this in mind, we
must first know the various conditional functions.
7.1 Conditional Functions
Conditions are valued as True or False. Conditions generally use the conditional operators
like =, ==, <>, >, <, >= and <=. All of the statements are similar, and decide on what to
Page No.33
AUTO IT
33
execute based depending on the conditions given.
7.1.1 If…Then…Else
Here is an example of a simple If Statement.
$var = 20
If $var > 10 Then
MsgBox(0, "Example", "$var was greater than 10!")
Else
MsgBox(0, "Example", "$var was less than 10")
EndIf
7.1.2 Select Case
A select case is similar to an If statement, but is generally used for a larger set of
comparisons, as it is easier to read than a large If/ElseIf block.
$var = 30
Select
Case $var > 1 AND $var <= 10
MsgBox(0, "Example", "$var was greater than 1")
Case $var > 10 AND $var <= 20
MsgBox(0, "Example", "$var was greater than 10")
Case $var > 20 AND $var <= 30
MsgBox(0, "Example", "$var was greater than 20")
Case $var > 30 AND $var <= 40
MsgBox(0, "Example", "$var was greater than 30")
Case $var > 40
MsgBox(0, "Example", "$var was greater than 40")
EndSelect
7.1.3 Switch Case
Switch Case is similar to a Select statement, except is generally only used when the same
expression is compared multiple times against different values.
Switch Int($var)
Case 1 To 10
MsgBox(0, "Example", "$var was greater than 1")
Case 11 To 20
MsgBox(0, "Example", "$var was greater than 10")
Case 21 To 30
Page No.34
AUTO IT
34
MsgBox(0, "Example", "$var was greater than 20")
Case 31 To 40
MsgBox(0, "Example", "$var was greater than 30")
Case Else
MsgBox(0, "Example", "$var was greater than 40 or less or equal to 0")
EndSwitch
7.1.4 ContinueCase
Basically ContinueCase aborts the current case and continues into the next case
statement in a Select or Switch block.
An example showing use is below
$msg = ""
$szName = InputBox(Default, "Please enter a word.", "", " M", Default, Default,
Default, Default, 10)
Switch
@erro
r Case
2
$msg =
"Timeout "
ContinueCase
Case 1; Continuing previous
case $msg &=
"Cancellation"
Case 0
Switch $szName
Case "a", "e", "i", "o",
"u" $msg =
"Vowel"
Case "QP"
$msg =
"Mathematics" Case "Q"
To "QZ"
$msg =
"Science" Case Else
$msg = "Others"
EndSwitc
h Case Else
Page No.35
AUTO IT
35
$msg = "Something went horribly wrong."
EndSwitch
MsgBox(0, Default, $msg)
Loops
Loops are around to make coding easier, and to execute a block of code multiple times. Why
copy and paste code 20 times (with minimal or no adjustments) when you could write the
same block that has the same outcome with just a few extra lines?
AutoIt has 4 different Loops, but we only cover 3 as one loop is used exclusively with Objects.
8.1 Types of Loops
8.1.1 For…Next
Is a loop based on an expression.
The loops is made up of a declaration of the variable that is incremented upon commencing
each increment of the loop, the number of times the loop will increment, and the distance
between the increment of each loop. Example is below.
For $i = 5 to 1 Step -1
MsgBox(0, "Count down!", $i)
Next
MsgBox(0,"", "Blast Off!")
There is also another version of For...Next that is for working with arrays and Objects.
Example is below.
;Using an
Array Dim
$aArray[4]
$aArray[0] = "a"
$aArray[1] = 0
$aArray[2] =
Page No.36
AUTO IT
36
1.3434
$aArray[3] =
"test"
$string = ""
For $element In $aArray
$string = $string & $element & @CRLF
Next
MsgBox(0, "For..IN Arraytest", "Result is: " & @CRLF & $string)
8.1.2 While…Wend
A while loop is based on an expression. The loop continues until this expression is false. An
example is below. The expression is tested before the loop is executed so the loop will be
executed zero or more times. To create an infinite loop, use a non-zero number as the
expression.
$i = 0
While $i <= 10
MsgBox(0, "Value of $i
is:", $i) $i = $i + 1
Wend
;An infinite
loop While 1
ToolTip ("Looping Still!")
Wend
8.1.3 Do…Until
Similar to a While loop, except the expression is tested after the loop is executed. Take a look
at the example below.
$i =
0 Do
MsgBox(0, "Value of $i
is:", $i) $i = $i + 1
Until $i = 10
Page No.37
AUTO IT
37
8.1.4 ExitLoop
Quite simply terminates or breaks out of the current loop.
$sum = 0
While 1 ;use infinite loop since ExitLoop will
get called $ans = InputBox("Running
total=" & $sum,
" Enter a positive number. (A negative number
exits)") If $ans < 0 Then ExitLoop
$sum = $sum + $ans
WEnd
MsgBox(0, "The sum was", $sum)
8.1.5 ContinueLoop
ContinueLoop continues execution of the Loop’s code at the expression testing point. It
should be used with caution however, as infinite loops may be created with incorrect use.
;Print all numbers from 1 to 10 except number 7
For $i = 1 To 10
If $i = 7 Then ContinueLoop
MsgBox(0, "The value of $i
is:", $i)
Next
Graphical User Interface (GUI)
This allows you and your users to interact with your scripts. It can be as simple or elaborate as
you like, containing many controls, or just a few. In this section we will go through the basics
of creating a GUI and some controls, then on to how know when input is received.
Page No.38
AUTO IT
38
9.1 Creating the GUI
The first step to creating a GUI is to create the main window, where all the controls will be
placed. To do that we use the following function:
GUICreate( "title" [, width [, height [, left [, top [, style [, exStyle [, parent]]]]]]] )
Similar to how the MsgBox accepted a flag, most of the GUI functions (well the creation
functions) also have one! But in this case it sets 2 things- a style and an extended style. Have a
browse through the help file and read up on the styles, you’ll find many different ways of
altering the look and function of your GUI and its controls. It is important to note, that when
using multiple styles, you should use the function BitOr. Even though some examples in the
help file use the + operator to define multiple styles, it is preferred that you use BitOr.
Reasoning for this is as follows.
To help understand why we should use BitOr over other the + operator, it is important to
know how it works. In a nutshell, BitOr takes two or more integer values and returns the
bitwise "OR" on the "binary representation" of the integers. The return result is always an
integer, in the case that one of the parameters are zero/null, the zero/null parameter would not
be processed in the bitwise "OR". So as the help file shows, performing a bitwise operation
on 3 (0011 in binary) and 6 (0110 in binary) is 7 (0111 in binary), not 9(1001 in binary), as
the result would be using the + operator. To show how parameters that are null/zero aren’t
processed, we can add a 0 to our previous function. BitOr (3, 0, 6)returns 7, just
likeBitOr(3,6).
Page No.39
AUTO IT
39
Conclusion:
AutoIt automation script can be compiled into a compressed, stand-alone executable which
can be run on computers that do not have the AutoIt interpreter installed. A wide range of
function libraries (known as UDF's, or "User Defined Functions") are also included as
standard or are available from the website to add specialized functionality. AutoIt is also
distributed with an IDE based on the free Scite editor. The compiler and help text are fully
integrated and provide a de facto standard environment for developers using AutoIt.
Bibliography
(n.d.).
ananian,s.(1998, 09 02). cs. Retrieved1020, 2011, from.cs.princeton.edu:
www.cs.princeton.edu/appel/modern/java
Bennet,J.(2008, July27). autoit.Retrieved1116, 2011, from www.autoitscript.com:
http://www.autoitscript.com/site/autoit/downloads/
Erlacher,F. (2009, 11 02). homepage.Retrieved1021, 2011, fromuibk: http://homepage.uibk.ac.at
Erlacher,F. (11/02/2009). PushdownAutomata.20.
Flesner,A.(2007). AutoItv3. Dallas:O'ReillyMedia.
Moser,G. (2008). Discrete Methanatics.
Torres,J. (2004). WindowsAdmin Scripting Little Black Book. Texas:ParaglyphPress.
Page No.40
AUTO IT
40
Page No.41
AUTO IT
41

More Related Content

ODP
Nagios Conference 2012 - Sam Lansing - Automating Windows Application Testing...
PPT
Lets Auto It
PPT
PDF
Automation Testing with Sikuli
PDF
PPTX
Software Automation Testing Introduction
PPTX
4 legged walking robot
PPTX
Web testing automation
Nagios Conference 2012 - Sam Lansing - Automating Windows Application Testing...
Lets Auto It
Automation Testing with Sikuli
Software Automation Testing Introduction
4 legged walking robot
Web testing automation

Viewers also liked (20)

PPTX
Solar tree ppt
PPTX
Smarter ways to do selenium automation @ work, Selenium, automation
PPTX
Using AutoIt for Millennium Task Automation
PPT
Hexapod Presentation
PDF
Practical Sikuli: using screenshots for GUI automation and testing
DOCX
Selenium interview questions
PPTX
Hexapod - Robot
PPTX
Two wheel self balancing robot
PPT
Java Basics for selenium
PDF
Ccc 1314
PDF
Ts 2992
DOCX
aplication gogogo
PPT
Basic Object Oriented Concepts
DOCX
No tlp Polisi
PPTX
Alexander Alexandrovich Belov – The greatest basketball player of all time
PPT
цахим монгол
PPT
AdVoice
PDF
Food Processing Brochure 060316[1]
PDF
Bogovete na promqnata
PPT
Open Source & Open Development
Solar tree ppt
Smarter ways to do selenium automation @ work, Selenium, automation
Using AutoIt for Millennium Task Automation
Hexapod Presentation
Practical Sikuli: using screenshots for GUI automation and testing
Selenium interview questions
Hexapod - Robot
Two wheel self balancing robot
Java Basics for selenium
Ccc 1314
Ts 2992
aplication gogogo
Basic Object Oriented Concepts
No tlp Polisi
Alexander Alexandrovich Belov – The greatest basketball player of all time
цахим монгол
AdVoice
Food Processing Brochure 060316[1]
Bogovete na promqnata
Open Source & Open Development
Ad

Similar to AUtoIT_Masters Project (20)

PDF
Epics Qt design specification
PDF
Ora fin r11i - v4.1
PDF
TYPO3 Inline Relational Record Editing (IRRE)
PDF
Nanotechnology and Tissue Engineering The Scaffold 1st Edition Cato Laurencin
PDF
Parth Salat - 18BIT083 - Industry CP report.pdf
PDF
ANSYS Fluent - CFD Final year thesis
PDF
1227201 Report
PDF
Ms active directory_design_guide
PDF
A System For Citations Retrieval On The Web
PDF
Nanotechnology and Tissue Engineering The Scaffold 1st Edition Cato Laurencin
PDF
Comerv3 1-12(2)
DOCX
Semantic web design for www.data.gov.sg - Technical Report
PDF
Master's Thesis
PDF
Ontology Representation Design Patterns And Ontologies That Make Sense 1st Ed...
PDF
FinalYearReport_Angel_Francos-2_PDF-2
PDF
Sanskrit Parser Report
DOCX
PDF
Lab4_Report_460
PDF
ID3 Algorithm - Reference Manual
DOCX
Blood donation
Epics Qt design specification
Ora fin r11i - v4.1
TYPO3 Inline Relational Record Editing (IRRE)
Nanotechnology and Tissue Engineering The Scaffold 1st Edition Cato Laurencin
Parth Salat - 18BIT083 - Industry CP report.pdf
ANSYS Fluent - CFD Final year thesis
1227201 Report
Ms active directory_design_guide
A System For Citations Retrieval On The Web
Nanotechnology and Tissue Engineering The Scaffold 1st Edition Cato Laurencin
Comerv3 1-12(2)
Semantic web design for www.data.gov.sg - Technical Report
Master's Thesis
Ontology Representation Design Patterns And Ontologies That Make Sense 1st Ed...
FinalYearReport_Angel_Francos-2_PDF-2
Sanskrit Parser Report
Lab4_Report_460
ID3 Algorithm - Reference Manual
Blood donation
Ad

AUtoIT_Masters Project

  • 1. Page No.1 AUTO IT 1 PROJECT REPORT ON AUTO IT(Automation and Scripting Language) AUTOMATA & FORMAL LANGUAGES PROFF: DR.GORAN VIRGINIA INTERNATIONAL UNIVERSITY
  • 2. Page No.2 AUTO IT 2 TEAM MEMBERS: SAI DEERAJ PATURI VIJAY NARAYAN SUNNIKANTH BORRA
  • 3. Page No.3 AUTO IT 3 Original Document: Alex Peters (LxP) http://yallara.cs.rmit.edu.au/~apeters/ Updated By: Brett Francis (BrettF) http://signa5.com
  • 4. Page No.4 AUTO IT 4 Table of Contents Acknowledgements.....................................................................................................................7 Introduction.................................................................................................................................8 What Is AutoIt?......................................................................................................................8 Features of AutoIt..................................................................................................................8 1 Starting Off.............................................................................................................................8 1.1 Download and Install AutoIt V3.....................................................................................8 1.2 Install SciTE4AutoIt3 ......................................................................................................9 1.3 Know how to access the help file.....................................................................................10 1.4 Know what you want to achieve......................................................................................10 1.5 What is contained in this tutorial?....................................................................................10 2 Your First Steps ...................................................................................................................11 2.1 Creating and Organizing Script Files ............................................................................11 2.2.1 Organization of Scripts ...........................................................................................11 2.1.2 Creating a Script from Explorer..............................................................................11 2.1.3 Creating a Script from SciTE..................................................................................11 .......................................................................................................................................... 2.2 Hello World Example.....................................................................................................11 2.2. 1 The Steps ...............................................................................................................11 2.2.2 Explanation..............................................................................................................12 2.2.3 Extended Work.......................................................................................................12 2.2.4 The Code ................................................................................................................13 .......................................................................................................................................... .......................................................................................................................................... 2.3 Simple Notepad Automation..........................................................................................13
  • 5. Page No.5 AUTO IT 5 2.4. 1 The Steps ...............................................................................................................14 2.3.2 Explanation..............................................................................................................16 2.3.3 Extended Work.......................................................................................................16 2.3.4 The Code ................................................................................................................15 2.5 Exercies..........................................................................................................................16 3 Programming—it’s all about values.....................................................................................16 3.1 How values are used......................................................................................................16 3.2 Different types of values................................................................................................18 3.3 Values in Depth.............................................................................................................18 3.3.1 Numeric Values.......................................................................................................18 3.3.2 Strings......................................................................................................................18 3.3.3 Booleans..................................................................................................................19 3.4 Exercises ........................................................................................................................19 4 Arrays of Variables...............................................................................................................19 4.1 Declaring and naming variables.....................................................................................20 4.2 The Naming Convention................................................................................................22 4.3 Assigning variable values ..............................................................................................22 4.4 Accessing variable values..............................................................................................23 4.5 Constants........................................................................................................................22 4.6 Macros...........................................................................................................................23 4.7 Arrays ............................................................................................................................25 4.8 Exercises ........................................................................................................................25 5 Functions...............................................................................................................................26 5.1 Example function calls...................................................................................................27 5.1.1 Run() and RunWait()...............................................................................................27 5.1.2 WinWait()................................................................................................................28 5.1.3 Send()......................................................................................................................28 5.2 Exercises.....................................................................................................................28
  • 6. Page No.6 AUTO IT 6 6 Operators..............................................................................................................................28 6.1 True or False?................................................................................................................31 True...................................................................................................................................31 False..................................................................................................................................31 6.2 Exercises ........................................................................................................................31 7 Branching..............................................................................................................................32 7.1 Conditional Functions....................................................................................................32 7.1.1 If…Then…Else........................................................................................................33 7.1.2 Select Case..............................................................................................................33 7.1.3 Switch Case ............................................................................................................33 7.1.4 ContinueCase...........................................................................................................33 7.2 Exercises ........................................................................................................................33 8 Loops....................................................................................................................................35 8.1 Types of Loops..............................................................................................................35 8.1.1 For…Next...............................................................................................................35 8.1.2 While…Wend..........................................................................................................36 8.1.3 Do…Until................................................................................................................36 8.1.4 ExitLoop..................................................................................................................37 8.1.5 ContinueLoop..........................................................................................................37 8.2 Exercises ........................................................................................................................37 9 The Graphical User Interface (GUI) ....................................................................................37 9.1 Creating the GUI...........................................................................................................38 10. Conclusion………………………………………………………………………………….39 11.Bibilography ……………………………………………………………………………….39
  • 7. Page No.7 AUTO IT 7 Acknowledgements Alex Peters (LxP): Thank you for all the hard work put into the original tutorial! Without you, this probably would never exist! Introduction What Is AutoIt? AutoIt v3 is a freeware BASIC-like scripting language designed for automating the Windows GUI and general scripting. Features of AutoIt • Easy to learn BASIC-like syntax • Simulate keystrokes and mouse movements • Manipulate windows and processes • Interact with all standard windows controls • Scripts can be compiled into standalone executables • Create Graphical User Interfaces (GUIs) • COM support • Regular expressions • Directly call external DLL and Windows API functions • Scriptable RunAs functions • Detailed help file and large community-based support forums • Compatible with Windows 95 / 98 / ME / NT4 / 2000 / XP / 2003 / Vista / 2008 • Unicode and x64 support • Digitally signed for peace of mind • Works with Windows Vista's User Account Control (UAC)
  • 8. Page No.8 AUTO IT 8 1 Starting Off 1.1 Download and Install AutoIt V3 First of you will need to download and install the latest stable release of AutoIt. Although you have the option of downloading a Compressed .ZIP distribution, it is advised to download the installer, as it has many advantages, such as easier access to the help file, the ability to double-click AutoIt scripts to start them or to edit them, and the painless use of the Includes code library (A large library of various UDFs [we’ll get into that later]) in your scripts. Download Latest Installer for AutoIt: http://www.autoitscript.com/cgi-bin/getfile.pl?autoit3/autoit-v3-setup.exe 1.2 Install SciTE4AutoIt3 " SciTE is a Scintilla based Text Editor. Originally built to demonstrate Scintilla, it has grown to be a generally useful editor with facilities for building and running programs." – Niel Hodgson SciTE4AutoIt3 contains SciTE, wrapped into a single installer with all needed configuration settings and lots of utility programs like Tidy, Obfuscator, AU3Check, ScriptWriter, AutoIt3Wrapper to enhanced SciTE for use with AutoIt3. It contains around 10 tools for use and ease of scripting.
  • 9. Page No.9 AUTO IT 9 I recommend that you download SciTE, as scripting using this is much easier, especially with all the tools available for quick and easy use. 1.3 Know how to access the help file The help file that is included with every installation is very comprehensive, and contains examples for every almost every function. You can easily access the help file in one of two ways: Through All Programs: Start > All Programs > AutoIt V3 > AutoIt Help File Through SciTE: If you have installed SciTE, using the help file when scripting is much easier. When you press <F1>, the help file will open displaying the keyword that the cursor was on. It is very important to get comfortable with the help file, both with what it contains, and how to use it because it will be your primary source on how to use any specific function. This tutorial covers the basic use of functions, but does not go into great depth of every function. 1.4 Know what you want to achieve This is very important for any project you undertake. Knowing what you want to achieve is the starting point in your script, and in turn will make what you want to achieve much easier. A good idea is to sit down with a piece of paper, and just brainstorm ideas. A common topic that I see in the forums is “what to make next”. Some responses to that age old question are: • Start multiple programs and arrange them nicely on the screen. • Start a single program and adjust some values on the window, then hide it. • Periodically perform an action in some open program (without interrupting any work taking place at that time) • Shut down the computer after some lengthy operation (like a backup or perhaps a download) • Automate some ‘housekeeping’ chores such as backing up important files and emptying the Recycle Bin 1.6 What is contained in this tutorial? This tutorial teaches you basic knowledge with scripting in AutoIt. At the end of every chapter, there are some Chapter Review questions, and exercises for you to complete. Answers for the chapter review and some exercises can be found at on page 39. A list of Frequently Asked Questions (a must read after this tutorial!) can be found on page 72.
  • 10. Page No.10 AUTO IT 10 2 Your First Steps Your first steps are important, with how to code, and what practices you should pick up early, to make you a more productive coder. To start off though, we will work through some examples, explaining what we are doing, and what everything means. 2.1 Creating and Organizing Script Files Creating a script file is the first step in starting to create your scripts. 2.2.1 Organization of Scripts Organization is one of the many keys to being able to create scripts (especially large projects) efficiently. It is always a good idea to keep your scripts contained in one folder, so that you always know where files are, and in some cases, what they are. For an example of how you could go about organizing this folder, we will look at what I have done. Basically in “My Documents” I have an “AutoIt” folder. The folders inside the “AutoIt” folder house my projects, scripts and programs from the Example Scripts Forum, that I like, or may want to build upon and study later, and a special folder which I dedicate to solutions of where I or others have helped people on the forums. Having it set up this way means I can easily find my scripts, examples for support in the forums or for personal use, and easy offline access to a large range of UDF libraries. 2.1.2 Creating a Script from Explorer 1. Navigate to any folder in Windows Explorer (I am in “My DocumentsAutoItTutorials”) 2. Right Click and select New > AutoIt Script 3. Type your filename in. Leave “.au3” intact if it is visible.
  • 11. Page No.11 AUTO IT 11 2.1.3 Creating a Script from SciTE SciTE is the editor you will be using for most if not all of your scripting, so it is important that you can0020at least create a script using it. 1. Open SciTE from Start > All Programs > AutoIt v3 > SciTE > SciTE 2. Go File > New or press <CTRL> + <N> to create a new script. 3. Save your script by File > Save As or <CTRL> + <SHIFT> + <S>. When saving a file in SciTE, it is very important that you make sure the file has .au3 (the file extension for AutoIt V3 scripts) on the end, as not having it will causing it to be saved as a plain file. 2.2 Hello World Example This is a modified version of the ‘Hello World’ tutorial available in the AutoIt V3 Help file (Under Tutorials). Like the tutorial it assumes that you have AutoIt V3 installed, and SciTE. This tutorial will teach you how to display a message box by use of native function, explain parameters of functions, the use of numeric flags, and basic use of strings and comments. It also discusses basic use of the help file. 2.2. 1 The Steps 1. Create a new script using the previously discussed explorer method and open it for editing in SciTE. 2. The code you can see in SciTE is comments. These are ignored by the interpreter. The character to make comments is a semi-colon (;). These can be placed at the start of lines, or after functions. 3. Now we want to tell AutoIt to display a message box. To do that, we have to use the MsgBox function. After the last comment, add the following line: MsgBox(0, "Tutorial", "Hello World!")
  • 12. Page No.12 AUTO IT 12 4. Save the script. 5. Run the script by either a. Right Click > Run Script on the saved .au3 file in explorer b. In SciTE, going to Tools > Go or pressing <F5> 6. Congratulations! You have just created your first script. 2.2.2 Explanation Almost every function in AutoIt will take a parameter. Parameters define what is to be done in a function, and changes things, such as text and how certain things are displayed. For this example, the MsgBox takes three parameters - a flag, a title and a message. The first parameter, the flag is a number that changes the way MsgBox displays. In this example we used 0. The next 2 parameters, the title and message are both strings. When using strings in AutoIt you are able to surround the text in double or single quotes. Both "This is some text" and 'This is some text' will work fine. Later on in this book we will discuss strings in more depth. 2.2.3 Extended Work So we used the function MsgBox, gave it a title and a message, and ran it. But we can do more to make the message box gain more attention, or to make it look different to standard. To change the effect that our message box created, we can alter the flag parameter. With the flag we can change the icon, some display options, what buttons are displayed, and much more. So open the help file and find the entry for MsgBox. As you can see, there is the title of the function, a short abstract of what the function does, the parameters of the function, return values, the remarks section, containing the flags to alter our message box and some more information that may help the reader, related functions, and an example.
  • 13. Page No.13 AUTO IT 13 Now to change how our message box displays. As you can see by the picture above we can change the icon displayed, and the buttons. So how can we do that? In the remarks section the help file, there is a table containing the flags for changing the message box. To use more than one flag add them together. Experiment with different values and see what you get. Remember, add the flag values together, and if you run your script and nothing happens, there is no need for alarm. It is more than likely that you have made a mistake in adding the values together. An invalid flag will not display a message box, so be careful when adding values together. One of the simplest methods for combining flags is using a calculator, and it is also fairly foolproof. 2.2.4 The Code This is the full code for this example and some extended examples. ; Script Start - Add your code below here ;Basic Example MsgBox(0, "Tutorial", "Hello World!") ;Extended Work MsgBox(18, "Tutorial", "Hello World!"); Abort, Retry, Ignore with Stop Icon MsgBox(32, "Tutorial", "Hello World!"); Normal message box with exclamation icon. MsgBox(12345, "Tutorial", "Hello World!") ; A message box with and invalid flag. This will NOT display. 2.3 Simple Notepad Automation This tutorial is about the automation of notepad. This tutorial will teach you basic use of the AutoIt v3 Window Info Tool, give you an introduction to Send, to window titles, to the Run function, to some Win* functions, and show you some basic automation. In the extended task, window states and Sleep function
  • 14. Page No.14 AUTO IT 14 are also explored, as well as some basic macros. 2.4. 1 The Steps 1. Create a new script and open it for editing. 2. The first thing we need to do is run Notepad. To do that we get the Notepad executable (notepad.exe). We got that from looking at the short cut to notepad in the Start Menu. So to execute notepad.exe, we must use the run function. And the following line into your script. Run(“notepad.exe”) 3. Save the script and run it. If all goes well, we should have a new instance of notepad appear. Don’t close this instance just yet, we still need to work with it in the next few steps. 4. When automating applications AutoIt can check for window title so that it knows which window it should work with. With Notepad the window title is Untitled - Notepad. AutoIt is case-sensitive when using window titles so you must get the title exactly right - the best way to do this is to use the AutoIt Window Information Tool. Run the Information Tool from Start Menu > AutoIt v3 > AutoIt Window Info, or since we are in SciTE, Tools > AU3Info, or use the hotkey <CTRL> + <F6>. 5. With the Info Tool open click on the newly opened Notepad window to activate it; the Info Tool will give you information about it. The information we are interested in is the window title. While in the notepad window, press <CTRL> + <ALT> + <F> to freeze the Info tool. That way we can access the information we just gathered about notepad. 6. Highlight the title in the AutoIt Info Tool window and press CTRL-C to copy it to the clipboard - we can then paste the title into our script without fear of misspelling it.
  • 15. Page No.15 AUTO IT 15 7. After running a copy of Notepad we need to wait for it to appear and become active before we send any keystrokes. We can wait for a window using the WinWaitActive function. Most window-related functions in AutoIt take a window title as a parameter. Enter the following as the second line in the script (use <CTRL> + <V> or Edit > Paste to paste our window title from the clipboard). Add the following line into the script: WinWaitActive(“Untitled – Notepad”) 1. After we are sure the Notepad window is visible we want to type in some text. This is done with the Send function. Now we will add this line to our script. Send("This is some text.") 2. Close the copy of Notepad that we previously opened (you will need to do this every time you run the script otherwise you will end up with lots of copies running!). Run the script. 3. You should see Notepad open, and then some text will magically appear! 8. Next we want to close notepad, we can do this with the WinClose function. WinClose("Untitled - Notepad") 4. When Notepad tries to close you will get a message asking you to save the changes. Use the Window Info Tool to get details of the dialog that has popped up so that we can respond to it :) 9. So, we add a line to wait for this dialog to become active (we will also use the window text to make the function more reliable and to distinguish this new window from the original Notepad window): WinWaitActive("Notepad", "Do you want to save") 10. Next we want to automatically press Alt-N to select the No/Don't save button (Underlined letters in windows usually indicate that you can use the ALT key and that letter as a keyboard shortcut). In the Send function to send an ALT key we use an exclamation point (!). 11.
  • 16. Page No.16 AUTO IT 16 Send("!n") 5. Run the script and you will see Notepad open, some text appears, then it closes! 2.3.2 Explanation Basically what we did in this script was run notepad, using the function Run. The only parameter we had was the path. Then we waited for the window to open, sent text, and then closed it. It was pretty much all explained as we went. 2.3.3 Extended Work So what if we wanted to send new lines, or stop the user from interfering from the text being sent? First off, we will start with making the sent text have new lines. Find the following line in your script: Send("This is some text.") And replace it with the following: Send("This is some text." & @CRLF & "This is on a new line! ") Notice we added the & (a string concentrator, meaning we can join 2 values together. For example $var = “one” & “two” will have $var equal “onetwo”. ‘&’ is an operator, which is a character which AutoIt interprets to assign variables, perform mathematical operations, comparison of values and logical operations. For a complete list, look in the help file under ‘Operators’. We also used the macro @CRLF, which is a carriage return and a line feed. Now how about stopping the user from interfering with our automation? For that we have a simple command, BlockInput. It is very simple to achieve this. We just have to add the following line at the start of the script. BlockInput (1) And this line at the end, to enable input for the user again. BlockInput (0) Have a look in the help file for BlockInput, and read more about it.
  • 17. Page No.17 AUTO IT 17 3 Programming—it’s all about values 3.1 How values are used This is Alex’s theory of values. I think it is very effective in describing what they are in relation to AutoIt. “In my opinion the most essential thing to realize when it comes to programming is this: it’s not much more than a game of throwing values around” Justification for that includes: The script made as part of the 2.2 Hello World Example instructs AutoIt to display a message box. To do this three values are needed: one dictating the style of the box (like icon and buttons), one for the title and one for the message. Suppose that you wanted to write a simple number guessing game. You would have a value for the number to guess and a value for what the user has entered as their guess. You would compare those two values to determine if the user has made a correct guess, and then you would probably display one of two human-readable messages informing them of the outcome, which are also values. On the very other end of the spectrum, perhaps you want to write a utility that updates the Windows operating system. You would have values to indicate which files are to be manipulated. You would open the files specified by those values and search for certain values in each file to determine whether you need to make adjustments. If you do, you would overwrite some part of each file with a new set of values. It can be seen rather clearly from the above scenarios that values can take many forms: numbers, characters, strings of characters, binary data or even a combination. For instance, these are all potential values: 12 3 Ab c 0.5 ZM Z . - = _ ? + ` This is a sentence! Page 17
  • 18. Page No.18 AUTO IT 18 In my opinion, understanding and thinking about things in terms of values should make any coding task much easier. Consider what values might be involved for any task that you plan to undertake; by knowing what values will be needed, you’ll be better prepared to know exactly how these values must be manipulated to get the job done. (You’ll also know exactly what to look for in the help file since AutoIt instructions are named according to what they do.) In the next few sections we will be looking at exactly what these values are, and different methods of storing the data. 3.2 Different types of values There are many different types of values. We have already encountered two- “Numeric Values”, “String Values”. Those values are probably the most important, and the most encountered value type. Common values you will encounter in AutoIt include: • Strings • Numerical Values • Booleans We will go through them in detail in the next section. 3.3 Values in Depth In the previous section we briefly discussed different types of values. Now we will go in depth on what they are, and then in the next section, how to store them. 3.3.1 Numeric Values Numeric values are entered into a script in the same fashion as one would enter numbers into spreadsheet software—that is, digit grouping symbols (also known as thousand separators) are not used, and numbers are made negative by placing a minus sign (-) to the front. Regardless of the standard notation for your region, a period is used to represent a decimal point (e.g. 124.45 and not 123,45). -1 ; Negative Number 10 ; Integer 10000 ; Ten Thousand 1.5 ; One and a half or One point five 3.3.2 Strings Since the value of a string can be virtually anything, AutoIt needs to be told where strings
  • 19. Page No.19 AUTO IT 19 start and end. This is necessary to prevent symbols belonging to the string from being interpreted by AutoIt in another way, and is done by placing a single quote (') or double quote (") at both ends of the string. (You may have noticed that the script from the tutorials uses double quotes.) When a string doesn’t contain either type of quote, whichever one you choose to surround the string makes no difference If a string should contain one type of quote but not the other then it makes sense to use that other type of quote to surround the string. What happens though if the string should contain both types of quote? In such a case you must nominate which quote you will use to surround the string, and then double up on any occurrences of that quote within the string. Here’s an example of a line of text containing both types of quote: I'm a string with single and "double" quotes If we wanted to surround the string with double quotes then we would double up on the double quotes within the string, giving us this: "I'm a string with single and ""double"" quotes." If we wanted to instead use single quotes then we would double up on the single quote and this would be the result: 'I''m a string with single and "double" quotes.' Both of these are complete strings, and should give you an understanding on how to use strings, as they are, or with single and double quotation marks. 3.3.3 Booleans Booleans have 2 basic values- True or False. They are used in Logical expressions. A basic script showing use of Booleans is below: $bool= False If NOT $bool = True Then Msgbox(0,"Bool comparison", "OK") ; Basically the above line tests if the variable $bool is not true (in other words, false). 4 Arrays of Variables Variables are areas in computer memory, identified by name, that hold values. The value
  • 20. Page No.20 AUTO IT 20 contained within a variable could come from the script itself, from a function (through its return value), from some other calculation (potentially involving other variables) or from any combination of these during the running lifetime of a script. A variable’s value can be read any number of times and it can be overwritten with some new value as often as desired. 4.1 Declaring and naming variables A variable’s name consists of a dollar sign ($) followed by any combination of one or more letters, numbers and the underscore character (_). Technically the $ is not part of the variable name itself, but it is used to indicate to AutoIt that it is a variable. Here are some examples of valid (although not necessarily advisable) variable names: $MyVar $Msg $WAIT_TIME $Answer $_ ABC_ 123 $MsgBox $19283 It should be noted that variable names are case-insensitive, which means that the names below would all point to the same variable in memory: $A BC $A Bc $Ab C $Ab c $aB C $aB c $ab C $ab c When declaring variables you have the option of declaring it in 3 scopes-
  • 21. Page No.21 AUTO IT 21 o Local scope if the variable name doesn't already exist globally, where it is then global. • Global o Forces creation of the variable in the Global scope • Local o Forces creation of the variable in the Local/Function scope. You should use Local or Global, instead of Dim, to explicitly state which scope is desired for a variable/constant/array. Examples of declaring variables are shown below: Dim $x, $y, $z ; Notice we can declare variables of the same scope on the same line if desired. Global $RADIUS ; Global Variable Local $_daysWorking ; Local Variable 4.2 The Naming Convention This is a naming standard for variables, used in UDFs (User Defined Functions) The first set of characters after the dollar sign (“$”) should be used to specify the type of data that will be held in it. The following list signifies the different prefixes and their data type significance. $a<letter> - Array (the following letter describes the data type taken from the rest of the data types below) $b - Binary data $h - File or window handle $i - Integer $f - Boolean $n - Floating point number $s - String $v - Variant (unknown/variable type of data) The rest of the name uses capitalized words to describe the function of the variable. Names like “$iC” are unacceptable. "$aiWeekDayNames" or "$iCounter" are much preferable. All variables must be declared at the beginning of the UDF with a “Local” scope and before they are used for the first time. The “Dim” or “Global” keywords are ambiguous inside of a UDF and as such should be avoided, all variables should be transferred via the Function Parameters using ByRef when
  • 22. Page No.22 AUTO IT 22 the updated value needs to be returned. 4.3 Assigning variable values A variable should have a value assigned to it before its value is requested. To do this the = symbol is used. Here are some examples of assigning values to variables: $number = 123 ; Some Number $string = “Some String with “”double”” and ‘single’ quotes” ; A string $Return = SomeFunction (); The return value of a function Since a variable’s name can be given wherever a value is expected, it is possible to assign the value of one variable to another. $Var1 = 'West Richmond' $Var2 = $Var1 After running the above code, both $Var1 and $Var2 will contain the value West Richmond. If the value of one of these variables is subsequently changed, the other variable will still contain the value West Richmond. 4.4 Accessing variable values To use the value contained by a variable in place of some hard-coded value, simply give the variable’s name wherever a value is expected. A simple example is below. $MyFlag = 0 $MyTitle = "This is a Title" $MyMsg = "Hello there!" MsgBox($MyFlag, $MyTitle, $MyMsg) The value contained by $MyTitle will be used for the title of the resultant message box (since the second parameter to MsgBox() sets the title) and the value contained by $MyMsg will be displayed as the message, where the overall style of the message box is set by $MyFlag. 4.5 Constants One clever use of variables is to save unnecessary repetition. Consider the following code: MsgBox(0, 'Example Script from Section 4', 'This is the first message box.') MsgBox(0, 'Example Script from Section 4', 'This is the second message box.') MsgBox(0, 'Example Script from Section 4', 'This is the third message box.')
  • 23. Page No.23 AUTO IT 23 Here the string value Example Script from Section 4 is used in multiple places. Imagine this string value being used hundreds of times and then imagine the work that would be required if the section number needed to be changed. By preparing a variable to hold this data we can define it once (so that if it ever needs changing, it will only need changing in one location) and improve the overall size (and readability) of the script. Here’s a functionally identical script embracing this idea: $Title = 'Example Script from Section 4' MsgBox(0, $Title, 'This is the first message box.') MsgBox(0, $Title, 'This is the second message box.') MsgBox(0, $Title, 'This is the third message box.') Notice how the value of $Title is set once and not changed again. Variables of this nature are called constants as they are used for reading only (their values are constant). It is good programming practice to explicitly declare variables as constants when that is their intended use, because AutoIt can then protect the value of such a variable from being accidentally changed later. This is done by adding the word Const before the variable name when its value is initially declared, e.g.— Const $Title = 'Example Script from Section 4' Constants can be set in the same scopes as variables. See the following example: Dim Const $Debugging = True Global Const $FLAG1 = 543210 Local Const $PI = 3.14159 4.6 Macros Macros are like predefined constants in that they can be used wherever a value is expected. Refer to the help file for a complete list of macros. An example of use that includes Macros, Variables is on the next page. Example of Use: Dim $version = @AutoItVersion ; AutoIt Version Dim $computername = @ComputerName ;
  • 24. Page No.24 AUTO IT 24 Computer Name Dim $OS = @OSVersion ; OS Version Dim $text = ""; Output text for message box. Switch $OS ; A Switch statement. We are going to make the value returned by @OSVERSION more readable. Case "WIN_2008" $OS = "Windows Server 2008" Case "WIN_VISTA" $OS = "Windows Vista" Case "WIN_2003" $OS = "Windows Server 2003" Case "WIN_XP" $OS = "Windows XP" Case "WIN_2000" $OS = "Windows 2000" Case "WIN_NT4" $OS = "Windows NT 4.0" Case "WIN_ME" $OS = "Windows ME" Case "WIN_98" $OS = "Windows 98" Case "WIN_95" $OS = "Windows 95" Case Else $OS = "Unknown!" EndSwitch $text = "AutoIt Version = " & $version & @CRLF ; The & character concentrates (joins) two values together. $text &= "Computer Name = " & $computername & @CRLF ; The &= adds to the variable. The same as doing $var = $var & "extra stuff" $text &= "Operating System = " & $OS MsgBox (0, "Computer Information", $text) For me the script returns the following:
  • 25. Page No.25 AUTO IT 25 In that script I also stuck you in the deep end, using Switch and Concentration operators. Don’t worry if it doesn’t fully make sense now, we will go over them in a later chapter. 4.7 Arrays Arrays are variables that have the ability to store multiple values of data elements of the same type and size. Each element in this variable can be accessed by an index number. A common situation where arrays would be the easiest is as follows: Say you want to store a list of people’s names, but you don’t know how many people’s names you need to store. The most efficient (and the only “sensible” method) is to use arrays. To store names such as, “Alex”, “Brett”, “Nicholas”, “Jos”, “Michael”, “George”, we use an array. First off we must declare the array, and to do that we need to know how many elements to create. So in my previous list we had 6. So now we can declare the array. Dim $names[6] It should be noted that the index for arrays start at 0. Now we can set the values of each element. $names[0] = “Alex” $names[1] = “Brett” $names[2] = “Nicholas” $names[3] = “Jos” $names[4] = “Michael” $names[5] = “George”
  • 26. Page No.26 AUTO IT 26 To access the variable names, it is much the same as normal variables, like so: MsgBox (0, “Name”, $names[2]) Another method to declaring arrays like above is like so. Personally I prefer the above method, as it makes it easier to see exactly what is being declared, and where. Dim $names[6] = ["Alex", "Brett", "Nicholas", "Jos", "Michael", "George"] MsgBox (0, "Name", $names[2]) This skims only the surface of arrays, as there is much more to them, such as multi dimensional arrays, but as this is a basic tutorial, we won’t go into them. 5 Functions When you get around to writing your own AutoIt scripts—which is soon—you will find that the majority of any work is done by calling functions. A function accepts zero or more parameters (i.e. input values), does its processing and then gives a return value (i.e. an output value) as a result. Here’s an example function call (which should look familiar): MsgBox(64, 'Tutorial', 'Hello world!') The name of the function is written to the left of the brackets. The parameters are entered within the brackets and are separated by commas. Here, we are passing three values to the MsgBox() function. Functions may or may not have a set number of parameters—for instance, there is actually a fourth argument to MsgBox() which assigns a timeout to the display of the message box. As it is an optional parameter, we have not used it. The help file states which parameters are optional for each function and what will be assumed if you do not specify them (in the case of MsgBox(), if a timeout isn’t specified then the box will show indefinitely). If a function doesn’t want any parameters at all then the brackets remain there, but are left empty: FunctionWithoutInputs()
  • 27. Page No.27 AUTO IT 27 The return value of the MsgBox() function is a number representing which button was pressed, but in the above examples we don’t actually catch any return values to use them; they are lost as a result. This is for two reasons: 1. It demonstrates that return values can be ignored. (We don’t need to know what button the user pressed since we only offered one.) 2. We have no other choice as we have not yet discussed variables. However, it is still possible to write useful scripts even if the output of every function call is ignored. At this point you have been given the knowledge to be able to write scripts that: • open, close, resize and focus windows; • adjust controls within them; • manipulate folders, files and their locations; • send mouse clicks and keyboard input; • shut down or restart the computer; • start programs; • play sounds through the computer’s speakers; or • Take any linear sequence of the above actions. All you need to do is refer to the AutoIt help file, find the functions you’d like to experiment with and provide them with the right values. 5.1 Example function calls 5.1.1 Run() and RunWait() Syntax: Run ( "filename" [, "workingdir" [, flag[, standard_i/o_flag]]] ) RunWait ( "filename" [, "workingdir" [, flag]] ) These functions are used to start programs. Using Run() instructs AutoIt to continue with the next instruction in the script immediately after the specified program starts, while RunWait() forces AutoIt to wait until the specified program ends before continuing. Try creating a new script (the tutorial taught you how to do this), pasting the following lines into it and running it: Run('Calc') Run('Notepad') MsgBox(64, 'Functions', 'I started Calculator and Notepad.') Notice how both Calculator and Notepad open on your screen, and the message box appears around the same time (possibly beneath the other windows). After closing them all, try changing the Run() calls to RunWait() and running the script again—you will find this time
  • 28. Page No.28 AUTO IT 28 that Notepad only appears after Calculator is closed, and the message box only appears after Notepad is closed. Just in case your edit of the above code didn’t work, here an edited version. RunWait('Calc') RunWait('Notepad') MsgBox(64, 'Functions', 'I started Calculator and then Notepad.') 5.1.2 WinWait() This function instructs AutoIt to wait until a window with a specific title exists. You pass this function a string indicating what the title is (or how it starts). Try this script: Run('Calc') MsgBox(64, 'Functions', 'Your Calculator is showing.') In its current form it can’t be guaranteed that the message box will appear after Calculator appears (especially on slower computers). This can be remedied by adding a WinWait() call like so: Run('Calc') WinWait('Calculator') MsgBox(64, 'Functions', 'Your Calculator is showing.') Try changing the window title in the script above to Calculators and running it. You will find that the message box never shows. This is because the titles have to be fairly exact for the script to work properly (default behavior is for the script to match partial titles from the start of the title). We will work in depth with titles in a later section. 5.1.3 Send() This function will simulate keyboard input to the window that currently has focus. Try this script (you may need to adjust the value passed to WinWait() if Notepad starts with a different title on your system): Run('Notepad') WinWait('Untitled - Notepad') Send('AutoIt is typing text{ENTER}into this window.') Notice when you run this that the {ENTER} token is replaced with an actual press of the Enter key. The appropriate help page will discuss other tokens that give this behavior. 6 Operators In a basic sense, an operator is a function which operates on (or modifies) another function or value.
  • 29. Page No.29 AUTO IT 29 AutoIt has the following assignment, mathematical, comparison and logical operators. Assignment Operator Description = Assignment. E.G $VAR = “String” (Assigns “string” to $VAR) = Addition assignment. E.G $VAR += 1 (adds 1 to $VAR) -= Subtraction assignment. *= Multiplication assignment. /= Division assignment. &= Concentration assignment. E.G $var = “one” and then $VAR &= “two”. $VAR becomes “onetwo” Mathematica l Operator Description + Adds two numbers. - Subtracts two numbers. * Multiplies two numbers. / Divides two numbers. & Concentrates/Joins two strings. ^ Raises a number to the power of another number. Comparison Operator Description = Tests if two values are equal (String case-insensitive). == Tests if two values are equal (String case-sensitive). <> Tests if two values are not equal. > Tests if the first value is greater than the second. >= Tests if the first value is greater or equal to the second. < Tests if the first value is less than the second. <= Tests if the first value is less or equal to the second. Logic Operator Description AND Logical AND operation. e.g. If $var = 5 AND $var2 > 6 Then (True if $var equals 5 and $var2 is greater than 6) OR Logical OR operation. e.g. If $var = 5 OR $var2 > 6 Then (True if $var equals 5 or $var2 is greater than 6) NOT Logical NOT operation. e.g. NOT 1 (False)
  • 30. Page No.30 AUTO IT 30 These are used as described in the description of each. Here is a script showing example of using operators: ;Assigning $number_one = 20 $number_two = 10 $number_three = 5 $string1 = "String" $string2 = "String Two" $number_three += $number_one MsgBox (0, "Assignment", $number_three) $number_three -= $number_one MsgBox (0, "Assignment", $number_three) $number_three *= $number_one MsgBox (0, "Assignment", $number_three) $number_three /= $number_one MsgBox (0, "Assignment", $number_three) $number_three &= $number_one MsgBox (0, "Assignment", $number_three) ;Maths $number_three = $number_one + $number_two MsgBox (0, "Maths", $number_three) $number_three = $number_one - $number_two MsgBox (0, "Maths", $number_three) $number_three = $number_one * $number_two MsgBox (0, "Maths", $number_three) $number_three = $number_one / $number_two MsgBox
  • 31. Page No.31 AUTO IT 31 (0, "Maths", $number_three) $number_three = $number_one & $number_two MsgBox (0, "Maths", $number_three) $number_three = $number_one ^ 2 MsgBox (0, "Maths", $number_three) ;Comparison If $number_one = 20 Then MsgBox (0, "Comparison", "Number 1 is 20!") If $string1 == $string2 Then MsgBox (0, "Comparison", "Both strings are the same!") If $number_two <> 20 Then MsgBox (0, "Comparison", "Number 2 is not 20!") If $number_two > 5 Then MsgBox (0, "Comparison", "Number 2 is greater than 5!") If $number_two >= 10 Then MsgBox (0, "Comparison", "Number 2 is greater or equal to 10!") If $number_one < 25 Then MsgBox (0, "Comparison", "Number 1 is less than 25!") If $number_one <= 20 Then MsgBox (0, "Comparison", "Number 1 is less or equal to 20!") 6.1 True or False? It is worth mentioning the way AutoIt interprets True and False. True and false are basically different values, which are outlined below: True • A string that is not null • Numeric value that is not equal to zero. False • A null string ("") • A numeric value that is equal to zero. Here are some examples to AutoIt’s interpretation of the true and false operators. $i_Value1 = 0 $i_Value2 = 0.0 $i_Value3 = 0.001 $i_Value4
  • 32. Page No.32 AUTO IT 32 = 1000 $i_Value5 = 20 - 25 $s_Value1 = "" $s_Value2 = "0" $s_Value3 = "0.00" $s_Value4 = "toast" If $i_Value1 Then MsgBox (0, "$i_Value1", $i_Value1 & "=TRUE" & @CRLF) Else MsgBox (0, "$i_Value1", $i_Value1 & "=FALSE" & @CRLF) EndIf If $i_Value2 Then MsgBox (0, "$i_Value2", $i_Value2 & "=TRUE" & @CRLF) Else MsgBox (0, "$i_Value2", $i_Value2 & "=FALSE" & @CRLF) EndIf If $i_Value3 Then MsgBox (0, "$i_Value3", $i_Value3 & "=TRUE" & @CRLF) If $i_Value4 Then MsgBox (0, "$i_Value4", $i_Value4 & "=TRUE" & @CRLF) If $i_Value5 Then MsgBox (0, "$i_Value5", $i_Value5 & "=TRUE" & @CRLF) If $s_Value1 Then MsgBox (0, "$s_Value1", $s_Value1 & "=TRUE" & @CRLF) Else MsgBox (0, "$s_Value1", $s_Value1 & "=FALSE" & @CRLF) EndIf If $s_Value2 Then MsgBox (0, "$s_Value2", $s_Value2 & "=TRUE" & @CRLF) If $s_Value3 Then MsgBox (0, "$s_Value3", $s_Value3 & "=TRUE" & @CRLF) If $s_Value4 Then MsgBox (0, "$s_Value4", $s_Value4 & "=TRUE" & @CRLF) 7 Branching Branching is the process of creating scripts that carry out tasks conditionally. We can then make scripts that are essentially failsafe. To create scripts with this in mind, we must first know the various conditional functions. 7.1 Conditional Functions Conditions are valued as True or False. Conditions generally use the conditional operators like =, ==, <>, >, <, >= and <=. All of the statements are similar, and decide on what to
  • 33. Page No.33 AUTO IT 33 execute based depending on the conditions given. 7.1.1 If…Then…Else Here is an example of a simple If Statement. $var = 20 If $var > 10 Then MsgBox(0, "Example", "$var was greater than 10!") Else MsgBox(0, "Example", "$var was less than 10") EndIf 7.1.2 Select Case A select case is similar to an If statement, but is generally used for a larger set of comparisons, as it is easier to read than a large If/ElseIf block. $var = 30 Select Case $var > 1 AND $var <= 10 MsgBox(0, "Example", "$var was greater than 1") Case $var > 10 AND $var <= 20 MsgBox(0, "Example", "$var was greater than 10") Case $var > 20 AND $var <= 30 MsgBox(0, "Example", "$var was greater than 20") Case $var > 30 AND $var <= 40 MsgBox(0, "Example", "$var was greater than 30") Case $var > 40 MsgBox(0, "Example", "$var was greater than 40") EndSelect 7.1.3 Switch Case Switch Case is similar to a Select statement, except is generally only used when the same expression is compared multiple times against different values. Switch Int($var) Case 1 To 10 MsgBox(0, "Example", "$var was greater than 1") Case 11 To 20 MsgBox(0, "Example", "$var was greater than 10") Case 21 To 30
  • 34. Page No.34 AUTO IT 34 MsgBox(0, "Example", "$var was greater than 20") Case 31 To 40 MsgBox(0, "Example", "$var was greater than 30") Case Else MsgBox(0, "Example", "$var was greater than 40 or less or equal to 0") EndSwitch 7.1.4 ContinueCase Basically ContinueCase aborts the current case and continues into the next case statement in a Select or Switch block. An example showing use is below $msg = "" $szName = InputBox(Default, "Please enter a word.", "", " M", Default, Default, Default, Default, 10) Switch @erro r Case 2 $msg = "Timeout " ContinueCase Case 1; Continuing previous case $msg &= "Cancellation" Case 0 Switch $szName Case "a", "e", "i", "o", "u" $msg = "Vowel" Case "QP" $msg = "Mathematics" Case "Q" To "QZ" $msg = "Science" Case Else $msg = "Others" EndSwitc h Case Else
  • 35. Page No.35 AUTO IT 35 $msg = "Something went horribly wrong." EndSwitch MsgBox(0, Default, $msg) Loops Loops are around to make coding easier, and to execute a block of code multiple times. Why copy and paste code 20 times (with minimal or no adjustments) when you could write the same block that has the same outcome with just a few extra lines? AutoIt has 4 different Loops, but we only cover 3 as one loop is used exclusively with Objects. 8.1 Types of Loops 8.1.1 For…Next Is a loop based on an expression. The loops is made up of a declaration of the variable that is incremented upon commencing each increment of the loop, the number of times the loop will increment, and the distance between the increment of each loop. Example is below. For $i = 5 to 1 Step -1 MsgBox(0, "Count down!", $i) Next MsgBox(0,"", "Blast Off!") There is also another version of For...Next that is for working with arrays and Objects. Example is below. ;Using an Array Dim $aArray[4] $aArray[0] = "a" $aArray[1] = 0 $aArray[2] =
  • 36. Page No.36 AUTO IT 36 1.3434 $aArray[3] = "test" $string = "" For $element In $aArray $string = $string & $element & @CRLF Next MsgBox(0, "For..IN Arraytest", "Result is: " & @CRLF & $string) 8.1.2 While…Wend A while loop is based on an expression. The loop continues until this expression is false. An example is below. The expression is tested before the loop is executed so the loop will be executed zero or more times. To create an infinite loop, use a non-zero number as the expression. $i = 0 While $i <= 10 MsgBox(0, "Value of $i is:", $i) $i = $i + 1 Wend ;An infinite loop While 1 ToolTip ("Looping Still!") Wend 8.1.3 Do…Until Similar to a While loop, except the expression is tested after the loop is executed. Take a look at the example below. $i = 0 Do MsgBox(0, "Value of $i is:", $i) $i = $i + 1 Until $i = 10
  • 37. Page No.37 AUTO IT 37 8.1.4 ExitLoop Quite simply terminates or breaks out of the current loop. $sum = 0 While 1 ;use infinite loop since ExitLoop will get called $ans = InputBox("Running total=" & $sum, " Enter a positive number. (A negative number exits)") If $ans < 0 Then ExitLoop $sum = $sum + $ans WEnd MsgBox(0, "The sum was", $sum) 8.1.5 ContinueLoop ContinueLoop continues execution of the Loop’s code at the expression testing point. It should be used with caution however, as infinite loops may be created with incorrect use. ;Print all numbers from 1 to 10 except number 7 For $i = 1 To 10 If $i = 7 Then ContinueLoop MsgBox(0, "The value of $i is:", $i) Next Graphical User Interface (GUI) This allows you and your users to interact with your scripts. It can be as simple or elaborate as you like, containing many controls, or just a few. In this section we will go through the basics of creating a GUI and some controls, then on to how know when input is received.
  • 38. Page No.38 AUTO IT 38 9.1 Creating the GUI The first step to creating a GUI is to create the main window, where all the controls will be placed. To do that we use the following function: GUICreate( "title" [, width [, height [, left [, top [, style [, exStyle [, parent]]]]]]] ) Similar to how the MsgBox accepted a flag, most of the GUI functions (well the creation functions) also have one! But in this case it sets 2 things- a style and an extended style. Have a browse through the help file and read up on the styles, you’ll find many different ways of altering the look and function of your GUI and its controls. It is important to note, that when using multiple styles, you should use the function BitOr. Even though some examples in the help file use the + operator to define multiple styles, it is preferred that you use BitOr. Reasoning for this is as follows. To help understand why we should use BitOr over other the + operator, it is important to know how it works. In a nutshell, BitOr takes two or more integer values and returns the bitwise "OR" on the "binary representation" of the integers. The return result is always an integer, in the case that one of the parameters are zero/null, the zero/null parameter would not be processed in the bitwise "OR". So as the help file shows, performing a bitwise operation on 3 (0011 in binary) and 6 (0110 in binary) is 7 (0111 in binary), not 9(1001 in binary), as the result would be using the + operator. To show how parameters that are null/zero aren’t processed, we can add a 0 to our previous function. BitOr (3, 0, 6)returns 7, just likeBitOr(3,6).
  • 39. Page No.39 AUTO IT 39 Conclusion: AutoIt automation script can be compiled into a compressed, stand-alone executable which can be run on computers that do not have the AutoIt interpreter installed. A wide range of function libraries (known as UDF's, or "User Defined Functions") are also included as standard or are available from the website to add specialized functionality. AutoIt is also distributed with an IDE based on the free Scite editor. The compiler and help text are fully integrated and provide a de facto standard environment for developers using AutoIt. Bibliography (n.d.). ananian,s.(1998, 09 02). cs. Retrieved1020, 2011, from.cs.princeton.edu: www.cs.princeton.edu/appel/modern/java Bennet,J.(2008, July27). autoit.Retrieved1116, 2011, from www.autoitscript.com: http://www.autoitscript.com/site/autoit/downloads/ Erlacher,F. (2009, 11 02). homepage.Retrieved1021, 2011, fromuibk: http://homepage.uibk.ac.at Erlacher,F. (11/02/2009). PushdownAutomata.20. Flesner,A.(2007). AutoItv3. Dallas:O'ReillyMedia. Moser,G. (2008). Discrete Methanatics. Torres,J. (2004). WindowsAdmin Scripting Little Black Book. Texas:ParaglyphPress.