Tricks: Difference between revisions

From AutoIt Wiki
Jump to navigation Jump to search
m (Added Debugging Code Easily header from SciTE4AutoIt3 article)
m (Syntax highlighting minor structure)
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 ===
==== 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.
The following line shows an error message and exits the script with an error code of 99 if $x is bigger than 5:


=== Save a Variable to a File ===
<syntaxhighlight lang="AutoIt">
If $x > 5 Then Exit 99 + 0 * MsgBox(0, "Error", "$x is bigger than 5!")
</syntaxhighlight>
 
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.
<pre>IniWrite("Variables.ini","Main","Data",$Data) </pre>
<syntaxhighlight lang="AutoIt">
IniWrite("Variables.ini", "Main", "Data", $Data)
</syntaxhighlight>


To get it back later just do.
To get it back later just do.
<pre>$Data = IniRead("Variables.ini","Main","Data","") </pre>
<syntaxhighlight lang="AutoIt">
$Data = IniRead("Variables.ini", "Main", "Data", '')
</syntaxhighlight>
 
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 ===
==== Debugging Code Easily ====
 
Has it's own page called [[Tricks Autoit Configuration]]
 
== Debugging Code Easily ==


<syntaxhighlight lang="AutoIt">
<syntaxhighlight lang="AutoIt">
Line 32: Line 47:
EndFunc  ;==>debug
EndFunc  ;==>debug
</syntaxhighlight>
</syntaxhighlight>
==== See Also ====
[[Tricks Autoit Configuration | Autoit Configuration]]

Revision as of 06:09, 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.

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

See Also

Autoit Configuration