Jump to content
Chimaera

AutoIt Snippets

Recommended Posts

3 hours ago, jchd said:

Also be aware that the Map datatype is deprecated.

What!? :blink:

When? Why? Was there any official annoucement?


A cross-platform implementation of the AutoIt language

My contributions to the AutoIt Community ##AutoIt at freenode, real-time chat

3fHNZJ.gif

Spoiler

If I have hurt or offended you in anyway, Please accept my apologies, I never (regardless of the situation) intend to do that to anybody.

Share this post


Link to post
Share on other sites
8 minutes ago, TheDcoder said:

What!? :blink:

When? Why? Was there any official annoucement?

 

Quote from a statement made by Jon in a private forum:

Quote

Well IIRC Map is fundamentally broken due to the way variables work in AutoIt.

So he won't be including it as it is, but it did stay in Beta for the moment. Don't hold your breath though to get this fixed and included. ;)

Jos 

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
3 hours ago, BrewManNH said:

Use a scripting.dictionary instead for future proofing

I agree

@Inpho You may wrap scripting.dictionary like BrewManNH has suggested, you can use these functions as inspiration:

  1. _Map_DictObjectToMap
  2. _Map_MapToDictObject

A cross-platform implementation of the AutoIt language

My contributions to the AutoIt Community ##AutoIt at freenode, real-time chat

3fHNZJ.gif

Spoiler

If I have hurt or offended you in anyway, Please accept my apologies, I never (regardless of the situation) intend to do that to anybody.

Share this post


Link to post
Share on other sites

@Jos Such a shame... oh well, AutoIt is already good enough with the current data types :)

Definitely adding this to the ToDo list of my implementation of AutoIt for sure though.


A cross-platform implementation of the AutoIt language

My contributions to the AutoIt Community ##AutoIt at freenode, real-time chat

3fHNZJ.gif

Spoiler

If I have hurt or offended you in anyway, Please accept my apologies, I never (regardless of the situation) intend to do that to anybody.

Share this post


Link to post
Share on other sites

Everything that can be done in Maps can be done in Scripting.Dictionary with the right wrappers.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
1 minute ago, BrewManNH said:

Everything that can be done in Maps can be done in Scripting.Dictionary with the right wrappers.

Agreed... still, having native support in the language would be a great feature to have :)


A cross-platform implementation of the AutoIt language

My contributions to the AutoIt Community ##AutoIt at freenode, real-time chat

3fHNZJ.gif

Spoiler

If I have hurt or offended you in anyway, Please accept my apologies, I never (regardless of the situation) intend to do that to anybody.

Share this post


Link to post
Share on other sites

For kicks :

#include <WinAPISysWin.au3>

_UntrapGhostSysTrayIcons()

Func _UntrapGhostSysTrayIcons()
    Local $aMPos = MouseGetPos(), $x = 1
    Local $h = ControlGetHandle(_WinAPI_FindWindow('Shell_TrayWnd', ''), "", "[Class:SysPager]")
    Local $apos = WinGetPos($h)
    MouseMove($apos[0] + $apos[2], $apos[1] + 10, 0)
    Local $iRange = $apos[2] / 10
    Do
        MouseMove($apos[0] + $apos[2] - $iRange, $apos[1] + 10, 11 - $x)
        $apos = WinGetPos($h)
        $x += 1
        $iRange = ($apos[2] / 10) * $x
    Until $x > 10
    MouseMove($apos[0] + $apos[2], $apos[1] + 10)
    MouseMove($aMPos[0], $aMPos[1], 0)
EndFunc

 

Edited by Deye

Share this post


Link to post
Share on other sites
#include <Date.au3>
Func _StringFormatMs($ms, $bRetMs = False) ; based on https://www.autoitscript.com/forum/topic/163621-convert-ms-to-dayhourminsec/?do=findComment&comment=1192334
    Local $sRetMs = "", $day = "", $hour, $min, $sec
    _TicksToTime($ms, $hour, $min, $sec)
    If $hour >= 24 Then
        $day = Int($hour / 24)
        $hour = Mod($hour, 24)
    EndIf
    If $bRetMs Then $sRetMs = "." & StringRight($ms, 3)
    Switch StringLen($day)
        Case 0
            Return StringFormat("%02i:%02i:%02i", $hour, $min, $sec) & $sRetMs
        Case 1, 2, 3
            Return StringFormat("%03i %02i:%02i:%02i", $day, $hour, $min, $sec) & $sRetMs
        Case Else
            Return $day & " " & StringFormat("%02i:%02i:%02i", $hour, $min, $sec) & $sRetMs
    EndSwitch
EndFunc   ;==>_StringFormatMs

Millisecond to nice readable format. 

Edited by argumentum
better code

Share this post


Link to post
Share on other sites
ConsoleWrite(_GetTimeSinceLogon() & " seconds since Logon." & @CRLF)
Func _GetTimeSinceLogon($sTimeDiffType = "s", $sUserName = @UserName, $sComputerName = @ComputerName) ; no AD, just local user
    Local $aRet = DllCall("netapi32.dll", "long", "NetUserGetInfo", "wstr", $sComputerName, "wstr", $sUserName, "dword", 11, "ptr*", 0)
    If @error Or $aRet[0] Then Return SetError(1, 0, False)
    Local $sSeconds = DllStructGetData(DllStructCreate("ptr;ptr;ptr;ptr;dword;dword;dword;ptr;ptr;dword;dword;dword;dword;ptr;dword;ptr;dword;dword;byte;dword", $aRet[4]), 10)
    DllCall("netapi32.dll", "long", "NetApiBufferFree", "ptr", $aRet[4])
    Local $slastLogon = _DateAdd('s', Number($sSeconds), "1970/01/01 00:00:00")
    Local $aLastLogon = StringSplit($slastLogon, "/ :") ; take the UTC to local time
    Local $tSystem = _Date_Time_EncodeSystemTime($aLastLogon[2], $aLastLogon[3], $aLastLogon[1], $aLastLogon[4], $aLastLogon[5], $aLastLogon[6])
    Local $tLocal = _Date_Time_SystemTimeToTzSpecificLocalTime($tSystem)
    $slastLogon = _Date_Time_SystemTimeToDateTimeStr($tLocal, 1)
    Local $sTimeDiff = _DateDiff($sTimeDiffType, $slastLogon, _NowCalc())
    Return $sTimeDiff
EndFunc   ;==>_GetTimeSinceLogon

(add #include as needed)

Share this post


Link to post
Share on other sites
Posted (edited)

About a 9 times faster than using the  ArrayUnique method for getting a Unique delimited: single digit\Chr string (2- 3 notations between delimits will go fast , 4 notations and above will go slower and slower  ..)

Edit:  reaching the limit of 100 Unique elements with the above mentioned limit  and ArrayUnique will perform just the same

Not recommended for using  with _ArrayToString  for a ready made array as _ArrayToString will go slow for large arrays

note: The ArrayUnique method will fail for any 8 and above digit (long) loop string process :
Not sure what is the limit when using  StringUnique_Delim but will probably do 8 and above  ..

fixed: on going from here (where it was leaving an extra duplicate in some cases ..)

#include <File.au3>
#include <Timers.au3>

$s = ""
For $i = 1 To 100000
    $s &= "||" & Random(0, 10, 1) ; 8- 9 times faster
;~  $s &= "||" & Round(Random(0, 10), 1) ;a bit faster
;~  $s &= "||" & Round(Random(0, 10), 2) ;significantly slower
Next
$hTimer = _Timer_Init()
$sOutput = StringUnique_Delim($s, "|")
ConsoleWrite("1D : " & _Timer_Diff($hTimer) & @CRLF)
_ArraySort($sOutput, 0, 1)
_ArrayDisplay($sOutput, "StringUnique")

$hTimer = _Timer_Init()
$a = _ArrayUnique(StringSplit($s, "|", 3))
ConsoleWrite("1D : " & _Timer_Diff($hTimer) & @CRLF)
_ArraySort($a, 0, 1)
_ArrayDisplay($a, "_ArrayUnique")

Func StringUnique_Delim($sString = "", $delim = ",", $bUBound = True)
    Local $sDelim = "\" & $delim
    Return StringSplit(StringTrimLeft(StringRegExpReplace($delim & $sString & $delim, $sDelim & '(?=' & $sDelim & ')|(' & $sDelim & '[^' & $sDelim & ']+)(?=' & $sDelim & ')(?=.*?\1' & $sDelim & ')|(' & $sDelim & '$)', ""), 1), $delim, $bUBound ? "" : 3)
EndFunc   ;==>StringUnique_Delim

 

Edited by Deye

Share this post


Link to post
Share on other sites

Hi, 

this closes the main Gui window with a slow double click, instead of immediate closing:

#include <GUIConstantsEx.au3>
;Opt("GUICloseOnESC", 0) ; disable closing the gui with escape

Global $closingdelay = 1000, $closecounter = 0, $TimeHandle = TimerInit(), $TimeDiff = TimerDiff($TimeHandle)
Global $formTitle = "My Testing App"

$Form1 = GUICreate($formTitle, 616, 440, 208, 165)

GUISetState(@SW_SHOW)

While 1
    CloseGuiOnDoubleClick("loop")

    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            CloseGuiOnDoubleClick("button")
    EndSwitch
WEnd

Func CloseGuiOnDoubleClick($typ)
;; Global variables:
;Global $closingdelay = 1000, $closecounter = 0, $TimeHandle = TimerInit(), $TimeDiff = TimerDiff($TimeHandle)
;Global $formTitle = "My Testing App"

;;
;CloseGuiOnDoubleClick("loop")          ;Insert in the main loop
;CloseGuiOnDoubleClick("button")        ;insert instead of exit in the $GUI_EVENT_CLOSE check, in the main loop

    $typ = StringLower($typ)
    Select
        Case $typ = "button"
            If $closecounter = 1 And TimerDiff($TimeHandle) <= $closingdelay Then
                GUIDelete()
                Exit
            EndIf
            If $closecounter = 0 Then
                $closecounter = 1
                $TimeHandle = TimerInit()
                WinSetTitle($Form1, "", "Slow doubleclick to close")
            EndIf
        Case $typ = "loop"
            If $closecounter = 1 And TimerDiff($TimeHandle) > $closingdelay Then
                $closecounter = 0
                WinSetTitle($Form1, "", $formTitle)
            EndIf
    EndSelect
EndFunc   ;==>CloseGuiOnDoubleClick

 


~Dan~ :Emu.Zone now with Desktop Pets / DeskMates - (Bugfixed UniScrMate 4.1)

Share this post


Link to post
Share on other sites
Posted (edited)

Perhaps, for replacing doubled empty lines + more  >   to one line spacing

$s = ""
For $i = 1 To 5
    $s &= Random(100, 1000, 1)
    $s &= @LF & Random(100, 1000, 1)
    For $j = 1 To 10
        $s &= @CRLF
    Next
Next

MsgBox(0, "Original", $s)
MsgBox(0, "Result", StringRegExpReplace($s, '(\n[^\n]+)(?=\n)(?=.*?\1)', ""))

Edit: or as jchd suggests (Thanks)

StringRegExpReplace($s, "\n\R{2,}", @CRLF)

 

Edited by Deye

Share this post


Link to post
Share on other sites

In AutoIt regexp implementation \R means @CR or @LF or @CRLF.  Thus we can just do:

StringRegExpReplace($s, "\R{2,}", @CRLF)

 


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
Posted (edited)

jchd ,

Using the \R switch instead of \n leaves an extra line feed .. ( if i had to replace all \n with my example )

also note that the result posted with mine needs to leave 1 empty line untouched as done with the example

so we need to keep that empty 1 line 

perhaps

StringRegExpReplace($s, "\n\R{2,}", @CRLF)

 

Edited by Deye

Share this post


Link to post
Share on other sites
Posted (edited)

Hi, here is a small function, which i wrote to avoid the StringSplit and its array usage.

It searches (directional search - from Left or Right) a String for the delimiter, and returns the remainder of the String with direction (left side/right side) including or excluding the delimiter char.
The delimiter is limited to 1 char.

 

;Example is at the bottom !

Func _StringSearchSplit($str, $delimiter, $dir = "L", $ret = "R", $incdel = -1)
    ; #FUNCTION# ====================================================================================================================
    ; Name ..........:  _StringSearchSplit
    ; Description ...:  Search for the first delimiter in a string, with searching direction, Case Sensitive search
    ; Syntax ........:  _StringSearchSplit( $String, $delimiter [, $dir ] [, $ret ] [, $incdel ])
    ; Parameters ....:  $String             - String to be checked.
    ;                   $delimiter          - 1 char delimiter, has to be defined
    ;                   $dir                - Search from direction (Left/Right), use "L" or "R" - Left is default
    ;                                           The first letter will be used for direction, if multiple letters are entered e.g "Lab" = "L"
    ;                   $ret                - Return side, Left or Right - Right is default. see above for valid entries.
    ;                   $incdel             - Include delimiter 0 = No, 1 = Yes
    ;
    ; Return values .:  Success             - String
    ;
    ;                       e.g. 1: _StringSearch("c:\bat\test.bb","\","L","L")   returns "c:"
    ;                       e.g. 2: _StringSearch("c:\bat\test.bb","\","L","L",1) returns "c:\"
    ;                       e.g. 3: _StringSearch("c:\bat\test.bb","\","L","R")   returns "bat\test.bb"
    ;                       e.g. 4: _StringSearch("c:\bat\test.bb","\","L","R",1) returns "\bat\test.bb"
    ;                       e.g. 5: _StringSearch("c:\bat\test.bb","\","R","R")   returns "test.bb"
    ;                       e.g. 6: _StringSearch("c:\bat\test.bb","\","R","R",1) returns "\test.bb"
    ;
    ;                   Failure             - Empty string and @error flag as follows:
    ;                   @error :            1 - Empty String
    ;                                       2 - Delimiter should have a length of 1 char
    ;                                       3 - Should not happen, but if it does, search the PANIC button !
    ; Author ........:  Dan_555 (Autoitscript.com forum)
    ; ===============================================================================================================================

    Local $y
    Local $tmptxt = ""

    $dir = StringLeft(StringUpper($dir),1)
    $ret = StringLeft(StringUpper($ret),1)

    SetError(0)

    If StringLen($str) = 0 Then
        SetError(1)             ;empty string
        Return ""
    EndIf

    If (StringInStr($str, $delimiter) = 0) Or (StringLen($delimiter) <> 1) Then
        SetError(2)                ;invalid delimiter
        Return ""
    EndIf

    If $dir <> "L" And $dir <> "R" Then $dir = "L"                  ;Set default values
    If $ret <> "L" And $ret <> "R" Then $ret = "R"

    If $dir = "L" Then $y = StringInStr($str, $delimiter, 1)        ;Search for the delimiter
    If $dir = "R" Then $y = StringInStr($str, $delimiter, 1, -1)

    If $incdel = 0 Then $incdel = -1                                ;Tricky calculations ;)
    If $incdel <> -1 Then $incdel = 0

    If $ret = "L" Then Return StringMid($str, 1, $y + $incdel)      ;DisAssemble the string
    If $ret = "R" Then Return StringMid($str, $y - $incdel)

    SetError (3)
    Return ""
EndFunc   ;==>_StringSearchSplit

;~ ;Example

;~ $tmptxt = "c:\bAt\man\mobile.cAll"
;~ $s1 = "a"
;~ $s2 = "\"
;~ $s3="-----------------------------------"
;~ CW("Search string: " & $tmptxt)
;~ CW($s3 & "<")
;~ CW("LL" & $s1 & "0 " & _StringSearchSplit($tmptxt, $s1, "L", "L"))
;~ CW("LL" & $s1 & "1 " & _StringSearchSplit($tmptxt, $s1, "L", "L", 1))
;~ CW($s3 & "<")
;~ CW("LR" & $s1 & "0 " & _StringSearchSplit($tmptxt, $s1, "L", "R"))
;~ CW("LR" & $s1 & "1 " & _StringSearchSplit($tmptxt, $s1, "L", "R", 1))
;~ CW($s3 & "<")
;~ CW("RR" & $s1 & "0 " & _StringSearchSplit($tmptxt, $s1, "R", "R"))
;~ CW("RR" & $s1 & "1 " & _StringSearchSplit($tmptxt, $s1, "R", "R", 1))
;~ CW($s3 & "<")
;~ CW("RL" & $s1 & "0 " & _StringSearchSplit($tmptxt, $s1, "R", "L"))
;~ CW("RL" & $s1 & "1 " & _StringSearchSplit($tmptxt, $s1, "R", "L", 1))
;~ CW($s3 & "<" & " Second Example :")
;~ CW("LL" & $s2 & "0 " & _StringSearchSplit($tmptxt, $s2, "L", "L"))
;~ CW("LL" & $s2 & "1 " & _StringSearchSplit($tmptxt, $s2, "L", "L", 1))
;~ CW($s3 & "<")
;~ CW("LR" & $s2 & "0 " & _StringSearchSplit($tmptxt, $s2, "L", "R"))
;~ CW("LR" & $s2 & "1 " & _StringSearchSplit($tmptxt, $s2, "L", "R", 1))
;~ CW($s3 & "<")
;~ CW("RR" & $s2 & "0 " & _StringSearchSplit($tmptxt, $s2, "R", "R"))
;~ CW("RR" & $s2 & "1 " & _StringSearchSplit($tmptxt, $s2, "R", "R", 1))
;~ CW($s3 & "<")
;~ CW("RL" & $s2 & "0 " & _StringSearchSplit($tmptxt, $s2, "R", "L"))
;~ CW("RL" & $s2 & "1 " & _StringSearchSplit($tmptxt, $s2, "R", "L", 1))

;~ Func CW($txt)
;~  ConsoleWrite(" > " & $txt &  @CRLF)
;~ EndFunc   ;==>CW
;~ ;- End of the example

 

Edited by Dan_555

~Dan~ :Emu.Zone now with Desktop Pets / DeskMates - (Bugfixed UniScrMate 4.1)

Share this post


Link to post
Share on other sites

@Dan_555 Nice function, reminds me of a similar function (strseg) that I wrote, but in C :)

It basically does the same thing, search for the next delimiter and return the string. It doesn't have support for the different directions though!


A cross-platform implementation of the AutoIt language

My contributions to the AutoIt Community ##AutoIt at freenode, real-time chat

3fHNZJ.gif

Spoiler

If I have hurt or offended you in anyway, Please accept my apologies, I never (regardless of the situation) intend to do that to anybody.

Share this post


Link to post
Share on other sites

Wait for Mouse to move (two ways)

Normally when you have a GUI you can use this:

Func _MouseWaitMove()
    Do
        Sleep(100)
    Until GuiGetMsg() = -11
EndFunc

And sometimes you don't have a GUI. This one always works:

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

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

 

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...