Fill variables based on flexible user input

Give a number, color and title that are all tied to one another...

For example:
white, 1, title1
yellow, 2, title2
orange, 3, title3

KM would fill in variables with the missing information.

So if the user provides, for example, "2" then
the number variable is set to "2"
the color variable is set to "yellow"
the title variable is set to "title2".

If the user provides "orange" then
the color variable is set to "orange"
the number variable is set to "three"
the title variable is set to "title3."

If the user provides "title1" then
the title variable is set to "title1"
the color variable is set to "white"
the number variable is set to "1".

What is the best way to approach this? (Maybe dictionary values?)

This is sort of a tricky thing to do, as you want to match on a value anywhere in the line. I'm sure there are a few ways to do it; here's one:

Download Macro(s): _Pick one item from many.kmmacros (4.9 KB)

Macro screenshot

Macro notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
System information
  • macOS 13.6.3
  • Keyboard Maestro v10.2

I've set it up to prompt you for the value to look up, and there is no error checking: If you enter a value not in the list, it will just fail. But run it and enter, for example, 3, then this is the output:

Each of those last three values is assigned to a separate variable. The key to making this work is delimiting the list. I used colons, and there has to be one before each term and after each term, including at the start and end of each line.

EDIT: If you change the colons to some other character, you also need to change them in both regex searches:

  1. (?m)(.*?:%Variable%local_SearchTerm%:.*?)$
  2. :(.*?):(.*?):(.*?):

Replace any colons in each of those with the new character you used. Note that it should be some obscure character unlikely to appear in your text, and not a reserved regex character.

That structure allows the regular expression search to extract the correct line, regardless of which value you entered. With the proper line extracted, it's then broken into its component parts by another regular expression search.


Wow. Thank you. That's two solutions in a couple of hours (one from another thread). With my limited Regex experience, I'm not sure I ever would've figured this out. Thanks!

When I started using KM, I knew very little regex. But seeing how useful it was to manipulate strings, I slowly improved my knowledge, mainly through trial and error. Some time ago, I wrote sort of a beginner's guide to regex, with an example and some links to places to gain more knowledge:


Thanks. I'll take a look at that primer.

I added .* to ...
to either side of the search variable which yields...

So now I can type in any single word from the matching line and the line will still be matched. So if the matching line is...
:yellow:2:The Sound of Music:

then both search terms "sound" and "music" would still return the matching line. Hope that makes sense and that I used .* properly.

It does make sense. I built the macro around your sample data, which only had one word per section, so I didn't worry about multi-word strings :).

To be extra safe, you could use it like this:


the .*? means "optionally match," so if there's nothing there, it won't try to force a match. But with the delimeters, I don't think you really need that.