Search code examples
windows-10copy-pasteautoit

how do I load a variable containing the arguments of clipget() into an array of single characters?


I wrote a program in AutoIt that automatically deletes http(/s)://(/www.) from anything it finds in the clipboard.

Now I want it to delete the 'page' specification (the ending) of the URL leaving only the whole site URL.

My first guess is to:

############################################################################
    (a) put the value of the variable set to clipget() into an array of single characters 
        ###    here is my question: #### how do I do that?   ###;
############################################################################
    (b) point to the third backslash in the string;
    (c) set the value to null;
    (d) move to the next argument and do the same;
    (e) repeat until end of array;
    (f) write the values of the array to a variable;
    (g) clipput() the string;
    (h) exit 

I'm new to AutoIt.

How do I load a variable containing the arguments of clipget() into an array?

Many thanks in advance

This is what I have until now:

this app will trim all url prefixes leaving only the unique url string

Here is an example of what I am struggling with. the '50' intiger literal needs to be replaced with a function that calculates the number of characters that exist between the third backslash from the left to the end of the string.

#include <MsgBoxConstants.au3>
Local $url = ClipGet()
;#########################################################################
;########################   that '50' needs to be a function   ###########
Local $url = StringTrimRight($url, 50) 
;#########################################################################
ClipPut($url)
$newurl = ClipGet()
MsgBox($MB_SYSTEMMODAL, "", $newurl)

Here is an example of where i am getting closer to what i want. I am getting the 'whole-site' domain syntax by itself. Now i want to CliptPut() [3] and delete MsgBox. how do i capture the [3] iteration?

#include <MsgBoxConstants.au3>

Example()

Func Example()
    Local $url = CLipGet()
    Local $spliturl = StringSplit($url, '/')
    For $i = 1 To $spliturl[0] 
;#############################################################
;replace 'MsgBox()' with 'ClipPut()'    ######################
;i want to CliptPut() '[3]' only  
        MsgBox($MB_SYSTEMMODAL, "", "$spliturl[" & $i & "] - " & 
;somebody help me please    ##################################
;#############################################################
$spliturl[$i])
    Next
EndFunc

UPDATE

so right now i have this:

#include <MsgBoxConstants.au3>

; Press Esc to terminate script, Pause/Break to "pause"

Global $g_bPaused = False

HotKeySet("{PAUSE}", "HotKeyPressed")
HotKeySet("{ESC}", "HotKeyPressed")
HotKeySet("!d", "HotKeyPressed") ; alt-d
HotKeySet("!c", "HotKeyPressed") ; alt-c



While 1
    Sleep(100)
WEnd

Func HotKeyPressed()
    Switch @HotKeyPressed ; The last hotkey pressed.
        Case "{PAUSE}" ; String is the {PAUSE} hotkey.
            $g_bPaused = Not $g_bPaused
            While $g_bPaused
                Sleep(100)
                ToolTip('Script is "Paused"', 0, 0)
            WEnd
            ToolTip("")

        Case "{ESC}" ; String is the {ESC} hotkey.
            Exit

        Case "!d" ; String is the alt + d hotkey.
            ;####################################################
            ;####     this is my answer   #######################
            ;set url to the string in clipboard
            $url = CLipGet()

            ;split url into pieces divided by the '/' character
            $spliturl = StringSplit($url, '/')

            ;set $site to third value in '$spliturl'
            $site = $spliturl[3]

            ;export $site to clipboard
            ClipPut($site)
                ;####    looks like it does the trick    ##########
                ;#### does anyone have a better way?    ###########
                ;##################################################
        Case "!c" ; String is the alt + c
            $sData1 = ClipGet()
                ;"replace https://" with "" and write the result to clipboard
                $sString1 = StringReplace($sData1, "https://www.", Null)
                ClipPut($sString1)
                $sData1 = ClipGet()
            $sData2 = ClipGet()
                $sString2 = StringReplace($sData2, "https://", Null)
                ClipPut($sString2)
                $sData2 = ClipGet()
            $sData3 = ClipGet()
                ;"replace https://" with "" and write the result to clipboard
                $sString3 = StringReplace($sData3, "http://www.", Null)
                ClipPut($sString3)
                $sData3 = ClipGet()
            $sData4 = ClipGet()
                $sString4 = StringReplace($sData4, "http://", Null)
                ClipPut($sString4)
                $sData4 = ClipGet()

            ;##################################################

    EndSwitch
EndFunc   ;==>HotKeyPressed

Solution

  • #include <array.au3>
    
    $x = _ShortenUrl("https://stackoverflow.com/questions/59360429/how-do-i-load-a-variable-containing-the-arguments-of-clipget-into-an-array-of")
    ConsoleWrite($x & @CRLF)
    
    $x = _ShortenUrl("how do i load a variable containing the arguments of clipget into an array of ...")
    ConsoleWrite($x & @CRLF)
    
    Func _ShortenUrl($string)
        $url = StringRegExp($string, "(?i)(^https?://[^/]*)", 3)
        If Not @error Then $string = $url[0]
        Return $string
    EndFunc   ;==>_ShortenUrl
    

    Use REGEX to get the desired string:
    (?i) case insensitive
    ^ start of line
    https?:// http(s)://
    [^/]* anything until the next /