Extracting Email Address From Text

I’d love it if one of you could write me a script/automation/shortcut/TextExpander macro to do the following simple task:

I have a text selection in mail.app that contains one email address, which you can assume is not complex or pathological. For example:

Please contact Mary (Mary.Poppins@example.com) for details

After invoking this, I want the email address (only) on the clipboard.


Hate to ask, but I’m doing a ton of these by hand but don’t have a moment to invest.

This might work…

https://www.icloud.com/shortcuts/20b575a4422b4397ab38a3b3d84723b7

My first shortcut! There’s a keyboard shortcut cmd-opt-e. Should appear in Services menu. You should be able to edit it in Shortcuts app to change the keyboard shortcut or whatever. Works when I tested it. The trick is copy the text to the clipboard first. Couldn’t figure out how to grab the selection.

Just because I may not be clear. Select the text. Copy it to the clipboard. Invoke CMD-OPT-E and voila, the email address alone should reside on the clipboard.

FWIW, I tried this and I get all the emails in the selection. But as the starting premise is there will be only one email in the selection, then that’s moot and I confirm it works here (on macOS 12.3.1)!

1 Like

Here a different approach, saving you the save step at start, a quick action made with automator
Extract email address.workflow.zip (97.5 KB).

File goes in ~Library/Services . Once added a keyboard choice of preference can be set via System Preferences ▸ Keyboard ▸ Shortcuts. Select ‘Services’ from the sidebar and find your service. Add a shortcut by double clicking (none) .

A little more set-up but once done, it avoids the extra copy step in @dmrogers ‘shortcut’ (do they have a name). I used the Automator app as Shortcuts doesn’t seem to have a ‘get current text selection’ option. I think this shows the weakness of porting apps from iOS (where the act of selecting text is an effort in its own right).

This is what the service does:

As with my test above, it finds all emails in the selection but again as your sceenario assumes only one that’s not limitation.

HTH.

2 Likes

Thanks!

This is probably overtaken by events now, but I’ve been extremely frustrated trying to find any documentation that just explicitly says how to have a Shortcut operate on a selection of text (instead of copying it to the clipboard first).

So, just on a guess, this worked. This keyboard shortcut is CTRL-OPT-CMD-E and it worked to extract the email address from selected text in an email on my machine, as well as selecting the text on this page in the example above.

If nothing else, I’ve learned something.

https://www.icloud.com/shortcuts/9f5c82d76edf4e1ab20bb481371fe5c9

2 Likes

As well as the lack of clear documentation, I hate the fad of naming apps for things that are really hard to search about. Doubtless someone will go one further and invent a coding language called ‘variable’.

Shortcuts seems like style over substance. The UI is not the issue, but the range of functions available. iOS != macOs in that regard. ISTM Automator is still a better starting point. Indeed you can still use AppleScript but Automator sorts the rest with the exception of being able to apply a shortcut. It sort of makes sense to use the System Preferences, but it sucks that Automator doesn’t let you open that in-app and let you set the shortcut without losing context/focus.

Still, MB’s request was a good reminder to look at both these tools. I guess it took me c.10 min to dust off Automator, make a solution, test, and describe it. Were it just for me, the last step is not needed. You were absolutely right to add a shortcut as in this instance it is the force multiply (if unintuitive to some) as it reduces the per-instance tasks to:

  • select text
  • call and run [thing] via shortcut
    … then…
  • select target
  • paste extracted email to target
3 Likes

Thank you both for this! The Automator approach worked right away for me. But try as I might I could not get the Shortcuts App shortcut to grab the email address out of the selection.

But, encouraged by the success reported here, I persisted, finally resorting to a sophisticated troubleshooting strategy: restart my Mac and hope for the best. It worked.

Shortcuts clearly is not yet mature on the Mac. But still … amazing what can be done with a few minutes work. Well, maybe more than a few minutes if counting those many extra minutes fiddling around!

Lots of working examples of varying quality and relevance on the r/shortcuts subreddit. And a highly curated collection of originals at MacStories.net.

2 Likes

Many, many thanks for this. It makes processing https://pizzaForUkraine.com/ donations much cleaner and less annoying!

4 Likes