Help 'cleaning up' what's in the clipboard?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
supplementfacts
Posts: 40
Joined: 15 Jul 2018, 16:29

Help 'cleaning up' what's in the clipboard?

30 Jul 2021, 21:36

Hi, I'm doing a script which copies a bit from a page's Chrome Inspect. Most of the time it works fine, but occasionally it copies too much, and in a very predictable way. I don't think there's any way to prevent this from happening, simply due to the vagaries of the Internet. But what does seem to be possible (though I don't know how to do it) is to 'clean up' what's on the clipboard.

For example, suppose it copies "IDONTWANTTHISthisiswhatiwantlfsjldsffdsNORDOIWANTTHIS". I'd like a script that checks the clipboard to see if it contains the unwanted strings at the beginning and end ("IDONTWANTTHIS" and "NORDOIWANTTHIS"), and removes them if they're there, so that the clipboard ends up containing only "thisiswhatiwantlfsjldsffds". I think this ought to be simple, but (as I said) I really don't know how to do it.

Can anyone help? Thanks!
User avatar
mikeyww
Posts: 26857
Joined: 09 Sep 2014, 18:38

Re: Help 'cleaning up' what's in the clipboard?

30 Jul 2021, 22:10

Code: Select all

#SingleInstance Force
Loop {
 WinWaitActive, ahk_class Chrome_WidgetWin_1 ahk_exe chrome.exe
 OnClipboardChange("clipChanged")
 WinWaitNotActive
 OnClipboardChange("clipChanged", 0)
}

clipChanged(type) {
 Static list := ["IDONTWANTTHIS", "NORDOIWANTTHIS"]
 If (type != 1)
  Return
 WinGetActiveTitle, aTitle
 If !(aTitle ~= "^view-source")
  Return
 OnClipboardChange("clipChanged", 0), ttext := Clipboard, Clipboard := ""
 For each, string in list
  ttext := RegExReplace(ttext, "i)(^" string "|" string "$)")
 Clipboard := ttext
 ClipWait, 0
 If ErrorLevel
  MsgBox, 48, Error, An error occurred while waiting for the clipboard.
 OnClipboardChange("clipChanged")
}
supplementfacts
Posts: 40
Joined: 15 Jul 2018, 16:29

Re: Help 'cleaning up' what's in the clipboard?

30 Jul 2021, 22:15

Thanks, I'll try it out in just a bit. But I do have a quick question: does it matter if the unwanted strings contain quotation marks? If so, do I just put a backslash in front of them, like for <script src="./ext/ have Static list := ["<script src=\"./ext/", "NORDOIWANTTHIS"]? Thanks again.
supplementfacts
Posts: 40
Joined: 15 Jul 2018, 16:29

Re: Help 'cleaning up' what's in the clipboard?

30 Jul 2021, 22:24

Also, leaving aside the quotation mark issue, I can't get the script to work on a simple test. Here's an animated GIF showing my attempt:

https://i.imgur.com/1JOFg4e.gif

I hope I'm not misunderstanding anything.
supplementfacts
Posts: 40
Joined: 15 Jul 2018, 16:29

Re: Help 'cleaning up' what's in the clipboard?

30 Jul 2021, 22:36

Sorry for posting three replies in a row. I've got it working. I just needed to remove this:

Code: Select all

 If !(aTitle ~= "^view-source")
  Return
And I needed to use double quotes to escape the quotation mark. Thank you so much! I think this will help a lot.
User avatar
mikeyww
Posts: 26857
Joined: 09 Sep 2014, 18:38

Re: Help 'cleaning up' what's in the clipboard?

31 Jul 2021, 07:23

Good to hear. It's possible that the routine will not work with certain strings that are recognized as special characters in regex. If that is the case, the routine can be updated as shown below, to ignore special sequences and instead treat the string as literal text.

Code: Select all

ttext := RegExReplace(ttext, "i)(^\Q" string "\E|\Q" string "\E$)")

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: joedf, OrangeCat, scriptor2016 and 133 guests