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!
Help 'cleaning up' what's in the clipboard?
-
- Posts: 41
- Joined: 15 Jul 2018, 16:29
Re: Help 'cleaning up' what's in the clipboard?
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")
}
-
- Posts: 41
- Joined: 15 Jul 2018, 16:29
Re: Help 'cleaning up' what's in the clipboard?
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.
-
- Posts: 41
- Joined: 15 Jul 2018, 16:29
Re: Help 'cleaning up' what's in the clipboard?
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.
https://i.imgur.com/1JOFg4e.gif
I hope I'm not misunderstanding anything.
-
- Posts: 41
- Joined: 15 Jul 2018, 16:29
Re: Help 'cleaning up' what's in the clipboard?
Sorry for posting three replies in a row. I've got it working. I just needed to remove this:
And I needed to use double quotes to escape the quotation mark. Thank you so much! I think this will help a lot.
Code: Select all
If !(aTitle ~= "^view-source")
Return
Re: Help 'cleaning up' what's in the clipboard?
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$)")