You are on page 1of 11

General

autoit is case-sensitive

Default keyword
Example:

 GUICtrlCreateLabel("Reference", 10, $GUIComponentsGap, Default, Default, $SS_RIGHT)

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

3 Keywords used when declare a variable:

 Global
 Local
 Dim

Their scopes definition

 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:

 AutoItSetOption("MustDeclareVars", 1) is active, then variables must be declared prior to


use.

Variable assignment

https://www.autoitscript.com/wiki/Tutorial_Core_Language

A String variable is surrounded by either double quotes or single quotes

Retrieve variable type:

 VarGetType(exp)

Array
How to empty an array

 Check the “dim usage scenarios” section

create a dynamic array by resizing an existing 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)

Key code for _IsPressed function


Check the function _IsPressed (for ex: 2D is Insert key)

ASCII character codes


Search the phrase in the autoit help file

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>

MsgBox($MB_OK, "Tutorial", "Hello World!")

Extended message box


https://www.autoitscript.com/forum/topic/109096-extended-message-box-new-version-20-sep-17/

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))

Send($custID & "{ENTER}") (combine keys in a send command)

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.

Mouse Management Functions


Use MouseClick with Coordinate parameters instead of Mousemove and then mouseclick

 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

Call a function when AutoIt exits


Use the OnAutoItExitRegister() function
User defined functions
https://www.autoitscript.com/wiki/Tutorial_Core_Language

File I/O
https://www.autoitscript.com/wiki/Tutorial_Core_Language

_FileCountLines

 Returns the number of lines in the specified file

How to delete a line in a text file

 Use the function _FileWriteToLine()


 If _FileWriteToLine() is called with $iOverWrite as 1 and $sText as "", it will delete the line.

GUI

Align labels
Check the following example:

GUICtrlCreateLabel("Mobile No", 10, 283, 70, 20, $SS_RIGHT)

Local $mobiNo_x = GUICtrlCreateInput("", 100, 280, 50, 20)

Local $mobiNo_y = GUICtrlCreateInput("", 160, 280, 50, 20)

GUICtrlCreateLabel("Invoice No", 10, 313, 70, 20, $SS_RIGHT)

Local $invoiceNo_x = GUICtrlCreateInput("", 100, 310, 50, 20)

Local $invoiceNo_y = GUICtrlCreateInput("", 160, 310, 50, 20)

To align labels, follow these 2 guidelines:

 Same width
 Use the $SS_RIGHT style

Draw a horizontal line

Use either of these functions

 GUICtrlCreateGraphic(5,25,390,2,$SS_SUNKEN)
 GUICtrlCreateLabel("",5,25,390,2,$SS_SUNKEN)

Base value for a group of GUI components


Guideline on how to implement this (check out the following example):

; GUICtrlCreateInput
0: initial top position

20: height of each CtrlInput

10: gap between each CtrlInput

 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

3: initial top position

20: height of each CtrlInput

10: gap between each CtrlInput

 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

 Global Const $iLeftCoor = 0


 Global Const $iTopCoor = 0
 Global Const $iWidthCoor = 0
 Global Const $iHeightCoor = 0

GUICtrlCreateInput
Toggle between readonly and editable guictrlcreateinput

 GUICtrlSetStyle($answer, $ES_READONLY)
 GUICtrlSetStyle($answer, $GUI_SS_DEFAULT_EDIT)

For an input GUI that is editable, use “GUICtrlCreateEdit”

Koda – GUI Builder


Install SciTE4AutoIt3, then open the editor / Tools / Koda

Create a dialog with no minimize, maximize, and close buttons


Local $progress_bar_gui = GUICreate($title, 250, 110, 950, 5, $DS_MODALFRAME)

Set the background color of a control


GUICtrlSetBkColor($percent_label, $GUI_BKCOLOR_TRANSPARENT)
Exit programs even when looping continuously
Check the example script

--------------------------------------------------------------------------------

Best coding practices


https://www.autoitscript.com/wiki/Best_coding_practices

https://www.autoitscript.com/wiki/UDF-spec

Variable Naming Convention

Use the schema:


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

Function naming conventions

Dim usage scenarios


Some usages:

 Empty an array

o
 And some others
 However, for var declaration, limit the use of Dim, and use Local/Global instead

ByRef vs Global variable


Func MainGUI()

Local $topic_file

LoadTopic($topic_file)

EndFunc

Func LoadTopic(ByRef $topic_file)

Local $topic_file_path = FileOpenDialog("Select a topic", "G:\New


data\AutoScr\MemoryExercise\topics", "All txt files (*.txt)")

$topic_file = FileOpen($topic_file_path, $FO_READ)

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:

 Use the keyword ByRef, OR


 Declare topic_file as a global variable
--------------------------------------------------------------------------------

Emergency stop

Check out the HotKeySet function

Wait for something to happen-More automation

Wait for sth to happen (do in this order which is from the most automatic method to the least):

 PixcelGetColor (as good as the function PixelCheckSum)


o RGB to Decimal Tool:
 https://www.mathsisfun.com/hexadecimal-decimal-colors.html
o Hex to Decimal Converter tool:
 Get the decimal value from the AutoIt v3 Window Info


 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

IDE for AutoIt

ISN autoit studio

 https://www.autoitscript.com/forum/topic/136766-isn-autoit-studio/

AutoIt debugger

 https://www.autoitscript.com/forum/topic/21834-graphical-autoit-debugger/

SCiTE4AutoIt

AutoIt with Microsoft Office


Excel
Interactive mode

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

To get row & column of a selected cell

 $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:

 The script runs very slowly, OR


o This perhaps is due to the performance of this single-cell reading method
o The slowness of it may cause new problems because the value from a cell may not
have been finished reading, but the script continues running on, which may
overwrite the previously unused value with a new one
 I got the error: “The requested action with this object has failed” (failed at the “value”
position)
o I am not sure why
 To fix the slowness and the error, better use new ways of assigning & getting values from an
excel cell:
o $array_values = _Excel_RangeRead($workbook, Default, "D" & $loopvar)
 We may use a for loop afterwards, like the following:
 For $orderNo_item In $orderNo_array
 Be careful though, _Excel_RangeRead may return a single value (not
an array) -> verify before using the For to loop through it
o _Excel_RangeWrite(…….)