Jump to content
Chimaera

AutoIt Snippets

Recommended Posts

Posted (edited)
2 hours ago, DutchCoder said:

 

Func _MouseWaitMove()
    Local $_start = _MouseInit()
    While $_start == _MouseInit()
        Sleep(100)
    WEnd
EndFunc

Func _MouseInit()
    Local $_get = MouseGetPos()
    Return $_get[0] & $_get[1]
EndFunc

 

Doesn't detect when I move the mouse from 1,12 to 11,2.

Fixed for my use:

Func _MouseInit()
    Local $_get = MouseGetPos()
    Return $_get[0] & ";" & $_get[1]
EndFunc

 

Edited by Inpho

Share this post


Link to post
Share on other sites

I have honestly taken more than I have provided here, so here is my attempt to provide for now.
This is an automatic coin toss script that writes the amount of times that it has been ran to file and displays it in the title bar. To make it better for repeated uses, it repeats itself every time you click 'OK' in the msgbox.  This code, like any other code that I have ever provided to this forum is free for any type of use without restriction.

 

Local $coin[3]
If FileExists( "runnumber.txt" ) then
Local $sTimesRan = FileReadLine( "runnumber.txt", 1 )
Else
Local $sTimesRan = 0
EndIf
$coin[1] = "Heads"
$Coin[2] = "Tails"
While 1
   $sTimesRan = $sTimesRan + 1
   FileDelete( "runnumber.txt" )
   FileWrite ( "runnumber.txt", $sTimesRan )
MsgBox(0, "Coin Flip" & " - " & $sTimesRan , $Coin[Random(1, 2, 1)])
WEnd

 


Spoiler

 

"If a vegetarian eats vegetables,What the heck does a humanitarian eat?"

"I hear voices in my head, but I ignore them and continue on killing."

"You have forced me to raise the indifference warning to beige, it's a beige alert people. As with all beige alerts please prepare to think about the possibility of caring."

An optimist says that giving someone power DOESN'T immediately turn them into a sadist. A pessimist says that giving someone power doesn't IMMEDIATELY turn them into a sadist.

 

 

Share this post


Link to post
Share on other sites

@argumentum As long as I can cook it afterwords.


Spoiler

 

"If a vegetarian eats vegetables,What the heck does a humanitarian eat?"

"I hear voices in my head, but I ignore them and continue on killing."

"You have forced me to raise the indifference warning to beige, it's a beige alert people. As with all beige alerts please prepare to think about the possibility of caring."

An optimist says that giving someone power DOESN'T immediately turn them into a sadist. A pessimist says that giving someone power doesn't IMMEDIATELY turn them into a sadist.

 

 

Share this post


Link to post
Share on other sites
Posted (edited)

This code is to detect all open Window Explorer windows. It returns a 1 or 2 dimension array, depending on the parameters you send. It returns the short name (basically, the folder name) and/or the full path to the folder. If no windows are open, it returns -1. To avoid errors, you should use IsArray to see if anything has been returned or check if the return value is -1. This function only returns data if there is a full address available (therefore, My Computer, Control Panel, My Documents, etc. will not be included). I can work on including these if anyone is interested.

The code has some added code to delete repeat entries in the array because when I ran the original script with the script directory window open, it returned about 60 instances of that window. I don't know why that happened. When the script directory was not open, it returned the correct number of windows. The extra code is the only return unique instances in the array, with no duplicates, whether the script directory is open or not. If anyone wants to weigh in on why that happened, I'd love to hear about it.

Enjoy!

#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w- 7

#include <array.au3>
Global $aList, $sWinList

; Example 1
$aList = _GetListOfOpenExplorerWindows(1, 3)
If IsArray($aList) Then
    For $i = 1 To $aList[0][0]
        $sWinList = $sWinList & $aList[$i][0] & " | " & $aList[$i][1] & @CRLF
    Next
    ConsoleWrite($sWinList)
    _ArrayDisplay($aList)
ElseIf $aList = -1 Then
    MsgBox(0, "", "No windows open.")
EndIf
; End Example 1

; Example 2
$aList = _GetListOfOpenExplorerWindows(1, 1)
If IsArray($aList) Then
    _ArrayDisplay($aList)
ElseIf $aList = -1 Then
    MsgBox(0, "", "No windows open.")
EndIf

$aList = _GetListOfOpenExplorerWindows(1, 2)
If IsArray($aList) Then
    _ArrayDisplay($aList)
ElseIf $aList = -1 Then
    MsgBox(0, "", "No windows open.")
EndIf
; End Example 2


; #FUNCTION# ==========================================================================================================================================
; Description ...: Retrieves a list of all open Windows Explorer windows
; Syntax ........: _GetListOfOpenExplorerWindows()
; Parameters ....: $vIncludeArrayCount: [optional] Include array count at array[0] or array[0][0] | [0 = No, 1 = Yes (Default)]
;                  $vColumnReturn:      [optional] Array Column Return | [1 = Short Names Only, 2 = Full Addresses, 3 or 1 + 2 = Return Both (Default)]
;                                       1 and 2 are 1D arrays; 3 is a 2D array.
; Return values .: Success:             An array (1D or 2D, depending on sent parameters). No open windows returns -1. Only works for windows that have
;                                       a full address (ex: does not work with My Computer, Control Panel, My Documents, etc.).
;                  Failure:             Returns -1 (No windows with full directory addresses are open).
; Author ........: abberration
; Example .......: Yes
; =====================================================================================================================================================

Func _GetListOfOpenExplorerWindows($vIncludeArrayCount = 1, $vColumnReturn = 3)
    Local $aWinList, $sGetText, $aWinText, $vStrLen, $sFormat, $i, $j, $k, $vRowsToDel
    $aWinList = WinList()
    Local $aOutput[0][2]
    $k = 0
    For $i = 1 To $aWinList[0][0]
        $sGetText = WinGetText($aWinList[$i][0])
        $aWinText = StringSplit($sGetText, @LF)
        For $j = 1 To $aWinText[0]
            If StringInStr($aWinText[$j], "Address: ") Then
                $sFormat = StringReplace($aWinText[$j], "Address: ", "")
                If DirGetSize($sFormat) > -1 Then
                    ReDim $aOutput[UBound($aOutput) + 1][2]
                    $aOutput[$k][0] = $aWinList[$i][0]
                    $aOutput[$k][1] = $sFormat
                    $k += 1
                EndIf
            EndIf
        Next
    Next
    For $i = 1 To UBound($aOutput)
        $vStrLen = StringLen($aOutput[$i - 1][0])
        If $vStrLen = 0 Then
            $vRowsToDel = $vRowsToDel & $i - 1 & ";"
        EndIf
    Next
    $vRowsToDel = StringTrimRight($vRowsToDel, 1)
    _ArrayDelete($aOutput, $vRowsToDel)
    If $vIncludeArrayCount = 1 Then
        _ArrayInsert($aOutput, 0, UBound($aOutput))
    EndIf
    If $vColumnReturn = 1 Then
        _ArrayColDelete($aOutput, 1)
    ElseIf $vColumnReturn = 2 Then
        If $vIncludeArrayCount = 1 Then
            If UBound($aOutput) > 0 Then
                $aOutput[0][1] = UBound($aOutput) - 1
            EndIf
        EndIf
        _ArrayColDelete($aOutput, 0)
    EndIf
    If UBound($aOutput) = 0 Then
        Return -1
    Else
        Return $aOutput
    EndIf
EndFunc

 

Edited by abberration
Fixed a bug. Added return code -1. Added error checking in example. I think it's good now.

RAID Calculator | Software Installer

The truth has been suppressed since the dawn of time.

Share this post


Link to post
Share on other sites

I use StringRegExp a lot, but it doesn't return a 2D array of matches when I capture lines of data (like a from csv). I (finally) created _Array_Resize which converts 1D arrays into 2D arrays based on the number of columns

; #FUNCTION# ====================================================================================================================
; Name ..........: _Array_Resize
; Description ...: Turns a 1D array into a 2D array based on $iColumns
; Syntax ........: _Array_Resize(Byref $aArray1d, $iColumns[, $iStart = 0])
; Parameters ....: $aArray1d            - [in/out] an Array.
;                  $iColumns            - the number of columns to create.
;                  $iStart              - [optional] the number of elements to skip. Default is 0.
; Return values .: Success - a 2D array
;                  Failure - False and sets @error
;                  |1 - the 1D array is not divisible by $iColumns evenly, resulting in an un-even array
; Author ........: Seadoggie01
; Modified ......: September 3, 2020
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _Array_Resize2(ByRef $aArray1d, $iColumns, $iStart = Default)

    If IsKeyword($iStart) Then $iStart = 0

    Local $iElements = UBound($aArray1d) - $iStart

    ; If the conversion would leave blank cells
    If Mod($iElements, $iColumns) <> 0 Then Return SetError(1, 0, False)

    Local $aArray2d[$iElements/$iColumns][$iColumns]

    Local $iRow = 0, $iCol = 0
    For $i=$iStart To UBound($aArray1d) - 1
        $aArray2d[$iRow][$iCol] = $aArray1d[$i]
        $iCol += 1
        ; If this is the end of a column
        If $iCol = $iColumns Then
            ; Increase the row counter and reset $iCol
            $iRow += 1
            $iCol = 0
        EndIf
    Next

    Return $aArray2d

EndFunc
Edited by seadoggie01
Unintended feature

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts

Share this post


Link to post
Share on other sites

There is a bug in your for loop, you should take account of $iStart in your calculation of row/column.  If you run this, it will crash :

#include <Array.au3>

Local $a = [0,0,0,1,2,3,4]

$a = _Array_Resize($a, 2, 3)
_ArrayDisplay($a)

Func _Array_Resize(ByRef $aArray1d, $iColumns, $iStart = Default)

    If IsKeyword($iStart) Then $iStart = 0

    Local $iElements = UBound($aArray1d) - $iStart

    ; If the conversion would leave blank cells
    If Mod($iElements, $iColumns) <> 0 Then Return SetError(1, 0, False)

    Local $aArray2d[$iElements/$iColumns][$iColumns]

    Local $iRow = 0
    For $i=$iStart To UBound($aArray1d) - 1
        $aArray2d[$iRow][Mod($i, $iColumns)] = $aArray1d[$i]
        ; If this is the end of a column, increase the row counter
        If Mod($i, $iColumns) = $iColumns - 1 Then $iRow += 1
    Next

    Return $aArray2d

EndFunc

 

Edited by Nine

Share this post


Link to post
Share on other sites
11 hours ago, Nine said:

If you run this, it will crash :

Well, aren't we picky about our scripts 😜 FIFY (Thanks! I don't use the $iStart parameter and didn't test it well enough... obviously)


All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts

Share this post


Link to post
Share on other sites

Yep, picky and streamliner ;)

Local $aArray2d[$iElements/$iColumns][$iColumns]

    For $i = 0 to $iElements-1
      $aArray2d[Floor($i/$iColumns)][mod($i,$iColumns)] = $aArray1d[$i+$iStart]
    Next

    Return $aArray2d

 

Share this post


Link to post
Share on other sites

_ScriptIsCompiled(): Returns 1 if script is a compiled executable EXE File!  returns 0 if an .au3 file; or an .a3x file

Func _ScriptIsCompiled()
    Local $hOpen = FileOpen(@ScriptFullPath, 16)
    Local $iContent = FileRead($hOpen,10)
    FileClose($hOpen)
    If ((StringLeft($iContent, 6) == "0x4D5A") Or (StringLeft($iContent, 6) == "0x5A4D") Or (StringLeft($iContent, 8) == "0x4D5A90") Or (StringLeft($iContent, 10) == "0x7F454C46")) Then
        Return 1 ; True
    Else
        Return 0 ; False
    EndIf
EndFunc   ;==>_ScriptIsCompiled by TRONG.LIVE
   
Edited by VIP
@Compiled Returns 1 if script is a compiled executable or an .a3x file; returns 0 if an .au3 file.

Regards,
 

Share this post


Link to post
Share on other sites

Simpler?

MsgBox(0, "", _ScriptIsCompiled() & @LF)

Func _ScriptIsCompiled()
    Return Not (StringRight(@ScriptName, 4) = ".au3")
EndFunc

 


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)

Share this post


Link to post
Share on other sites

Oh I always forget this pragma exists!

@VIP well, so why simpler codes don't work?


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)

Share this post


Link to post
Share on other sites

a3x cannot run directly, it's like au3 need autoit.exe to run it!
You will need to change the script accordingly.


Regards,
 

Share this post


Link to post
Share on other sites

Sorry but run the following code:
a) from SciTE
b) compiled as .exe
c) compiled as .a3x

You get "Compiled" in cases b) & c) as expected.

MsgBox(0, "", (@Compiled ? "C" : "Not c") & "ompiled")
MsgBox(0, "", (_ScriptIsCompiled() ? "C" : "Not c") & "ompiled")

Func _ScriptIsCompiled()
    Return Not (StringRight(@ScriptName, 4) = ".au3")
EndFunc

 


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)

Share this post


Link to post
Share on other sites

AutoIT.exe can run scripts with any arbitrary extension. Testing the extension is a bad idea.
Maybe you don't understand what I mean.
When you run the script with "AutoIT.exe AnyName.ext"
Will be different from script compiled to exe.
Due to the problem of mistakenly identifying the virus I did not use the script compiled to the exe, instead I used a3x.
Identification of a3x as compiled file changes the script structure and associated paths. So it needs adjustments.

I had a suggestion for another version of AutoIT, which unfortunately is not supported.
And I got tired of reporting misidentification with Antivirus providers.

 

 

Edited by VIP

Regards,
 

Share this post


Link to post
Share on other sites

Of course if you rename .a3x to something else, the macro fails. But why would one do that?

One may as well wrap a genuine AutoIt .exe inside something else with some other extension linked to a kludge in the OS, spoiling detection of fixed byte pattern at fixed place.
One may also run .any_extension_source from Scite provided some patches here and there.


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)

Share this post


Link to post
Share on other sites

Let's just think that _ScriptIsCompiled() is different from @Compiled !


Regards,
 

Share this post


Link to post
Share on other sites
Func _ScriptRestart()
    If _ScriptIsCompiled() Then
        Run('"' & @ScriptFullPath & '" ' & $CmdLineRaw, @ScriptDir, Default, 1)
    Else
        Run('"' & @AutoItExe & '" "' & @ScriptFullPath & '" ' & $CmdLineRaw, @ScriptDir, Default, 1)
    EndIf
    Exit
EndFunc   ;==>_ScriptRestart    TRONG.LIVE

Func _ScriptIsCompiled()
    Local $hOpen = FileOpen(@ScriptFullPath, 16)
    Local $iContent = FileRead($hOpen, 10)
    FileClose($hOpen)
    If ((StringLeft($iContent, 6) == "0x4D5A") Or (StringLeft($iContent, 6) == "0x5A4D") Or (StringLeft($iContent, 8) == "0x4D5A90") Or (StringLeft($iContent, 10) == "0x7F454C46")) Then
        Return 1
    Else
        Return 0
    EndIf
EndFunc   ;==>_ScriptIsCompiled    TRONG.LIVE

 

Edited by VIP

Regards,
 

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...