Clip() - Send and Retrieve Text using the Clipboard
-
- Posts: 75
- Joined: 06 May 2017, 11:07
Re: Clip() - Send and Retrieve Text using the Clipboard
Hi! Another grateful, longtime user of Clip() here.
I'm now trying to port it to AHK v2 so I can keep enjoying the goodness. But I'm stuck at the part where it uses a label and then retrieves the value of A_ThisLabel, which doesn't exist anymore in v2. SetTimer also requires functions and not labels now.
Do you know how I could adapt that part of the function so that I can make it work in v2?
I'm now trying to port it to AHK v2 so I can keep enjoying the goodness. But I'm stuck at the part where it uses a label and then retrieves the value of A_ThisLabel, which doesn't exist anymore in v2. SetTimer also requires functions and not labels now.
Do you know how I could adapt that part of the function so that I can make it work in v2?
-
- Posts: 75
- Joined: 06 May 2017, 11:07
Re: Clip() - Send and Retrieve Text using the Clipboard
I ended up doing this, which "works", but I'm not sure I haven't introduced bugs:
Code: Select all
Clip(Text := "", Reselect := "", Restore := "")
{
Static BackUpClip := "", Stored := False, LastClip := "", Restored := ""
If (Restore) {
If (A_Clipboard == LastClip)
A_Clipboard := BackUpClip
BackUpClip := LastClip := Stored := ""
return Clip()
} Else {
If !Stored {
Stored := True
BackUpClip := ClipboardAll() ; ClipboardAll must be on its own line
} Else {
SetTimer ClipRestore, 0
}
LongCopy := A_TickCount, A_Clipboard := "", LongCopy -= A_TickCount
; LongCopy gauges the amount of time it takes to empty the clipboard
; which can predict how long the subsequent clipwait will need
If (Text = "") {
SendInput("^c")
ClipWait (LongCopy ? 0.6 : 0.2), True
} Else {
A_Clipboard := LastClip := Text
ClipWait 10
SendInput("^v")
}
SetTimer ClipRestore, -700
Sleep 20
; Short sleep in case Clip() is followed by more keystrokes such as {Enter}
If (Text = "")
Return LastClip := StrReplace( A_Clipboard, "`r" )
Else If (ReSelect = True) or (Reselect and (StrLen(Text) < 3000)) {
Text := StrReplace(Text, "`r")
SendInput("{Shift Down}{Left " StrLen(Text) "}{Shift Up}")
}
}
Return
ClipRestore() {
Clip( , , "RESTORE" )
}
Clip:
Return Clip()
}
Re: Clip() - Send and Retrieve Text using the Clipboard
I recently found this and love the idea. In practice, I'm encountering an issue with the function restoring BackupClip after successfully copying the highlighted text. Absolutely nothing special about the use case and the issue will generally crop up after the script has been active for some time. Anyone else had issues with the function not working in v1?
-
- Posts: 4
- Joined: 05 Jun 2023, 10:58
Re: Clip() - Send and Retrieve Text using the Clipboard
@elbitjusticiero I know you said you got it figured out but when I was trying your code out I ran into some issues with Clip() continuously recalling itself. I think this stems from the call to Clip() after the return in line 12.
I may be way off base, but after doing some msgbox debugging, I found that removing that mention of Clip() in line 12 (Since any output by that point was already returned anyway) and adding an additional (maybe unnecessary) did the trick. The code below works for me. I hope others find it useful.
If you want, you could post it to the V2 forums and link here so others can see and contribute if interested.
I may be way off base, but after doing some msgbox debugging, I found that removing that mention of Clip() in line 12 (Since any output by that point was already returned anyway) and adding an additional (maybe unnecessary)
Code: Select all
SetTimer , 0
If you want, you could post it to the V2 forums and link here so others can see and contribute if interested.
Code: Select all
#Warn ; Enable warnings to assist with detecting common errors.
SetWorkingDir(A_ScriptDir) ; Ensures a consistent starting directory.
Clip(Text := "", Reselect := "", Restore := "")
{
Static BackUpClip := "", Stored := False, LastClip := "", Restored := ""
If (Restore) {
If (A_Clipboard == LastClip){
A_Clipboard := BackUpClip
}
BackUpClip := LastClip := Stored := ""
SetTimer , 0
return
} Else {
If !Stored {
Stored := True
BackUpClip := ClipboardAll() ; ClipboardAll must be on its own line
} Else {
SetTimer ClipRestore, 0
}
LongCopy := A_TickCount, A_Clipboard := "", LongCopy -= A_TickCount
; LongCopy gauges the amount of time it takes to empty the clipboard
; which can predict how long the subsequent clipwait will need
If (Text = "") {
SendInput("^c")
ClipWait (LongCopy ? 0.6 : 0.2), True
} Else {
A_Clipboard := LastClip := Text
ClipWait 10
SendInput("^v")
}
SetTimer ClipRestore, -700
Sleep 20
; Short sleep in case Clip() is followed by more keystrokes such as {Enter}
If (Text = ""){
Return LastClip := StrReplace( A_Clipboard, "`r" )
} Else If (ReSelect = True) or (Reselect and (StrLen(Text) < 3000)) {
Text := StrReplace(Text, "`r")
SendInput("{Shift Down}{Left " StrLen(Text) "}{Shift Up}")
}
}
Return
ClipRestore() {
Clip( , , "RESTORE" )
}
}
-
- Posts: 75
- Joined: 06 May 2017, 11:07
Re: Clip() - Send and Retrieve Text using the Clipboard
Thank you, @MoltenKeyboards! It looks like I had introduced a bug after all.
-
- Posts: 75
- Joined: 06 May 2017, 11:07
Re: Clip() - Send and Retrieve Text using the Clipboard
BTW @berban, if you could look at it and ensure it's all correct, it would be helpful for those who made the jump to v2!
Re: Clip() - Send and Retrieve Text using the Clipboard
Hi @elbitjusticiero,
Thanks for bringing this to my attention! I put it on the V2 forums here: viewtopic.php?f=83&t=118764
Let me know if you see any issues with it. I altered what you had slightly according to my understanding of the differences with V2!
Berban
Thanks for bringing this to my attention! I put it on the V2 forums here: viewtopic.php?f=83&t=118764
Let me know if you see any issues with it. I altered what you had slightly according to my understanding of the differences with V2!
Berban