Tricks

From AutoIt Wiki
Revision as of 06:03, 8 August 2013 by Jaberwocky6669 (talk | contribs) (Added Debugging Code Easily header from SciTE4AutoIt3 article)
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.

AutoIt Configuration

Has it's own page called Tricks Autoit Configuration

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