Autohotkey clipboard variable value forever?

I have the following simple code that sends keystrokes for text on the clipboard with a 15ms delay between characters (I use this to traverse huge lists of treeview items).

Problem: If I copied "text1" to the clipboard and then "text2", this script outputs "text1text2" instead of "text2".

If I reload the script then it prints "text2".

Is there a bug in the code below, or is it a bug when implementing% clipboard% in Autohotkey 1.1.14.03?

#v::
textToType=" "
textToType=%clipboard%
LoopCount:=StrLen(textToType)
;StringLen, LoopCount, textToType
Array%LoopCount%:=textToType
loop %LoopCount%
{
theChar:=Array%A_Index%
Send %theChar%
sleep 15
}
return

      

Update: Thanks for pointing out smarter ways to do this, but I would still like to figure out what is wrong in the above code snippet.

Update 2: The error was in my understanding of the AHK syntax. Array%LoopCount%:=textToType

assigns an integer string value textToType to the th STRING element of the STRING named "Array".

Update 3: (Thanks to @John Y for the clarification)

In fact, there is no "declared" array at all, in the traditional sense. You just have a bunch of individual variables, dynamically created as needed, which end up with numbered names. Array1 and Array2 are not elements in any Array object. They are just two completely independent variables. AutoHotkey provides a way to glue numbers at the ends of names, so you can use them like an array.

+3


source to share


3 answers


The reason your script is not working correctly is because you are using a pseudo-array to store different words from your clipboard.

I have commented out your code to explain what it does:

#v::
    textToType  := "" ; Empty variable
    textToType  := Clipboard ; Move clipboard into variable

    ; Get lenght of the word
    ; Used as array index / loop count
    LoopCount := StrLen(textToType)

    ; Put the clipboard in an array at index 'LoopCount'
    Array%LoopCount% := textToType

    ; Loop through the array as many times
    ; as the string is long
    Loop % LoopCount
    {
        ; Retrieve the word at this index in the array
        theChar := Array%A_Index%
        ; Send the whole word
        Send, % theChar
        sleep 15
    }
return

      

Instead of sending each character at a time, you are sending whole words from specific indices to an array Array

.



Let's say you copy a word Dragon

, this word is 6 letters long. So you would put that in Array6

, then you would loop through your array 6 times using the same variable. At this point, the loop will take each index at a time and move it to theChar

. On your 6th circle in the loop, you put Array6

in theChar

and immediately printed out the entire word.

Then you copy the word Stackoverflow

. This will go in Array13

, and we're going to loop 13 times. On the sixth circle, we will print Dragon

which is at Array6

, and then continue moving until we reach 13, where we will print Stackoverflow

since this is at Array13

.

So why doesn't your script do what you want. Hope this helps a little.

See alpha bravo code example posted for what the correct way to achieve what you want to do.

+3


source


keep it simple



#v::
loop Parse, Clipboard
{
    Send %A_LoopField%
    sleep 15
}
return
      

+3


source


There must be a bug in the implementation of the clipboard assignment in AHK. In the code below, the behavior of AHK is that each time a value is accessed, dir

AHK fetches the last value from the clipboard instead of fetching the value dir

during script activation.

; Remove all CR+LF from the clipboard contents:
dir = %clipboard%
sleep 100
dir := StrReplace(dir, "'r'n")

      

EDIT: To fix this, I added 1 second of sleep before the clipboard destination code:

sleep 1000
; Remove all CR+LF from the clipboard contents:
dir = %clipboard%
dir := StrReplace(dir, "'r'n")

      

100 millisecond sleep didn't seem to work.

Value access dir

now only gives the value of the last clipboard assignment when activated.

0


source







All Articles