Please Help With Clipboard StringReplace Issue! Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
LeroyMcCheeks
Posts: 10
Joined: 12 Dec 2019, 16:22

Please Help With Clipboard StringReplace Issue!

23 Jan 2021, 23:47

Hi All,

I use a similar code for my job and it works just fine. Nearly every other use of it in this script works as well. The only problem seems to be acronyms and I can't figure out why exactly. So I have something like this:

Code: Select all

OnClipboardChange:
StringReplace, Clipboard, Clipboard, GS, GSW, all
This works well with longer words or even phrases, functioning as an auto-correct but for the clipboard instead of typing. But this example will return GSWWWWWW and the amount of W's seems to vary. It does the same thing with Jr. and Jr but I can't figure out why, exactly. It seems like it should be so easy -- and I'm sure it actually is -- but my brain is just not grasping it. Please help!

Thanks in advance.
User avatar
Spawnova
Posts: 557
Joined: 08 Jul 2015, 00:12
Contact:

Re: Please Help With Clipboard StringReplace Issue!

24 Jan 2021, 02:31

I think that's happening because changing the clipboard through your program also calls the clipboard change event, so you are catching yourself in a loop, I'm not sure the best way to go about it, but adding a time check seems to work for me

Code: Select all

#persistent
OnClipboardChange("ClipChanged")
clipboard := "letters and numbers 1234 gs words gs gs gsgsgs  gs  jr jr jrjrjr  gs"
;the clipboard now contains the corrected information
return

ClipChanged(dataType) {
	static last := 0,replacements := []
	if (last = 0) { ;last will serve 2 functions, to determine first run and determine last clipboard change
		
		replacements["gs"] := "GSW"  ;define replacements here
		replacements["jr"] := "Jr."
		
		last := 1 ;set last to any value above 0 so this code only runs once
	}
	if (dataType = 1 and a_tickcount > last) { ;clipboard is not empty and does not contain non text data
		data := clipboard
		for k,v in replacements
			data := StrReplace(data,k,v)
		clipboard := data
		last := a_tickcount + 50  ;any clipboard event in the next 50ms is ignored
	}
}
Edit: Woops, when I copied the script over to here I still had it running and it changed stuff XD, fixed that.
User avatar
boiler
Posts: 17044
Joined: 21 Dec 2014, 02:44

Re: Please Help With Clipboard StringReplace Issue!  Topic is solved

24 Jan 2021, 07:45

Since the routine itself is causing the clipboard to change, it is indeed causing a loop. The solution is to temporarily turn off OnClipboardChange as shown below. Also, non-deprecated versions of OnClipboardChange and StrReplace should be used, as is done here.

Code: Select all

#Persistent
OnClipboardChange("Replace")
return

Replace() {
	OnClipboardChange("Replace", 0)
	Clipboard := StrReplace(Clipboard, "GS", "GSW")
	OnClipboardChange("Replace")
}
LeroyMcCheeks
Posts: 10
Joined: 12 Dec 2019, 16:22

Re: Please Help With Clipboard StringReplace Issue!

24 Jan 2021, 15:16

Thanks so much, both of you! The second post is a little easier and it worked. I appreciate the help.

One more quick question I thought of when I ran into another issue after fixing this... is there a way to only do this replacement if the case matches? I know that's part of the syntax in other commands, something like :*: I believe (or maybe that's partial match).

See, I had a spreadsheet for fantasy basketball and it imports data from various sites. The problem is, some sites use the team abbreviations and some use the full names. Same with players, some use the proper non-English characters for the Europeans and others don't. In order for my VLOOKUP function to work in sheets, it all needs to match. And it's a pain to retype every day when there's a new lineup. So I'm using AHK and it's making everything uniform. The new problem is -- it's minor cuz I have workarounds -- when I'm replacing NY to NYK (New York Knicks) it is also also changing Carmelo Anthony to AnthoNYK, for example.

Thanks again!
User avatar
boiler
Posts: 17044
Joined: 21 Dec 2014, 02:44

Re: Please Help With Clipboard StringReplace Issue!

24 Jan 2021, 16:51

Put the following line at/near the top of your script (like right after #Persistent) to make the StrReplace case sensitive:

Code: Select all

StringCaseSense, On

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Aqualest, erann, jdfnnl and 347 guests