Tricks: Difference between revisions

From AutoIt Wiki
Jump to navigation Jump to search
 
m (Added Debugging Code Easily header from SciTE4AutoIt3 article)
Line 1: Line 1:
== This page is intended to list simple but useful tricks. ==
== 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:<pre>If $x > 5 Then Exit 99 + 0*MsgBox(0, 'Error', '$x is bigger than 5!')</pre>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.


=== Show Error Message and Exit in one line ===
=== Save a Variable to a File ===
The following line shows an error message and exits the script with an error code of 99 if $x is bigger than 5:<pre>If $x > 5 Then Exit 99 + 0*MsgBox(0, 'Error', '$x is bigger than 5!')</pre>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.
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.
For example if you have a variable called $Data and you wanted to save it for later just use IniWrite.
Line 15: Line 15:
You can even get it back after the script has exited and restarted.
You can even get it back after the script has exited and restarted.


=== AutoIt configuration ===
=== AutoIt Configuration ===
 
Has it's own page called [[Tricks Autoit Configuration]]
Has it's own page called [[Tricks Autoit Configuration]]
== Debugging Code Easily ==
<syntaxhighlight lang="AutoIt">
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
</syntaxhighlight>

Revision as of 06:03, 8 August 2013

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