"Smart" deletion of typed string

Howdy folks, I thought I would share a simple macro that has saved my bacon a few times.

I like to use typed strings for certain kinds of macros, but as everybody knows, if you want Keyboard Maestro to delete that string, it can cause issues depending on what app you're in. God forbid you trigger a macro like this while you're editing a macro because you could end up deleting a bunch of actions haha.

So what I do is unselect the option to delete the typed string in whatever macro that uses them. Then, as the first action, I place an "Execute a macro" action using a parameter that matches however many characters the typed string is.

For example, I have a macro that closes all Safari windows whose trigger is "csaf". So I put an execute a macro action at the beginning of it with the parameter "4", and then this macro will use that parameter to delete that number of characters but only if certain apps are at the front (primarily text editors and others who are not affected by the delete character). In the "matches" line you can put in whatever apps you want that you don't mind the delete key being simulated, separated by a RegEx alternation character.

It's not fancy, but it works well for me and maybe will help others too.

EDIT: Updated to use RegEx to match front application instead of a list of applications. Thanks to @tiffle for the tip!

Delete typed string trigger text (nested).kmmacros (3.5 KB)

1 Like

Hi @cdthomer,
I know how you like to make your macros more efficient so how about replacing that massive IF action with something less wordy? Like this:

KM 0 2021-09-09_22-27-01

Test Multiple Application At Front Detection.kmmacros (3.3 KB)

I've included only a few possible application names in the matches field (outlined in red) which of course is a very simple regular expression with each application name separated by a pipe | character. I leave it to you to populate it with your set of apps.

Just food for thought for you!

4 Likes

WHAT?! How about because I didn’t even think of doing it that way :laughing: Man that’s an awesome tip, thanks so much for sharing!

1 Like