Jump to content
Chimaera

AutoIt Snippets

Recommended Posts

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

; Returns the string on the left side of a search string consisting of digits only

ConsoleWrite(_StringGetDigitLeft("1234abcd5678") & @CRLF) ; Results in "1234"

Func _StringGetDigitLeft(Const ByRef $sString)
    If Not IsString($sString) Then
        Return SetError(1, 0, "")
    EndIf

    Local $iLength = StringLen($sString)
    Local $sCheckString = ""
    Local $sLeft = ""

    For $i = 1 To $iLength
        $sCheckString = StringLeft($sString, $i)
        If StringIsDigit($sCheckString) Then
            $sLeft = $sCheckString
        Else
            ExitLoop
        EndIf
    Next

    Return $sLeft
EndFunc   ;==>_StringGetDigitLeft
#AutoIt3Wrapper_Au3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w- 7

; Returns the string on the right side of a search string consisting of digits only

ConsoleWrite(_StringGetDigitRight("1234abcd5678") & @CRLF) ; Results in "5678"

Func _StringGetDigitRight(Const ByRef $sString)
    If Not IsString($sString) Then
        Return SetError(1, 0, "")
    EndIf

    Local $iLength = StringLen($sString)
    Local $sCheckString = ""
    Local $sRight = ""

    For $i = $iLength To 1 Step -1
        $sCheckString = StringRight($sString, $iLength - $i + 1)
        If StringIsDigit($sCheckString) Then
            $sRight = $sCheckString
        Else
            ExitLoop
        EndIf
    Next

    Return $sRight
EndFunc   ;==>_StringGetDigitRight

 

Share this post


Link to post
Share on other sites

Simpler:

; Returns the string on the left side of a search string consisting of digits only
ConsoleWrite(_StringGetDigitLeft("1234abcd5678") & @CRLF) ; Results in "1234"
ConsoleWrite(_StringGetDigitLeft("abcd5678") & @CRLF) ; Results in ""

; Returns the string on the right side of a search string consisting of digits only
ConsoleWrite(_StringGetDigitRight("1234abcd5678") & @CRLF) ; Results in "5678"
ConsoleWrite(_StringGetDigitRight("1234abcd") & @CRLF) ; Results in ""

Func _StringGetDigitLeft(Const ByRef $sString)
;   Return String(Number($sString)) ; requires at least one leading digit
    Return StringRegExpReplace($sString, "^(?|(\d+)|())(.*$)", "$1")
EndFunc

Func _StringGetDigitRight(Const ByRef $sString)
    Return StringRegExpReplace($sString, "(.*?)(?|(\d+)|())$", "$2")
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

There's a certain grace that comes with admitting you were wrong (or that someone did something better). Trust me, I've been there... too much. It happens a lot, don't worry about it.


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

Hi guys, I've spent a couple hours today to understand socks5 proxy and tried to implement connecting to an external proxy through autoit. My goal is to add this function to my IRC client. It's all about learning isn't it ?!?

I've got the authentication part working, and I am sending the snippet here thinking it might interest somebody :)

Have fun!

Next step, I will be testing some requests to IRC servers, I am also interested into adding SSL support !!!

-el tonio

 

 

 

; Socks5 proxy server authentication using the username/password method (0x02)

; Pure AutoIt 3, Authentication to Socks5 proxy server using the username/password method

Global Const $AuthNumberOfAuthMethodsSupported = 2
Global Const $AuthMethodNoAuthenticationRequired = 0x00
Global Const $AuthMethodUsernamePassword = 0x02

Local $ProxyHost = "your Socks5 server hostname";
Local $ProxyPort = "your Socks5 server port";
Local $Username = "User name"
Local $Password = "Password"

TCPStartup()
Opt("TCPTimeout", 5000)

; 1. Connect to Proxy
Local $socket

$socket = TCPConnect(TCPNameToIP($ProxyHost),$ProxyPort)
If $socket > 0 Then

    ConsoleWrite($socket & " Connected..." & @CRLF)

    ; 2. GREETING the server (Or telling it what Auth Method you support)
    Local $sReq = Chr("0x05") _                                 ; Socks version 5
                & Chr("2") _                                    ; 2 auth methods supported
                & Chr($AuthMethodNoAuthenticationRequired) _    ; No Auth Required method
                & Chr($AuthMethodUsernamePassword)              ; Username/Password method

    ConsoleWrite("$sReq : " & $sReq & @CRLF)

    TCPSend($socket, $sReq)

    Local $sBuff
    ; Waiting for the server to reply
    While 1
        $sBuff = TCPRecv($socket,6,1)
        If @error Then ExitLoop
        If StringLen($sBuff) > 0 Then ExitLoop

        Sleep(100)
    WEnd
    
    ConsoleWrite(@CRLF & "!   Reply: " & $sBuff & @CRLF)
    
    If StringRight($sBuff, 2) = "02" Then ; Server chose the username/password method which is GREAT!
    
        ; 3. Perform Authentification
        $sReq = "0x01" _
                & StringMid(Binary(StringLen($Username)), 3, 2) _
                & StringMid(StringToBinary($Username), 3) _
                & StringMid(Binary(StringLen($Password)), 3, 2) _
                & StringMid(StringToBinary($Password), 3)

        ConsoleWrite("$sReq : " & $sReq & @CRLF)

        TCPSend($socket, $sReq)

        ; Wait for the Reply
        ConsoleWrite("Waiting for the reply ... " & @CRLF)
        While 1

            $sBuff = TCPRecv($socket,10,1)
            If @error Then ExitLoop
            If StringLen($sBuff) > 0 Then ExitLoop

            Sleep(100)
        WEnd

        ConsoleWrite(@CRLF & "!   Reply: " & $sBuff & @CRLF)
        If StringRight($sBuff, 2) = "00" Then ; Authentication successful

            ConsoleWrite("Authenticated..." & @CRLF)
            
            While 1
                ; Now you can work on requests to whatever, from this point the request is similar to Socks4
                
                Sleep(10)
            WEnd

        Else ; Authentication failed

        EndIf
    Else
        ; In case the server do not discuss by the book!!
        
    EndIf
Else
    ; Connecting error
    ConsoleWrite("Connecting error " & " - " & @error & @CRLF)

EndIf

TCPCloseSocket($socket)
TCPShutdown()
Exit

Was helpful to me:

https://en.wikipedia.org/wiki/SOCKS#SOCKS5

https://blog.zhaytam.com/2019/11/15/socks5-a-net-core-implementation-from-scratch/

 

 

 

 

Edited by T0ny

Share this post


Link to post
Share on other sites

I found myself needing to search different parts of a large array and did not need all the results from _ArrayFindAll, so I wrote a similar function, which I call _ArrayFindRanges. Instead of $iStart and $iEnd, you use ranges separated by semi-colons (ex: "2-5;8-12;18-30;42-50"). I tested various aspects and I am not finding any flaws yet. If anyone has suggestions to make it better or more efficient, I would love to hear from you. Hope you guys like it!

🤠

Update! (10-6-2020)

I took seadoggie01's suggestion to only ReDim once (Thx, seadoggie01!). Also, while integrating this function into a script, I discovered I was forcing the $iForward parameter to be forward searching only in the _ArraySearch call. I fixed that as well. I changed where Default is passed to $iRanges will search the entire array (this makes it act like _ArrayFindAll). Lastly, I discovered if you entered nothing or incorrect data for $iRanges, it will crash your program. I included a check that will return an error code instead of going through the loops, letting you know you have no matches because you passed improper data for $iRanges. I just realized I need to put in more checks to make sure only numbers are passed and possible error codes - I will work on this.

The syntax should be the same except for not receiving errors for having the correct number of parameters. _ArraySearch uses 9 parameters and my function uses 8 parameters (the $iStart and $iEnd are replaced with $iRanges).

It's working efficiently and perfectly in my script. I will work on more error checking and update when I have completed it.

Updated code:

#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w- 7
#include <array.au3>

; Examples

Local $aValues[20] = ["", "", "you", "you", "you", "me", "me", "", "", "you", "me", "", "", "me", "you", "me", "", "", ""] ; 1D array
Local $aValues2[2][4] = [["peter", "paul", "mary", "paul"], ["peter", "mary", "mary", "peter"]] ; 2D array
Local $test, $testErrorNotArray = "wow"

$test = _ArrayFindRanges($aValues, "Me", "2-7;12-18", 0, 0, 0, False) ; test 1D array
If @error Then MsgBox(0, "", @error)
_ArrayDisplay($test)

$test = _ArrayFindRanges($aValues2, "mary", "1-4", 0, 0, 1, True) ; test 2D array choosing to search columns instead of rows
If @error Then MsgBox(0, "", @error)
_ArrayDisplay($test)

$test = _ArrayFindRanges($aValues2, "joe", "1-4", 0, 0, 1, True) ; test 2D array, testing @error result
If @error Then MsgBox(0, "", @error)
_ArrayDisplay($test)

$test = _ArrayFindRanges($testErrorNotArray, "joe", "1-4", 0, 0, 1, True) ; test 2D array, testing a different @error result
If @error Then MsgBox(0, "", @error)
_ArrayDisplay($test)

#cs

Syntax:
_ArrayFindRanges ( Const ByRef $aArray, $iRanges ("num1-num2;num3-num4;num5-num6"), [, $iCase = 0 [, $iCompare = 0 [, $iSubItem = 0 [, $bRow = False]]]]]] )
Note: $iRanges must enclose all the number ranges in quotes and separate the various ranges by semi-colons.

Success:
An array of all index numbers in array containing $vValue.

Failure:
Sets the @error flag to non-zero (see _ArraySearch() description for @error).

Remarks
The values of $iCompare can not be combined together.

#ce

; #FUNCTION# ====================================================================================================================
; Title .........: ArrayFindRanges
; AutoIt Version : 3.3.14.2
; Description ...: Search ranges of an array (similar to _ArrayFindAll)
; Author ........: abberration
; Thanks ........: seadoggie01 for improvement suggestions
; ===============================================================================================================================

Func _ArrayFindRanges(Const ByRef $aArray, $vValue, $iRanges, $iCase = 0, $iCompare = 0, $iForward = 1, $iSubItem = 0, $bRow = False)
    Local $iIndex = 1, $avResult[UBound($aArray)], $aRangeSplit, $aValuesSplit, $iResult
    If $iRanges = Default Then $iRanges = "1-" & UBound($aArray)
    If $iCompare = Default Then $iCompare = 0
    If $iSubItem = Default Then $iSubItem = 0
    If $bRow = Default Then $bRow = False

    $aRangeSplit = StringSplit($iRanges, ";")
    If $aRangeSplit[1] = "" Then
        Return SetError(@error, 0, -1)
    Else
        For $i = 1 To $aRangeSplit[0]
            $aValuesSplit = StringSplit($aRangeSplit[$i], "-", 2)
            For $j = $aValuesSplit[0] To $aValuesSplit[1]
                $iResult = _ArraySearch($aArray, $vValue, $j, $aValuesSplit[1], $iCase, $iCompare, $iForward, $iSubItem, $bRow)
                $j = $iResult
                If $iResult = -1 Then ExitLoop
                $avResult[$iIndex - 1] = $iResult
                $iIndex += 1
            Next
        Next
    EndIf
    If $iIndex = 1 Then Return SetError(@error, 0, -1)
    ReDim $avResult[$iIndex - 1]
    Return $avResult
EndFunc   ;==>_ArrayFindRanges

 

Edited by abberration
Last update for today. Included thanks to seadoggie01 in function header and a couple grammar fixes.

Share this post


Link to post
Share on other sites

My two three cents: (whatever they're worth :D )

1. Arrays resizing (ReDim) is a costly process. To speed things up, don't do it every time you find a match. Instead, either calculate the largest number of possible results up front and then size your array, or start with an array of say 100 and add as needed. At the very least, right now you're resizing your array after every match and at the end. Instead, just resize it when you get a match (start with $iIndex = 0 and resize before adding the value to the array).

2. I'm not sure (because I haven't tested), but I would think that calling _ArrayFindAll once instead of _ArraySearch many times would be more efficient.

3. I think that an invalid string passed in $iRanges (shouldn't that be $sRanges?) will break your function... you aren't checking the @error value of StringSplit. And yeah, GIGO, but I try to handle all the garbage I can find

That said, as long as it works for you, who cares?! :) I probably would've started with the results from _ArrayFindAll and chucked out all the results I didn't need, but that's probably wasteful. It's a good function if you have a need for it!


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

Hi, seadoggie01,

You make a great point about ReDim. I will look at the code soon and try to rework it to only ReDim once.

_ArrayFindAll is what I used to model my function. It uses _ArraySearch for it's results, so using _ArrayFindAll should not be any faster.

You are correct, I should have done more error checking. Before I put in the one @error, when I passed bad info, it only returned an empty array. When I put in the one @error, it seemed to return the correct error handle. But I do need to do more testing in this area.

I got it working and was very tired, so I just posted it. I will continue working on it. I think it could be a very useful addition when it's perfected. I have already determined that declaring the default value of $iRanges is useless as it is. I need to work on that.

Thank you for your suggestions. I will use them to improve the script.

 

Share this post


Link to post
Share on other sites
On 9/17/2020 at 1:55 PM, jchd said:

Simpler:

; Returns the string on the left side of a search string consisting of digits only
ConsoleWrite(_StringGetDigitLeft("1234abcd5678") & @CRLF) ; Results in "1234"
ConsoleWrite(_StringGetDigitLeft("abcd5678") & @CRLF) ; Results in ""

; Returns the string on the right side of a search string consisting of digits only
ConsoleWrite(_StringGetDigitRight("1234abcd5678") & @CRLF) ; Results in "5678"
ConsoleWrite(_StringGetDigitRight("1234abcd") & @CRLF) ; Results in ""

Func _StringGetDigitLeft(Const ByRef $sString)
;   Return String(Number($sString)) ; requires at least one leading digit
    Return StringRegExpReplace($sString, "^(?|(\d+)|())(.*$)", "$1")
EndFunc

Func _StringGetDigitRight(Const ByRef $sString)
    Return StringRegExpReplace($sString, "(.*?)(?|(\d+)|())$", "$2")
EndFunc

 

Hello @jchd,

I did a small benchmark to compare the performance of our _StringGetDigitLeft and _StringGetDigitRight functions.
For this purpose I have used the following password list: https://github.com/danielmiessler/SecLists/blob/master/Passwords/Common-Credentials/10-million-password-list-top-1000000.txt
The code of my benchmark was as follows:

Local $asPasswordList = 0
Local $hTimer = 0
Local $fDiff = 0.0

$asPasswordList = FileReadToArray("10-million-password-list-top-1000000.txt")
$hTimer = TimerInit()
For $sPassword In $asPasswordList
    _StringGetDigitLeft($sPassword)
;~  _StringGetDigitRight($sPassword)
Next
$fDiff = TimerDiff($hTimer)
ConsoleWrite($fDiff & " ms" & @CRLF)

It turned out that your functions based on regular expressions were each over 50% faster!
So I have to give you credit for the fact that your variants would be technically preferable in any case. 🏆👏

Edited by DonChunior

Share this post


Link to post
Share on other sites

Well, it's not always the case that regexps are the fastest way to perform a given task. However once one takes the pain to reasonably master their "strange" syntax and specific features, they can prove elegant and unvaluable in a large number of use cases. Of course their power tends to make overuse tempting.


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
18 hours ago, DonChunior said:

It turned out that your functions based on regular expressions were each over 50% faster!

And I'd like to mention that it is probably because AutoIt has a lot of overhead in string processing and the interpreter itself is a bit slow. Compare that to an optimized RegEx engine written in C(++) and it is clear why it is faster, despite being logically a more complex operation :)

Speed isn't everything, and AutoIt isn't that fast really, so unless you are processing many strings in bulk, it is okay to use whatever you prefer, it won't make a significant difference in operation.

Share this post


Link to post
Share on other sites
4 hours ago, TheDcoder said:

And I'd like to mention that it is probably because AutoIt has a lot of overhead in string processing and the interpreter itself is a bit slow. Compare that to an optimized RegEx engine written in C(++) and it is clear why it is faster, despite being logically a more complex operation :)

Speed isn't everything, and AutoIt isn't that fast really, so unless you are processing many strings in bulk, it is okay to use whatever you prefer, it won't make a significant difference in operation.

That my version would be easier to understand (for me) was admittedly a subjective opinion.
I just wanted to look at the topic more objectively, hence my benchmark.

Share this post


Link to post
Share on other sites

Code to a String:

This is a tool to modify code from the clipboard to be pasted as code that can let one program write a file and run the code from that file.

I had written an #include file that needed to run a separate program to make a window, but I didn't want to hard code the location of the include file or the window. So I decided to carry the window program within the main program.

To do this I wrote the separate program to make the window, then copied it to the clipboard and ran the program below that takes the clipboard and reformats it into code that when run puts that code into a string.  

I copy that code into the main program then when I need to run the window program the main program calls the function that  takes the string 
writes it to a file and runs the file.

 

;Make a $sString of code to include in prgm
;Put the code on the clipboard run the prg and paste the string building code in your prg
#include <array.au3>
Global $q = Chr(39)
$aStr = StringSplit(clipget(),@CRLF,3) ;ArrayFromClip
for $i = 0 to UBound($aStr) - 1
    if $i = 0 then
        $aStr[$i] = "$sString = " & $q & $aStr[$i] & $q & "& @CRLF"
    Else
        $aStr[$i] = "$sString &= " & $q & $aStr[$i] & $q & "& @CRLF"
    EndIf
Next
_ArrayToClip($aStr,@CRLF)

So copying the program above to the clipboard and running it would produce...

$sString = ';Make a $sString of code to include in prgm'& @CRLF
$sString &= ';Put the code on the clipboard run the prg and paste the string building code in your prg'& @CRLF
$sString &= '#include <array.au3>'& @CRLF
$sString &= 'Global $q = Chr(39)'& @CRLF
$sString &= '$aStr = StringSplit(clipget(),@CRLF,3) ;ArrayFromClip'& @CRLF
$sString &= 'for $i = 0 to UBound($aStr) - 1'& @CRLF
$sString &= '   if $i = 0 then'& @CRLF
$sString &= '       $aStr[$i] = "$sString = " & $q & $aStr[$i] & $q & "& @CRLF"'& @CRLF
$sString &= '   Else'& @CRLF
$sString &= '       $aStr[$i] = "$sString &= " & $q & $aStr[$i] & $q & "& @CRLF"'& @CRLF
$sString &= '   EndIf'& @CRLF
$sString &= 'Next'& @CRLF
$sString &= '_ArrayToClip($aStr,@CRLF)'& @CRLF
$sString &= ''& @CRLF

Adding:

FileWrite(@workingdir & "filename.au3",$sString)
ShellExecute(@workingdir & "filename.au3")

would write it and run it.

You could add code to erase the file as part of $GUI_EVENT_CLOSE or just before the codes exits with OnAutoItExitRegister.  That way you don't end up with multiple copies if you run in different folders.

I hope this is understandable :)  but I happy to answer a question if it isn't.

Share this post


Link to post
Share on other sites

While I hate this kind of file, I recently had to re-write an Excel VBA script that wrote the worksheet to a fixed width format file. It isn't perfect, but this is what I have so far:

; #FUNCTION# ====================================================================================================================
; Name ..........: _Export_FixedWidth
; Description ...: Exports an array to a file using a fixed width format
; Syntax ........: _Export_FixedWidth($avData, $aiWidth, $aiPrecision, $sFile[, $sNewLine = Default])
; Parameters ....: $avData              - a 2D array of variants.
;                  $aiWidth             - a 1D array of integers.
;                  $aiPrecision         - a 1D array of integers. Default copies $aiWidth.
;                  $sFile               - full file path to save as or a file handle.
;                  $sNewLine            - [optional] newline. Default is @CRLF.
; Return values .: Success - True
;                  Failure - False and sets @error:
;                  |1 - $avData isn't a 2D array
;                  |2 - $avData has more columns than (@extended) 1 - $aiWidth or 2 - $aiPrecision
;                  |3 - Unable to write data to $sFile
; Author ........: Seadoggie01
; Modified ......: December 17, 2020
; Remarks .......: $aiWidth's and $aiPrecision's sizes must match $avData's column count.
;                  If $sFile is a handle, it will not be closed
;                  Data is right aligned; set $aiWidth's value to negative to right align data. 
;                  +$aiPrecision uses the absolute value (helpful if $aiPrecision = $aiWidth)
; Related .......: 
; Link ..........: 
; Example .......: No
; ===============================================================================================================================
Func _Export_FixedWidth($avData, $aiWidth, $aiPrecision, $sFile, $sNewLine = Default)

    If IsKeyword($aiPrecision) Then $aiPrecision = $aiWidth
    If IsKeyword($sNewLine) Then $sNewLine = @CRLF
    
    ; Ensure 2D array
    If UBound($avData, 0) <> 2 Then Return SetError(1, 0, False)
    ; Check sizes of the arrays
    If UBound($aiWidth) <> UBound($avData, 2) Then Return SetError(2, 1, False)
    If UBound($aiPrecision) <> UBound($avData, 2) Then Return SetError(2, 2, False)
    
    Local $sExportText
    ; For each row
    For $iRow = 0 To UBound($avData) - 1
        ; For each column
        For $iColumn = 0 To UBound($avData, 2) - 1
            ; Format the data as a string using the width and precision assigned by the user
            $sExportText &= StringFormat("%" & (-1 * $aiWidth[$iColumn]) & "." & Abs($aiPrecision[$iColumn]) & "s", $avData[$iRow][$iColumn])
        Next

        $sExportText &= $sNewLine
    Next

    If Not FileWrite($sFile, $sExportText) Then Return SetError(3, 0, False)
    Return True
    
EndFunc

As the VBA was looping through each cell, reading it (and the precision), and writing it to file, it was taking upwards of 10 minutes as the worksheet was 72 columns x 15367 rows (~1.1 M cells). I'm very impatient and this was just too much. Now it takes less than a second to write the data I lied, I needed more error checking. 🤦‍♂️ However, 24 seconds is still much better :D

Edited by seadoggie01

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

Text in, AutoIt string concatenation out, via clipboard. Can choose double or single quote character for output.

; Text2Au3.au3
; Text to AutoIt string concatenation
; Clipboard in + out
; CLD rev. 1/12/2021
;
; Quote char for output code
Global $sQChar = '"'     ; double quote (")
; Global $sQChar = "'" ; single quote (')
;
Global $sIn = ClipGet()
If Not $sIn Then Exit
Global $sArg = ""
If $CmdLine[0] > 0 Then $sArg = $CmdLine[1]; Arg "W" encodes Ascii 128+
Global $aIn = StringSplit($sIn, "")
Global $sCloseQuo = $sQChar & " & "
Global $bTx = 0, $sOut = ""

For $i = 1 To $aIn[0]
    Select
        Case $aIn[$i] = $sQChar
            $sOut &= $sQChar & $sQChar
        Case $aIn[$i] = Chr(13)
            PutCloseQuo()
            $sOut &= "@CR"
            If $i < $aIn[0] And $aIn[$i + 1] = @LF Then
                $sOut &= "LF"
                $i += 1
            EndIf
            $sOut &= " & "
        Case $aIn[$i] = Chr(10)
            PutCloseQuo()
            $sOut &= "@LF & "
        Case Asc($aIn[$i]) < 32
            PutCloseQuo()
            $sOut &= "Chr(" & Asc($aIn[$i]) & ") & "
        Case AscW($aIn[$i]) > 127
            If $sArg = "W" Then
                PutCloseQuo()
                $sOut &= "ChrW(" & AscW($aIn[$i]) & ") & "
            Else
                ContinueCase
            EndIf
        Case Else
            If $bTx = 0 And StringLen($sOut) > 0 Then $sOut &= $sQChar
            $bTx = 1
            If Not $sOut Then $sOut = $sQChar
            $sOut &= $aIn[$i]
    EndSelect
Next
If StringInStr(StringRight($sOut, 3), "&") Then $sOut = StringTrimRight($sOut, 3)
If $bTx = 1 Then $sOut &= $sQChar
ClipPut($sOut & @CRLF & @CRLF)
Exit MsgBox(0, @ScriptName, "Ctrl-V pastes output")

Func PutCloseQuo()
    If $bTx = 1 And StringLen($sOut) > 0 Then $sOut &= $sCloseQuo
    $bTx = 0
    Return
EndFunc  ;==>PutCloseQuo

 

Edited by CarlD

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