Stripping Out Unwanted Elements in a Variable

I am only just dipping my toe into the world of keyboard maestro and have hit a snag.
I have created a simple macro that creates a new folder and names it based on the current mail subject. So far so good.

The emails always come with the subject heading in the following format, although obviously the names and locations change.

"FW: Billy Jones - S0000001 - Location".

I want to be left with a variable "Jones-S0000001" that has no spaces to rename the folder with.

Can anyone help with this problem and also direct me to a source that explains these things in a way that a none programmer can grasp?

The first stage is to define clearly, to yourself, exactly what you want.

The second stage is to translate that definition into things that Keyboard Maestro makes available.

For example:

  • "Billy Jones" -> "Jones". You just want the last word of any name ? What about about double-barreled or hyphenated names ?
  • S0000001 - Location . You specifically want to discard the word Location ? Or just anything after (and including) a final hyphen ? Will there always be a final hyphen ?

Once you've fully explained it to yourself, in relation to all the possible cases that you expect to arise, then you will find it much easier to explain to Keyboard Maestro, in terms of, for example:

1 Like

Thanks for your post.

I want to keep the surname, if it is double barrelled then it would be best to keep both but not essential.

To disregard anything after and including the final hyphen, as well as the space before the last hyphen.

Well, I think you may find that a lot of people here recommend the Regular Expression route, though that requires learning the basics of Regular Expressions.

regexRoute.kmmacros (3.7 KB)

and my personal feeling is that if you are going to learn a language like that, you might get more flexibility and less fiddle from learning a fuller language like JavaScript or Python

JS route.kmmacros (2.1 KB)

and if, very sensibly, you would prefer to avoid learning any language beyond the Keyboard Maestro actions themselves, then I would recommend:

  1. Studying @peternlewis 's response lower down in this thread: How Do I Get the Position of a Character in a String using KM Actions? - Questions & Suggestions - Keyboard Maestro Discourse to find out how to get the position of the next hyphen, and
  2. action:Substring of Variable or Clipboard [Keyboard Maestro Wiki] to find out how to use position indexes to extract sub-strings.
1 Like

Thanks for that, It looks like I have some studying to do. But for the immediate problem the regular expression example worked for me.

You're a star

1 Like