Tricks

From AutoIt Wiki
Jump to navigation Jump to search

This page is intended to list simple but useful tricks.

Show Error Message and Exit in One line

The following line shows an error message and exits the script with an error code of 99 if $x is bigger than 5:

If $x > 5 Then Exit 99 + 0 * MsgBox(0, "Error", "$x is bigger than 5!")

The trick works as follows: When $x is bigger than 5, the statement after the "Then" is executed. This statement consists of a calculation. While parsing this calculation, AutoIt notes that it has to execute the MsgBox() function to be able to fill in all values into the calculation. However, since the value can change depending on which button is used to close the box, we need to eliminate the return code from our calculation. That's why the return value of the MsgBox() function is multiplied by 0. Since we don't want 0 to be the error code we'll just add 99 to it. Now AutoIt is able to execute the Exit command so the script exits at this point.

Save a Variable to a File

A variable's value can be saved to a file for later use. For example if you have a variable called $Data and you wanted to save it for later just use IniWrite.

IniWrite("Variables.ini", "Main", "Data", $Data)

To get it back later just do.

$Data = IniRead("Variables.ini", "Main", "Data", '')

You can even get it back after the script has exited and restarted.

Debugging Code Easily

debug("This is a test")
debug("Jump to this line")
debug("just by clicking my output line")

Func debug($msg, $error = @error, $extended = @extended, $erl = @ScriptLineNumber)
    ConsoleWrite("(" & $erl & ") : = (" & $error & ")(" & $extended & ") " & $msg & @LF)
    If $error <> 0 Then SetError($error, $extended, $error)
    Return $error
EndFunc   ;==>debug

Add Your Own Include Path

When you have used AutoIt for a while you probably will have several UDFs in one or more files of which you would like to keep track. You can add the path to those files in the registry. If you do this then AutoIt will pick them up as any other include files. For instance, if you want to include an au3 containing UDFs then you have to either put the au3 in the same directory as the script or you have to type the path to the au3 in the include statement. The following code eliminates the need to do this as long as the au3 is in the path which is supplied to the function.

; AutoIt V3 3.3.8.1

#include <File.au3>

_setIncludePaths([A path to a folder which contains au3 files.])

Func _setIncludePaths(Const $myIncludes)	
	If Not $myIncludes Then
		Return SetError(1, 0, False)
	EndIf
	
	Local Const $includeLocations = RegRead("HKEY_CURRENT_USER\Software\AutoIt v3\AutoIt", "Include")
	
	If Not @error Then	
		Local Const $myIncludesList = _FileListToArray($myIncludes, "*.au3", 1)
		
		If Not @error Then
			Local $myIncludesString = $includeLocations
			
			For $i = 1 to $myIncludesList[0]
				$myIncludesString &= ';' & $myIncludesList[$i]
			Next
			
			If $myIncludesString Then
	 			RegWrite("HKEY_CURRENT_USER\Software\AutoIt v3\AutoIt", "Include", "REG_SZ", ';' & $myIncludesString)
			EndIf
		EndIf
	EndIf
EndFunc