Can I Use KM to Extract and Reformat Data From a Text String (Email Address)?

Hello all. I am new to KM9 and trying to figure out if KM is the right tool for the job.

I want to extract data and reformat from copied text.

  1. Copy email address in the following format:
    John, Smith A.

  2. Convert to John A. Smith or better John Smith

  3. Paste or copy to clipboard.

Is this possible? Thanks in advance...

Yes, it's definitely possible, and KM is a reasonable choice for getting that done. You'll probably want to use a regular expression (sometimes abbreviated "regex") to do it, and fortunately, even if you don't understand regex, I just did a quick Google search and found plenty of examples.

Hey Murat,

What better tool might you have in mind?  :wink:

Keyboard Maestro makes this kind of job easy (although not necessarily simple) once you know enough about parsing text.

Learning to parse text with regular expressions and various other tools takes time and effort, but once you grasp a few basics getting started isn't too hard.

Most people run into trouble by thinking they can just pick up regular expression and other tools and use them to fine effect. They also try to do too much too fast, or they get focussed on trying to do in one action what is better done in several.

(I'll show a couple of simpler techniques based solely on the input and output example you gave here in a minute.)

With this macro I'm showing the basic strategy I'd use when tackling this sort of task, and I've done the following:

  1. The variable copiedEmailAddress represents text copied to the clipboard.

(Enable the orange actions and disable the red action to automate the copy to clipboard part of the macro.)

  1. Copied text is very often not copied cleanly, so I'm using a filter to remove any vertical and horizontal whitespace.

  2. Remove Email Address using Search and Replace.

  3. Remove Any Single Letter Abbreviations and Corresponding Period if it Exists.

  4. Remove Comma Separator and any Corresponding Whitespace.

  5. Displaying the result in a pop-up window.


Extract Name from Paired NAME-EMAIL_ADDRESS v1.00.kmmacros (8.7 KB)

Okay, here's an “easy” way to accomplish the task with AppleScript.

  1. I'm still using Peter's filter to handle any leading or trailing vertical or horizontal whitespace.

  2. I'm using AppleScript's word parser to break the string up into what it understands a “word” to be.

  3. I know the first two words are what I'm looking for in this case, so I extract them.

  4. I put the two words back together with a space using a slightly tricky technique employing AppleScript's text item delimiters.

Not too mind bending I hope.


EDIT: Agh! Third try... The new one has the suffix: (AppleScript) v1.03

Extract Name from Paired NAME-EMAIL_ADDRESS (AppleScript) v1.03.kmmacros (9.2 KB)

A couple of alternative methods using AppleScript.

Method 01:

set emailAddressStr to "John, Smith A."
return word 1 of emailAddressStr & space & word 2 of emailAddressStr

Method 02:

set emailAddressStr to "John, Smith A."
tell (words 1 thru 2 of emailAddressStr)
   return item 1 & space & item 2
end tell

Here's one using only Keyboard Maestro native actions that uses a “simpler” and more direct regular expression to get the job done.

Extract Name from Paired NAME-EMAIL_ADDRESS (KM Native) v1.50.kmmacros (7.7 KB)

And finally here's one way to use the shell:

Extract Name from Paired NAME-EMAIL_ADDRESS (Shell) v1.00.kmmacros (8.0 KB)

I think I got sloppy and didn't change all my variables to locals and maybe a couple of other things.

Getting too tired to be writing code I reckon.

At least the power's still on her in Dallas. (Knock on wood!)


This is actually easy using non-scripting KM Actions, provided you know a bit of RegEx.
I have to say to more email addresses are usually provide in a different format:
User name <email address>
so for your example, it would be:
Smith, John A. <>

Regardless of the format, it can be easily extracted using RegEx, using the KM Search using Regular Expression action.

So, the simple KM Macro do to this would be:

RegEx Search to Extract Name and EMail:
(\w+), (\w+)(?:.*)?\h+(.+@.+)

It is NOT clear what you want to output, but here is a Macro that will output:
%Local__FName% %Local__LName% <%Local__EMail%>

Modify as you see fit.

BTW, I think your example data is flawed, and should be:
Smith, John A.

Below is just an example written in response to your request. You will need to use as an example and/or change to meet your workflow automation needs.

Please let us know if it meets your needs.


Example Output -- Clipboard

MACRO:   Extract Name and Email Address [Example]

-~~~ VER: 1.0    2021-02-16 ~~~
Requires: KM 8.2.4+   macOS 10.11 (El Capitan)+
(Macro was written & tested using KM 9.0+ on macOS 10.14.5 (Mojave))

DOWNLOAD Macro File:

Extract Name and Email Address [Example].kmmacros
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.