Differentiate on How Clipboard Was Changed?

I realize the answer is probably "no," but I thought I'd ask just to be sure. I had pretty much finished my URL processing macro when I discovered a major logic flaw.

It's always ready to go, and launches on clipboard change. I have code that checks to make sure what's on the clipboard should be processed (i.e. not an image, not regular text, and that it is a URL that's in my list of filterable URLs). It was working great, until I ran into the issue:

If I delete something from the clipboard stack, either manually or via another macro (Insert Text by Pasting), that triggers the URL filtering macro. In the case of another macro, I usually then delete the item that was pasted from the clipboard. This results in two clipboard changes, both of which trigger the macro.

That's not a problem, as the "only process some stuff" routine kicks it out right away. Unless the second item in the stack is a URL that the macro just processed. In that case, the macro needlessly runs again.

I thought about changing all my insert text commands to use typing instead of pasting, but that's quite slow for larger blocks of text. And it doesn't solve the problem, because if I manually delete an item from the clipboard, the same problem can occur.

So, is there any way to have a macro only run when something was actively copied to the clipboard, instead of when it changed due to a change in the stack? Assuming no, I'd love to see this as an option for the When Clipboard Changed activation trigger. Maybe it could have an option box for "on any event" or "only when new item added to the stack?"

thanks;
-rob.

I also considered saving the processed URLs to a variable, one by one, then adding a check to my "don't run if" section to see if the URL isn't in the list. But as I copy and paste probably dozens of such URLs daily, that variable would quickly become massive.

-rob.

I'm far from a technical expert, but as far as I know, there's no way to differentiate something being actively copied to the clipboard vs. the stack being manipulated. That said, if you primarily copy via C, you could change the "Trigger on clipboard change" trigger to a C hotkey trigger, and that might effectively do what you want.

Unfortunately, what I'm copying are links in Mail messages, which you have to get to via the contextual menu. So for now, I switched the macro to run using Control-C, which first activates the contextual menu under the mouse location, then selects the Copy Link menu item.

And while this is an OK solution for Mail, it means I need to hand-code support for any other apps where I'm likely to want URL filtering. (And in an ideal world, I'd really like this script running all the time, in all apps, so it catches filterable URLs wherever I get them.)

-rob.

Ah ha! I found a workaround. It's not 100% ideal, but it works, and it's relatively painless.

After I clean a URL, when I paste it back to the clipboard, I add a nonsense bit at the end:

#XgYn5 (or whatever)

Now, in the macro, in the exclusion check at the front, I simply check if the clipboard ends with #XgYn5. If so, the macro aborts.

At least in my testing, adding a parameter to the URL doesn't bother any page I tried it with—it's either dropped completely, or still listed as part of the URL but ignored.

Good enough for me!

-rob.

(Edit: A friend told me that ? will be processed by the server, but hashes (#) are ignored. Even better.)

3 Likes

There's an example here, uses Clipboard Changed trigger:

Unfortunately, CLIPBOARDSEED() changes when you delete something from the pasteboard, so it won't work for me in this case.

thanks;
-rob.

Hey Rob,

A couple of things...


I use the Delete Past Clipboard action to actively remove things I'm done with from the clipboard history.

I don't know if that will help with your task, but take note anyway – it's a useful feature.


It's possible to scrape URLs out of Mail with AppleScript – which might possibly streamline your workflow.

It's not always simple, so I'd need to see a sample or two.

P.M. me if necessary.

-Chris

Actually it's the use of Delete Past Clipboard that causes the issue. When you delete an item from the clipboard, the next-oldest becomes topmost. This also registers as a clipboard change, so my macro would run again. The use of my hashtag perfectly solved the problem, though.

I'm not so much interested in scraping emails out of Mail, but cleaning up links from newsletters so they can't track me when I read things. And yea, there's some fun regex in there to parse the URLs, but it seems to be working :).

The script is working quite well now, I'll be posting it shortly.

-rob.

2 Likes

Hi @griffman ,
That is a great solution - and the hash (#) tells the browser to look for an anchor link in the page that has an anchor name you have provided. The random alphanumeric string you showed will likely never match any anchor you come across. So it is ignored as you say.
Cheers,
Paul

2 Likes