Trigger macro when system clipboard changes *and* new clipboard item contains a specific string?

I'd like to have a macro triggered when both of the following are true...

  1. The system clipboard has changed.

  2. The new system clipboard item contains a specific string. (For purposes of this post, let's say "xyz".)

Is there a way to do this?

I suppose one approach would be to use the "clipboard changed" trigger, and then have the first action somehow check if the current system clipboard item contains the string in question. It it does, then the macro can proceed. If it doesn't, then the macro would be cancelled.

But perhaps folks can suggest a better approach?

Yes, this is pretty simple. Here's an example that notifies you when the clipboard contains the designated string:

Clipboard Check.kmmacros (21 KB)

Macro screenshot

2 Likes

Thanks! This makes a lot of sense. With that said, is there no way to do this where the macro doesn't get triggered at all if the new clipboard item doesn't contain the string? I guess there's just not a trigger that would accommodate for this?

A macro is either triggered or it isn't. I wouldn't worry about resource usage for something as simple as this, because if the If/Else condition isn't satisfied, then nothing beyond it will happen.

Additionally, you can restrict this macro by placing it in a group that is only available for specific applications, so that will cut down on the number of runs. This obviously depends on whether you want it to be available for a specific application or globally.

More generally, it's always a good idea to give broader context, in order to avoid an XY problem.

Depending on your ultimate aim, this may or may not be a good/efficient route towards it.

1 Like

This reason probably sounds silly, but honestly, I just get distracted when the KM menu bar icon starts circling. So I just wanted to try to minimize that here. But if not possible, not the end of the world!

That's totally possible:

Clipboard Check.kmmacros (21 KB)

Macro screenshot

You won't find the Don't Rotate Status Menu action in the Keyboard Maestro actions list, as it's something Peter (the developer) posted to the forum. If you want to reuse it for other macros, save it as a favourite.

1 Like

Oh wow. I had no idea about this. Thank you!

2 Likes

TIL the hard way that the "System Clipboard Changed" trigger is not invoked when com.stairways.keyboardmaestro.engine MaxClipboardHistory had been set to 0 to disable pasteboard history.

Leaving this here, in case someone else in the future struggles with that trigger.

1 Like

Thanks at @noisneil. As a layman I'd say that this costs an awful lot of resources. But I guess that Peter has arranged things in such a way that the impact on my system will be minimal?

Want to use this to have a short URL created automatically via TinyURL whenever the clipboard contains the string https://www.dropbox.com/scl/fi/, which I retrieve via:

URL Shortener.kmmacros (3.1 KB)

It's just a content check whenever the clipboard changes, which can be further restricted by current application or other conditions. Sure, if for some reason the clipboard changed 100 times per second, it might be an issue, but that seems unlikely.

Regarding Dropbox links, you could do it all in one:

Copy and Shorten Dropbox Link.kmmacros (40 KB)

Macro screenshot

...or, as you suggested, simply shorten previously copied links that contain https://www.dropbox.com/scl/fi/, as you suggested:

Auto-Shorten Copied Dropbox Links.kmmacros (39 KB)

Macro screenshot

I wasn't sure what the &dl=0 bit was for, as this doesn't appear in links I create, so I left it out.

2 Likes

Thank you!

For security reasons, I want to be sure that I am sharing the correct file, so I modified your macro to display the name of the shared file:


Auto-Shorten Copied Dropbox Links.kmmacros (21.4 KB)

1 Like

Nice!

I've added this to the auto-copy macro too. If you hit โ‡งโŒ˜C while in your Dropbox folder, it will copy the link and notify accordingly. Worth noting that you need to be on macOS Sequoia (or later) for it to work, as it uses the new context menu shortcut, โŒƒโ†ต.

Copy and Shorten Dropbox Link.kmmacros (43 KB)

Macro screenshot

2 Likes

I had to update my macro, since the static Dropbox address isn't as static as expected (it constantly changes, I don't see the system in it).

I now just grab the name of the file to upload from the Finder's selection:


Auto-Shorten Copied Dropbox Links.kmmacros (24.8 KB)
BTW: While trying to create a better regular expression, I managed to make Keyboard Maestro not responding anymore (there obviously was an error in my expression). Even the Kill all macro didn't work anymore.
Image-000166