Professional Documents
Culture Documents
AutoIt Automation Notes
AutoIt Automation Notes
autoit is case-sensitive
Default keyword
Example:
Comments
https://www.autoitscript.com/wiki/Tutorial_Core_Language
; : this is a comment
Another way is to use #cs (comment start) and #ce (comment end)
Variables
https://www.autoitscript.com/wiki/Tutorial_Core_Language
Global
Local
Dim
https://www.autoitscript.com/wiki/Tutorial_Core_Language
A variable declared globally (with the Global keyword) is visible anywhere in the script.
Always declare your global variables in the global scope, not in a function. It will prevent
another function from using it before its declaration and the declaration is implicit (see
examples).
A variable declared locally (with the Local keyword), has a visibility which depends of the
scope where it's declared.
Declaration in the global scope: the variable is visible everywhere; declare it as Local if this
one is only to be used in the same scope, i.e. outside of any functions. A variable declared as
Local in the Global scope is still a Global variable, it's only for the sake of code clarity that
you'd use the Local declaration in the Global scope.
Declaration in a function: the variable is visible by the function itself and nowhere else.
Declaration:
Variable assignment
https://www.autoitscript.com/wiki/Tutorial_Core_Language
VarGetType(exp)
Array
How to empty an array
ReDim $aOrderNoList[UBound($aOrderNoList)+1]
Escape character
AutoIt doesn’t seem to have escape characters (my opinion), so there are guidelines for different
situations:
To escape the ampersand character, add 1 more ampersand character, for ex:
o $button = GUICtrlCreateButton("Open && Print", 17, 40, 73, 30)
Clipboard
The clipboard usage below may not work as expected
_ClipBoard_SetData($custID, $CF_TEXT)
Perhaps because $custID contains number, or this number has leading 0 or ending 0.
Because of this, the value of $custID isn’t copied into the clipboard. Use the workaround
below:
o _ClipBoard_SetData("" & $custID & "", $CF_TEXT)
Unexpected case:
when there is nothing to copy (for ex: copy nothing in an empty notepad++ file), the
clipboard retains the previous value copied to it
#include vs #include-once
Message Box
Display a message box
#include <MsgBoxConstants.au3>
Operators
https://www.autoitscript.com/wiki/Tutorial_Core_Language
Branching
https://www.autoitscript.com/wiki/Tutorial_Core_Language
If-ElseIf-Else
Select
Switch
String Management
When working with String functions (for ex: StringINStr, StringLeft), keep in mind that @CRLF
contains two characters (not just one)
StringSplit
StringSplit($my_string, @CRLF, $STR_ENTIRESPLIT)
Remember to add the flag ENTIRESPLIT at the end; otherwise, extra blank lines will be
generated as the string is split on both @CR and @LF
Loop
https://www.autoitscript.com/wiki/Tutorial_Core_Language
Functions
Built-in functions
Send
From my experience, should sleep after a send command (ex: sleep(50))
For presentation, press the Tab key twice and then enter sleep command like this:
Send($cust_id)
Sleep(50)
Send("{TAB}")
Sleep(50)
Send($email)
Sleep(50)
HotKeySet
We can define a keyset to stop the script on the spot should any issues arise:
When using AutoIt with a GUI version, remember to place the HotKeySet function above the
continuous while loop to register this HotKeySet assignment. The function StopExit() can be placed
anywhere.
HotKey can sometimes interfere with normal keyboard functions, hence they should be disabled
when not in use or when normal keyboard functions are used.
MouseClick("Left", 860, 672, 1): if we use 1 (instead of 0) as the last parameter for this
function, we can see the cursor moving effect -> cool
File I/O
https://www.autoitscript.com/wiki/Tutorial_Core_Language
_FileCountLines
GUI
Align labels
Check the following example:
Same width
Use the $SS_RIGHT style
GUICtrlCreateGraphic(5,25,390,2,$SS_SUNKEN)
GUICtrlCreateLabel("",5,25,390,2,$SS_SUNKEN)
; GUICtrlCreateInput
0: initial top position
Example:
o (100, iTopCoor+0, 50, 20)
o (100, iTopCoor+30, 50, 20) ; (30 = 0 + 20 + 10)
o (100, iTopCoor+60, 50, 20) ; (60 = 30 + 20 + 10)
; GUICtrlCreateLabel
Example:
o (10, iTopCoor+3, 70, 20)
o (10, iTopCoor+33, 70, 20)
o (10, iTopCoor+63, 70, 20)
These Const(s) Base Values are for the following functions. Initially, they are all 0.
GUICtrlCreateInput
GUICtrlCreateLabel
GUICtrlCreateInput
Toggle between readonly and editable guictrlcreateinput
GUICtrlSetStyle($answer, $ES_READONLY)
GUICtrlSetStyle($answer, $GUI_SS_DEFAULT_EDIT)
--------------------------------------------------------------------------------
https://www.autoitscript.com/wiki/UDF-spec
Scope naming convention
(under the section "scopes of variables")
declare a var as Global in a Global scope -> visible any where in the script -> any functions
can use it, & some functions will use the var
declare a var as Local in a Global scope -> visible any where in the script -> however, it is
NOT used in any functions (this is AutoIt convention, not rules)
declare a var as Local in a function -> used only within that function
declare a var inside a loop still makes the var visible within the function, not only within the
loop itself
o Declaring variables inside loops should be avoided as the variable is re-declared on
each repetition
no need to declare the iteration count variable -> usually used within the loop only
Empty an array
o
And some others
However, for var declaration, limit the use of Dim, and use Local/Global instead
Local $topic_file
LoadTopic($topic_file)
EndFunc
EndFunc
---
Look at the example above, if there is no keyword ByRef (as highlighted above), the topic_file in the
LoadTopic func is a separate variable to the original one, and this is the var which gets initiated (not
the original). So, when the func LoadTopic is done, the original topic_file still does not have any
value which is wrong. There are 2 solutions:
Emergency stop
Wait for sth to happen (do in this order which is from the most automatic method to the least):
http://www.binaryhexconverter.com/hex-to-decimal-converter
PixelCheckSum: check if a region of pixels has changed
use "TogglePause" function
Use WinWaitActive
use Sleep
When we use the function MouseClick("Left", 860, 672, 1), sometimes the cursor gets frozen a bit. In
this case, we need to sleep a bit before continuing:
sleep(50)
Another technique for more automation (rather than human intervention) is verifying data being
entered (used especially on an input field or a dropdown menu on a web page):
Notification of completion
Use any of the following methods:
MsgBox
ToolTip
Beep
o Beep(500, 1000)
; 500 is the tone, and, 1000 is the length of time in mili-seconds.
Portable AutoIt
To make AutoIt portable on different platform, parameterize these:
Coordinate values
MouseMove, MouseClick
PixelGetColor
https://www.autoitscript.com/forum/topic/136766-isn-autoit-studio/
AutoIt debugger
https://www.autoitscript.com/forum/topic/21834-graphical-autoit-debugger/
SCiTE4AutoIt
Silent mode
Assign a value to a cell:
o $workbook.ActiveSheet.Cells($loopvar,1).Value = "text"
To get a value from a cell:
$cell = $workbook.ActiveSheet.Cells($loopvar,1).Value
$oRange = $workbook.Application.ActiveCell
ConsoleWrite($oRange.Column & @TAB & $oRange.Row & @LF)
However, when getting a value from a cell using the above method (silent mode), I got the following
problems: