My AutoIt script sends a list of clicks and key-presses to automate an old closed source application.
It has bugs so I want to know how I can debug AutoIt scripts. Or at least output the script's line number (to show code executed in real time).
My AutoIt script sends a list of clicks and key-presses to automate an old closed source application.
It has bugs so I want to know how I can debug AutoIt scripts. Or at least output the script's line number (to show code executed in real time).
In SciTE from Tools select “Trace: Add trace lines”. This will add a ConsoleWrite to each line if nothing is selected. If you select some code first it will add ConsoleWrite lines to what you have selected.
If you are getting an error in your compiled code you can add this to the top of your script before compiling it. When it errors out it will give you the right line number in your script.
Syntax
Au3Check
As per Documentation - Intro - AutoIt Syntax Checker (Au3Check):
Example:
Reports (almost) every non-runtime error, prior to execution and compile.
Runtime
Current line
As per Documentation - Function Reference -
AutoItSetOption()
:Example:
Compiled scripts
#AutoIt3Wrapper_Run_Debug_Mode=Y
runs script with console debugging.Due to prepended include files, line numbers (in error messages) of compiled scripts do not match original .au3 file. As per Documentation - SciTE4AutoIt3 - Au3Stripper:
Example:
Line numbers in error messages of compiled scripts match those in
scriptname_stripped.au3
now (present after compile).Error codes and return values
ConsoleWrite()
. Add#AutoIt3Wrapper_Change2CUI=y
if to be read from compiled script.@error
,@extended
and@ScriptLineNumber
SciTE4AutoIt3
>Tools
>Trace: Add Trace Lines
inserts such a command for every line. Displays current@error
code (and concerning line).SciTE4AutoIt3
>Tools
>Debug to Console
(Alt + D) inserts such a command for currently selected line. Displays@ScriptLineNumber
, return value and@error
code. Executes original line a second time for its return value (may be undesired).VarGetType()
returns the internal type representation of a variant._ArrayDisplay()
to view structure and content of array variables.Assertions
As per Documentation - User Defined Function Reference -
_Assert()
:Related. Useful for regression tests and to verify edge cases and assumptions (or to simply abort in case of unexpected circumstances). Related functions include:
IsBinary()
IsBool()
IsDllStruct()
IsFloat()
IsHWnd()
IsInt()
IsNumber()
IsPtr()
IsString()
IsDeclared()
IsFunc()
IsKeyWord()
FileExists()
Error handling
Generally functions return either- (or a combination of) return value or -error code. Non
0
error code values (sometimes negative) signify failure. Return values (if used as such) alternate between0
(failure) and1
(success).Handle errors as they appear. Scripts debug themselves provided for correct error handling. Avoid progressively nesting
If
-statements. Example:Or more specifically:
Return values enable constructs like:
Correlating error codes to text messages may help. Especially useful if shared among multiple related functions that call upon each other transparently returning encountered error codes (some
FindOnPage()
returning$ERR_PAGELOAD
from aLoadPage()
dependency for example).