Clip() - Send and Retrieve Text using the Clipboard

Post your working scripts, libraries and tools for AHK v1.1 and older
elbitjusticiero
Posts: 75
Joined: 06 May 2017, 11:07

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

Post by elbitjusticiero » 16 Mar 2023, 14:37

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?

elbitjusticiero
Posts: 75
Joined: 06 May 2017, 11:07

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

Post by elbitjusticiero » 17 Mar 2023, 11:27

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()
}



Jasonosaj
Posts: 46
Joined: 02 Feb 2022, 15:02
Location: California

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

Post by Jasonosaj » 21 Mar 2023, 10:23

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?

MoltenKeyboards
Posts: 4
Joined: 05 Jun 2023, 10:58

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

Post by MoltenKeyboards » 20 Jun 2023, 00:16

@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)

Code: Select all

SetTimer , 0
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.

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" )
	}
}

elbitjusticiero
Posts: 75
Joined: 06 May 2017, 11:07

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

Post by elbitjusticiero » 20 Jun 2023, 17:22

Thank you, @MoltenKeyboards! It looks like I had introduced a bug after all. ;-)

elbitjusticiero
Posts: 75
Joined: 06 May 2017, 11:07

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

Post by elbitjusticiero » 20 Jun 2023, 17:23

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!

User avatar
berban
Posts: 97
Joined: 14 Apr 2014, 03:20

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

Post by berban » 23 Jun 2023, 17:43

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

Post Reply

Return to “Scripts and Functions (v1)”