Jump to content

General Tips for using AutoIt


Recommended Posts

Based on Valuaters excellent AutoIt Wrappers thread (Still not a sticky??) I'm going to attempt something similar here with examples that cover things that you can do to avoid problems when you are using AutoIt. It will include both GUI and non-GUI tips. Please feel free to add your own but please make sure they are accurate. If not you will be getting a PM to either Edit or Remove the reply.

If you see any errors in my replies, please PM me so it can be corrected.

If our supreme commander or any of The mods fell this thread should be removed or moved then please do so and PM me to let me know.

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

I'm not exactly super experienced with Autoit; however I am quite experienced in structured programming languages like C++ and Pascal.

Since Autoit is a structured language, it shares allot of common things with other languages, so learning the basics of structured programming will help you be good(better) at Autoit.

I would suggest all new Autoit users to start learning loops, variables and arrays then moving on to things like pixelsearch and controlsend. I just had to say this, because there’s tons of threads like these every day, when a person doesn't understand how a loop works and tries to make a valid script.

Another great tip to learning Autoit is using the helpfile, it has detailed explanations on the syntax and every function in Autoit. If you want to send a key, try typing "Send" in the help search. It will show Function Reference to any commands related to send.

I hope these simple tips will help :)

Edited by Qousio
Link to comment
Share on other sites

How to avoid GUI flicker issues in loops.

When setting the control state always check the current state first.

If BitAnd(GUICtrlGetState($hMyControl), $GUI_DISABLE) Then GUICtrlSetState($hMyControl, $GUI_ENABLE)
;

When setting control data

In this case I want to make sure the control is always lower case during the message loop.

This will cause flicker every time the string is checked to see if it's lower case when the control contains punctuation or whitespace.

;
$sString = GUICtrlRead($hMyControl)
If NOT StringisLower($sString ) Then GUICtrlSetData($hMyControl, StringLower($sString ))
;

This won't

;
$sString = GUICtrlRead($hMyControl)
If StringRegExp($sString ,"[[:upper:]]") Then GUICtrlSetData($hMyControl, StringLower($sString ))

For the same thing using upper case it would be

$sString = GUICtrlRead($hMyControl)
If StringRegExp($sString ,"[[:lower:]]") Then GUICtrlSetData($hMyControl, StringUpper($sString ))
;

Here is a script which will demonstrate the lower case scenario

;
$frm = GUICreate("Flicker Demo", 170, 70)
$in1 = GUICtrlCreateInput("TYPE SOME TEXT HERE", 10, 10, 150, 20)
$in2 = GUICtrlCreateInput("Now TyPe Some Text Here", 10, 40, 150, 20)
GUISetState()

While 1
   If GUIGetMsg() = -3 Then Exit
   $str1 = GUICtrlRead($in1)
   $str2 = GUICtrlRead($in2)
   If NOT StringIsLower($str1) Then GUICtrlSetData($in1, StringLower($str1))
   If StringRegExp($str2,"[[:upper:]]") Then GUICtrlSetData($in2, StringLower($str2))
Wend
;

Thanks to SmOke_N for suggesting a slight change to the RegExp

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Using a Regular expression to match a unicode character.

The only way I have found so far is using hex values (\xnn) unfortunatly that is limited to 2 hex characters and unicode requires up to 4, so after some experimenting I discovered that you can still do it if you use curly brackets

\x{nnnn}

To use the full range of Unicode characters for ChrW(256) on upwards use the range as below

[\x{0100}-\x{FFFF}]

Enjoy

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Using a Regular expression to match a unicode character.

The only way I have found so far is using hex values (\xnn) unfortunatly that is limited to 2 hex characters and unicode requires up to 4, so after some experimenting I discovered that you can still do it if you use curly brackets

\x{nnnn}

To use the full range of Unicode characters for ChrW(256) on upwards use the range as below

[\x{0100}-\x{FFFF}]
Thank you for sharing this useful trick.

Just a point: the 0x0100..0xFFFF is just a part of Unicode, albeit by far the most used part. Unicode has been specifying blocks well above 0xFFFF many years ago, in the post-UCS-2 age (Unicode version > 1.1).

The full list of allocated blocks is here. Unicode will never exceed the 0x10FFFF upper bound.

I don't believe that common AutoIt users would need to routinely handle highly rare blocks like 'Byzantine Musical Symbols' or 'Old Persian', whose code blocks are beyond 0xFFFF.

Issues could arise with the use of some "high" private areas (0xF0000..0xFFFFD and 0x100000..0x10FFFD) for ad hoc purpose by third-party applications. There are more than 137,000 codepoints for such private use.

You gave me the incentive to try > 0xFFFF codepoints. The good news is that the \x{nnnnn} trick works in the pattern matching part of AutoIt regexes even when nnnnn > FFFF. Still, built-in functions like StringLen see 2 separate characters instead of 1. Indeed the internal (UTF-16) representation uses two 16-bit words, but they code for just one single Unicode character.

This is something that concerned [advanced] users should be aware of.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Thank you for sharing this useful trick.

Just a point: the 0x0100..0xFFFF is just a part of Unicode, albeit by far the most used part. Unicode has been specifying blocks well above 0xFFFF many years ago, in the post-UCS-2 age (Unicode version > 1.1).

The full list of allocated blocks is here. Unicode will never exceed the 0x10FFFF upper bound.

I don't believe that common AutoIt users would need to routinely handle highly rare blocks like 'Byzantine Musical Symbols' or 'Old Persian', whose code blocks are beyond 0xFFFF.

Issues could arise with the use of some "high" private areas (0xF0000..0xFFFFD and 0x100000..0x10FFFD) for ad hoc purpose by third-party applications. There are more than 137,000 codepoints for such private use.

You gave me the incentive to try > 0xFFFF codepoints. The good news is that the \x{nnnnn} trick works in the pattern matching part of AutoIt regexes even when nnnnn > FFFF. Still, built-in functions like StringLen see 2 separate characters instead of 1. Indeed the internal (UTF-16) representation uses two 16-bit words, but they code for just one single Unicode character.

This is something that concerned [advanced] users should be aware of.

Thanks for that Info. I'll look into it and possibly change the regex a bit. For now I think it's covered for normal usage.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

[autoit]; Avoid hogging the CPU - use the Sleep function

; Avoid hitting the recursion limit

; If you must call a function from within itself

; then either add a check that gracefully handles

; reaching the recursion limit or your script will

; exit as illustrated in the code below.

Global $i = 0

_test_recursion()

Func _test_recursion()

SplashTextOn("", $i, 60, 40, Default, Default, 17)

;~ Sleep(9) ; <<< Avoid hogging the CPU - use the Sleep function

$i += 1

If $i > 4707 Then Sleep(2000)

_test_recursion()

EndFunc ;==>_test_recursion

; Recursion is useful and needed at times

; See _PathFull

Edited by Jon

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

here is a little thing about array's for new users

UBOUND() returns the MAX amount of slots available in an array... for Ex

Dim $array1[3]
$array1[0]=''
$array1[1]=''
$array1[2]=''
Ubound ($array) ;Will = 3 because this array is ZERO based

and alittle bit about GUI's

KODA helps alot for new users and advanced users alike, personaly i don't use it i do it from scratch, its all on preference

but obviously for experienced scripters... and not so obvious for nonscripters

NEVER have Guicreate() in an uncontroled loop because that will open endless windows haha its happened to me :)

Link to comment
Share on other sites

Just like to add Koda (for designing forms) is in the standard install for autoIT and can be found in C:\Program Files\AutoIt3\SciTE\Koda by default - can be tricky to find

Also to talk to a cmd prompt check out the Named Pipe examples that also come bundled with autoIt.

Thirdly a very fast way learn is to look at other peoples scripts - even the standard include UDFs. Most people should find better ways of organising code in some part of their own projects.

Link to comment
Share on other sites

; Avoid hogging the CPU - use the Sleep function

; Avoid hitting the recursion limit
; If you must call a function from within itself
; then either add a check that gracefully handles
; reaching the recursion limit or your script will
; exit as illustrated in the code below.

Global $i = 0
_test_recursion()

Func _test_recursion()
    SplashTextOn("", $i, 60, 40, Default, Default, 17)
;~  Sleep(9) ; <<< Avoid hogging the CPU - use the Sleep function
    $i += 1
    If $i > 4707 Then Sleep(2000)
    _test_recursion()
EndFunc   ;==>_test_recursion

; Recursion is useful and needed at times
; See _PathFull

That code will result in an infinite recursion.

Edit: Just realized that was the point...

Edited by Richard Robertson
Link to comment
Share on other sites

  • 2 months later...

How do I get the filename only from a path? Easy

$sFile = "C:\some\path\somefile.au3"
$sFile = StringRegExpReplace($sFile, "^.*\\(.*)$", "$1")
MsgBox (0,'',$sFile)

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

How do I get the filename only from a path? Easy

$sFile = "C:\some\path\somefile.au3"
$sFile = StringRegExpReplace($sFile, "^.*\\(.*)$", "$1")
MsgBox (0,'',$sFile)

Additional information regarding path manipulation using StringRegExpReplace:

;
Local $sFile = "C:\Program Files\Another Dir\AutoIt3\AutoIt3.chm"

; Drive letter -                                     Example returns     "C"
Local $sDrive = StringRegExpReplace($sFile, ":.*$", "")

; Full Path with backslash -                         Example returns     "C:\Program Files\Another Dir\AutoIt3\"
Local $sPath = StringRegExpReplace($sFile, "(^.*\\)(.*)", "\1")

; Full Path without backslash -                     Example returns     "C:\Program Files\Another Dir\AutoIt3"
Local $sPathExDr = StringRegExpReplace($sFile, "(^.:)(\\.*\\)(.*$)", "\2")

; Full Path w/0 backslashes, nor drive letter -     Example returns     "\Program Files\Another Dir\AutoIt3\"
Local $sPathExDrBSs = StringRegExpReplace($sFile, "(^.:\\)(.*)(\\.*$)", "\2")

; Path w/o backslash, not drive letter: -             Example returns     "Program Files\Another Dir\AutoIt3"
Local $sPathExBS = StringRegExpReplace($sFile, "(^.*)\\(.*)", "\1")

; File name w/0 ext -                                 Example returns     "AutoIt3"
Local $sFilenameExExt = StringRegExpReplace($sFile, "^.*\\|\..*$", "")

; Dot Extension -                                     Example returns     ".chm"
Local $sDotExt = StringRegExpReplace($sFile, "^.*\.", ".$1")

; Extension -                                         Example returns     "chm"
Local $sExt = StringRegExpReplace($sFile, "^.*\.", "")

MsgBox(0, "Path File Name Parts", _
        "Drive             " & @TAB & $sDrive & @CRLF & _
        "Path              " & @TAB & $sPath & @CRLF & _
        "Path w/o backslash" & @TAB & $sPathExBS & @CRLF & _
        "Path w/o Drv:     " & @TAB & $sPathExDr & @CRLF & _
        "Path w/o Drv or \'s" & @TAB & $sPathExDrBSs & @CRLF & __
        "File Name w/o Ext " & @TAB & $sFilenameExExt & @CRLF & _
        "Dot Extension     " & @TAB & $sDotExt & @CRLF & _
        "Extension         " & @TAB & $sExt & @CRLF)
;

Cannot remember the author name, but it can come in handy sometimes.

Link to comment
Share on other sites

  • 3 months later...

Q: How do I make sure a folder path ends in a backslash ("\")?

A: Standard method

$sPath = "C:\Some\folder"
If StringRight($sPath, 1) <> "\" Then $sPath &= "\"

Better yet is this one

$sPath = "C:\Some\Folder"
$sPath = StringRegExpReplace($sPath, "(?i)^([a-z]:.*?)(?:\\)*$", "$1\\")

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Examples and descriptions of common autoit errors.

http://code.google.com/p/m-a-t/wiki/commanerror

This was posted by me earlier in the Chat section, I've just updated it into my wiki.

Mat

problem was with case sensitivity in the link. It is now fixed and the below post was deleted to save on crap accumulation.

Edited by Mat
Link to comment
Share on other sites

Examples and descriptions of common autoit errors.

http://code.google.com/p/m-a-t/wiki/Commonerror

This was posted by me earlier in the Chat section, I've just updated it into my wiki.

Mat

That page gives me an error! :)

Page "Commonerror" Not Found
Link to comment
Share on other sites

  • 3 months later...

Problem: You attemt to write a string of unicode characters to an ini file using

$sStr = "Αυτό είναι μια δοκιμή"
IniWrite($sIni, "Test", String", $sStr)

And when you open the ini file to view it the string is simply a bunch of characters including a lot of "?"s. Ini files don't play nice with strings containing characters > Chr(255).

Solution: Write it to the ini as binary. I used this method with great success in a language translator.

$sStr = StringToBinary("Αυτό είναι μια δοκιμή")
IniWrite($sIni, "Test", String", $sStr)

Read it with

$sRtn = IniRead($sIni, "Test", "String", "")
$sRtn = BinaryToString($sRtn)
Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...