"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 the %TriggerValue% token as a supplied parameter. This parameter is passed to this macro, which counts the number of characters in the parameter and sets a variable to be used to delete those characters.

For instance, if my typed string is -test, then the %TriggerValue% will pass that string to this macro, which will count 5 characters and set the variable to 5. That variable will then simulate the delete key 5 times to get rid of the string.

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!

CURRENT VERSION: 2.0.0 (Saturday, March 19, 2022)

v2.0.0. Saturday, March 19, 2022
Added an if command to prevent the macro from executing unless you are in a text field.
This will prevent accidental unwanted deletion of possibly important information (like the 5 notes I just deleted :laughing:).

VERSION HISTORY (click to expand/collapse)

Version 1.1 uses the character count of the typed string trigger to set the variable used for deleting the characters.
This means you no longer have to specify a number in the "Execute a macro" action, but rather can simply pass the %TriggerValue% token as a parameter.
This makes it a lot easier to use as you can save it as a favorite (with KM10) with the parameter already supplied.

12)Delete typed string trigger text (subroutine).kmmacros (12 KB)

Macro screenshot (click to expand/collapse)

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

Version 1.1 is now up with a much simpler way to set the character count for deleting the typed string characters. See the initial post for more info!

Version 2.0.0. is up. See post #1 for more info.