Setting clipboard as variable twice

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
prestonage
Posts: 35
Joined: 17 Nov 2021, 10:53

Setting clipboard as variable twice

25 Nov 2021, 22:31

This isn't that big of an issue, but I'm keen to resolve anything I don't understand in AHK as it seems to become more useful to me everyday.

Anyway, I was writing a script that copies a line in Notepad++ and sets it as a variable, then goes up two lines, and sets another as another variable. But, when I do this it sets them both as the second variable.

Code: Select all

!+1::

WinActivate, ahk_exe notepad++.exe			; open Notepad++

Sleep, 50

Send, ^{numpad3}							; navigate to relevant tab

Sleep, 20

Send, {Down 100}							; ensure that cursor is at the bottom

Sleep, 10

Send, ^+x									; Notepad++ shortcut to copy text of current line

Sleep, 20

var1 = %clipboard%							; set clipboard contents as variable

Sleep, 10

Send, {Up 2}								; navigate two lines up

Sleep, 10

Send, ^+x									; copy this line

Sleep, 20

var2 = %clipboard%							; set clipboard contents as second variable

Sleep, 10

WinMinimize, ahk_exe notepad++.exe			; minmize Notepad++

return

Using MsgBox to display var1 and var2 afterwards, I can't get the first line that is copied to appear. Anytime the second variable is set to the clipboard contents, it overwrites it. I would imagine that this is because it is thinking var1 = %clipboard%, and %clipboard% = var2, therefore var1 = var2, but I don't know how to tell AHK that the contents of the clipboard have changed and that they are not equal to each other.
Lepes
Posts: 141
Joined: 06 May 2021, 07:32
Location: Spain

Re: Setting clipboard as variable twice

25 Nov 2021, 23:52

this seems to work:

Code: Select all

!+1::

WinActivate, ahk_exe notepad++.exe			; open Notepad++

Sleep, 50
Send, ^{numpad3}							; navigate to relevant tab
Sleep, 20
Send, {Down 100}							; ensure that cursor is at the bottom
Sleep, 10
Clipboard := ""            ; set clipboard to nothing, so clipwait will work
Send, ^+x									; Notepad++ shortcut to copy text of current line
ClipWait, 1               ; wait at most 1 second for a new content of the clipboard
var1 := clipboard							; set clipboard contents as variable
Sleep, 10
Send, {Up 2}								; navigate two lines up
Sleep, 10
Clipboard := ""
Send, ^+x									; copy this line
ClipWait, 1
var2 := clipboard							; set clipboard contents as second variable
Sleep, 10

Send, % "`nvar1 is: " var1 " and var2 is: " var2 

WinMinimize, ahk_exe notepad++.exe			; minmize Notepad++

return
sofista
Posts: 650
Joined: 24 Feb 2020, 13:59
Location: Buenos Aires

Re: Setting clipboard as variable twice

26 Nov 2021, 08:28

An alternative and different approach, since in my experience moving through Notepad++ editor is prone to fail.

It is assumed that you have already switched to the correct tab in Notepad++.

Code: Select all

!+1::
Clipboard := ""                       ; clear Clipboard
SendInput, ^a                         ; select all
SendInput, ^c                         ; copy selected text
ClipWait 2                            ; pause for Clipboard data
If ErrorLevel {
	MsgBox, Failed. No text selected!
	return                            ; exit if Clipboard is empty
}
arrLines := StrSplit(Clipboard, "`n") ; create an array to easily manage lines
xLines := arrLines.Count()            ; get the number of lines
var1 := arrLines[xLines]              ; assign var1 to last line
var2 := arrLines[xLines - 2]          ; assign var2 to last line minus 2

MsgBox, % var1 "`n" var2              ; show captured data
return
prestonage
Posts: 35
Joined: 17 Nov 2021, 10:53

Re: Setting clipboard as variable twice

27 Nov 2021, 11:37

Lepes wrote:
25 Nov 2021, 23:52
this seems to work:

Code: Select all

!+1::

WinActivate, ahk_exe notepad++.exe			; open Notepad++

Sleep, 50
Send, ^{numpad3}							; navigate to relevant tab
Sleep, 20
Send, {Down 100}							; ensure that cursor is at the bottom
Sleep, 10
Clipboard := ""            ; set clipboard to nothing, so clipwait will work
Send, ^+x									; Notepad++ shortcut to copy text of current line
ClipWait, 1               ; wait at most 1 second for a new content of the clipboard
var1 := clipboard							; set clipboard contents as variable
Sleep, 10
Send, {Up 2}								; navigate two lines up
Sleep, 10
Clipboard := ""
Send, ^+x									; copy this line
ClipWait, 1
var2 := clipboard							; set clipboard contents as second variable
Sleep, 10

Send, % "`nvar1 is: " var1 " and var2 is: " var2 

WinMinimize, ahk_exe notepad++.exe			; minmize Notepad++

return
Ahh, it seems like clipwait was what I was missing. I had tried to empty the clipboard between copies earlier, but that didn't fix it. I was worried there was something about the logic of x = clipboard = y that was tripping me up, and that would surely cause more issues down the line. Just a matter of waiting. What's odd is that I had already tried waiting a decent amount of time, I think, perhaps it wasn't long enough or perhaps there's something special about clipwait.
prestonage
Posts: 35
Joined: 17 Nov 2021, 10:53

Re: Setting clipboard as variable twice

27 Nov 2021, 11:41

sofista wrote:
26 Nov 2021, 08:28
An alternative and different approach, since in my experience moving through Notepad++ editor is prone to fail.

It is assumed that you have already switched to the correct tab in Notepad++.

Code: Select all

!+1::
Clipboard := ""                       ; clear Clipboard
SendInput, ^a                         ; select all
SendInput, ^c                         ; copy selected text
ClipWait 2                            ; pause for Clipboard data
If ErrorLevel {
	MsgBox, Failed. No text selected!
	return                            ; exit if Clipboard is empty
}
arrLines := StrSplit(Clipboard, "`n") ; create an array to easily manage lines
xLines := arrLines.Count()            ; get the number of lines
var1 := arrLines[xLines]              ; assign var1 to last line
var2 := arrLines[xLines - 2]          ; assign var2 to last line minus 2

MsgBox, % var1 "`n" var2              ; show captured data
return
Yes, of course, I agree. I had already found a much better way of doing what I was trying to do (with iniread/write), but not knowing why this wasn't working was bugging me. It was the lack of ClipWait in the script.

Thanks for the replies.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], Ineedhelplz, Spawnova and 240 guests