Clip() - Send and Retrieve Text using the Clipboard

Post your working scripts, libraries and tools.
User avatar
berban
Posts: 97
Joined: 14 Apr 2014, 03:20

Clip() - Send and Retrieve Text using the Clipboard

23 Jun 2023, 17:39

Clip() - Send and Retrieve Text using the Clipboard

This is an update to the V1 post here. Thanks to @elbitjusticiero for bringing it to my attention that people might still be using it.

Note, please let me know if there are errors in my usage of V2 since a lot of this stuff I learned just now in order to post this! :silent:

Code: Select all

; Clip() - Send and Retrieve Text Using the Clipboard
; by berban - updated 6/23/2023
; https://www.autohotkey.com/boards/viewtopic.php?f=83&t=118764
Clip(Text:="", Reselect:="", Restore:=False)
{
	Static BackUpClip, Stored := False, LastClip
	If Restore {
		If (A_Clipboard == LastClip)
			A_Clipboard := BackUpClip
		BackUpClip := LastClip := Stored := ""
	} Else {
		If !Stored {
			Stored := True
			BackUpClip := ClipboardAll() ; ClipboardAll must be on its own line (or does it in v2?)
		} Else
			SetTimer Clip.Bind(,,True), 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 Clip.Bind(,,True), -700
		Sleep 20 ; Short sleep in case Clip() is followed by more keystrokes such as {Enter}
		If (Text = "")
			Return LastClip := A_Clipboard
		Else If ReSelect and ((ReSelect = True) or (StrLen(Text) < 3000)) ; and !(WinActive("ahk_class XLMAIN") or WinActive("ahk_class OpusApp"))
			SendInput("{Shift Down}{Left " StrLen(StrReplace(Text, "`r")) "}{Shift Up}")
	}
}
elbitjusticiero
Posts: 78
Joined: 06 May 2017, 11:07

Re: Clip() - Send and Retrieve Text using the Clipboard

30 Jun 2023, 17:10

Thank you so much! I am a heavy user of this function so I just had to adapt it when I switched to v2. I'm now copying your "official" version. ツ
MoltenKeyboards
Posts: 4
Joined: 05 Jun 2023, 10:58

Re: Clip() - Send and Retrieve Text using the Clipboard

13 Jul 2023, 16:36

Thanks berban for doing this! Your function seems to be more streamlined than what I was able to accomplish!
nhan
Posts: 12
Joined: 18 Mar 2023, 13:22

Re: Clip() - Send and Retrieve Text using the Clipboard

26 Jul 2023, 06:10

Hi @berban, could you please update the examples in v1 to v2 as well? Would be helpful for me to learn how to use it. Thanks a lot!
Descolada
Posts: 1141
Joined: 23 Dec 2021, 02:30

Re: Clip() - Send and Retrieve Text using the Clipboard

13 Aug 2023, 07:34

@berban, I think your use of SetTimer is incorrect. Namely Clip.Bind(,,True) will create a different BoundFunc object every time it's called, so SetTimer Clip.Bind(,,True), 0 will not stop the timer started by SetTimer Clip.Bind(,,True), -700. To demonstrate that this happens, try the following code:

Code: Select all

SetTimer Clip.Bind(,,True), -700
SetTimer Clip.Bind(,,True), 0
This throws an error because the Clip function will still be called.

You could instead store the BoundFunc in a static variable, like so:

Code: Select all

; Clip() - Send and Retrieve Text Using the Clipboard
; by berban - updated 6/23/2023
; https://www.autohotkey.com/boards/viewtopic.php?f=83&t=118764
Clip(Text:="", Reselect:="", Restore:=False)
{
	Static BackUpClip, Stored := False, LastClip, RestoreClip := Clip.Bind(,,True)
	If Restore {
		If (A_Clipboard == LastClip)
			A_Clipboard := BackUpClip
		BackUpClip := LastClip := Stored := ""
	} Else {
		If !Stored {
			Stored := True
			BackUpClip := ClipboardAll() ; ClipboardAll must be on its own line (or does it in v2?)
		} Else
			SetTimer RestoreClip, 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 RestoreClip, -700
		Sleep 20 ; Short sleep in case Clip() is followed by more keystrokes such as {Enter}
		If (Text = "")
			Return LastClip := A_Clipboard
		Else If ReSelect and ((ReSelect = True) or (StrLen(Text) < 3000)) ; and !(WinActive("ahk_class XLMAIN") or WinActive("ahk_class OpusApp"))
			SendInput("{Shift Down}{Left " StrLen(StrReplace(Text, "`r")) "}{Shift Up}")
	}
}
EDIT: for convenient access, another variant where SendInput is replaced with Send:

Code: Select all

; Clip() - Send and Retrieve Text Using the Clipboard
; by berban - updated 6/23/2023
; https://www.autohotkey.com/boards/viewtopic.php?f=83&t=118764
Clip(Text:="", Reselect:="", Restore:=False)
{
	Static BackUpClip, Stored := False, LastClip, RestoreClip := Clip.Bind(,,True)
	If Restore {
		If (A_Clipboard == LastClip)
			A_Clipboard := BackUpClip
		BackUpClip := LastClip := Stored := ""
	} Else {
		If !Stored {
			Stored := True
			BackUpClip := ClipboardAll() ; ClipboardAll must be on its own line (or does it in v2?)
		} Else
			SetTimer RestoreClip, 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 = "") {
			Send("^c")
			ClipWait LongCopy ? 0.6 : 0.2, True
		} Else {
			A_Clipboard := LastClip := Text
			ClipWait 10
			Send("^v")
		}
		SetTimer RestoreClip, -700
		Sleep 20 ; Short sleep in case Clip() is followed by more keystrokes such as {Enter}
		If (Text = "")
			Return LastClip := A_Clipboard
		Else If ReSelect and ((ReSelect = True) or (StrLen(Text) < 3000)) ; and !(WinActive("ahk_class XLMAIN") or WinActive("ahk_class OpusApp"))
			Send("{Shift Down}{Left " StrLen(StrReplace(Text, "`r")) "}{Shift Up}")
	}
}

Return to “Scripts and Functions (v2)”

Who is online

Users browsing this forum: No registered users and 57 guests