Extract portion of captured text

I think I have this part of it working, and will share it shortly. They key was to remove the carrot ("^") in the second step of the macro. Now I’m trying to figure out a way to search for the email address using Alfred, and pass the email into this workflow in Keyboard Maestro.

The caret (not carrot) :slight_smile: means “match at the beginning of the string”. $ would mean “match at the end”, by the way.

Is there a particular reason you want to use Alfred for this?
You should be able to do the search with KM, possibly using AppleScript in a KM Execute an AppleScript action (KM Wiki).

If you'll provide the details of where you need to search for the email, perhaps we can help.

Thank you for the replies. No particular reason to use Alfred, other than that I already search for contacts there, and couldn’t see an obvious way to search for contacts in Keyboard Maestro. If I can get this working in Keyboard Maestro, I’d be happy.

I’m searching for the email address in my Mac’s Contacts, as the project management system syncs email addresses (but not URLs) to my Contacts.

Thanks for any help.

Can you post your Alfred workflow to search for a contact?
Changes are that it is using AppleScript within the workflow to accomplish this. If so, you can use that same AppleScript in KM.

I think it would simplify you workflow if you can do it all within KM.

This example macro searches Contacts for the name you supply at the prompt and creates a URL based on a portion of the email.

I don't have numbers in the email addresses of my Contacts so I just grabbed the root email address. Just change ^(.+?). to your \d+ to grab the first digits.

No error checking, just an example of how to script Contacts to retrieve information and build a string with that data. Hope it helps.

Keyboard Maestro “Find Contact” Macro

Find Contact.kmmacros (7.0 KB)


Very nice, compact script. I had been fooling around with it but couldn't arrive at such a compact solution. Well done!

BTW, I also did quite a bit of searching, and I didn't find anything usable on the Internet.

Excellent point, since if the Contact is not found, then an AppleScript error is thrown.

1 Like

Easy enough to add.

tell application "Keyboard Maestro Engine" to set myName to getvariable "Name"

  tell application "Contacts" to set scriptResults to value of email of first person whose name is myName
on error
  set scriptResults to "[ERROR] Contact Not Found: " & myName
end try

return scriptResults
1 Like

Thank you both for this. I appreciate you diving in and giving a workable solution.

One feature that Alfred has, is that it filters contacts as you type, looking only in parts of the address if needed. Anyway, I really appreciate your work on this.

Thanks for the nice words (and the AppleScript test too).

You might find this article useful:
Integrating Alfred With Keyboard Maestro

Alfred seems to need a custom workflow to pass a selection to Keyboard Maestro (and that means buying the PowerPack). JMichaelTX pointed you to one such workflow that would work, though.

Alternately, you could remove the prompt (and if-then-else structure) in my example macro with a list (a very long list) or your contact names and just select from that. Replace the AppleScript code to set the myName variable in the Execute AppleScript action with:

tell application "Contacts"
	set allNames to the name of every person
end tell
set myName to {choose from list allNames with prompt "Select a name:"}

Another alternative is to use the MACRO: Spotlight Search Prompt by @DanThomas. It is very fast and would give you a dynamic lookup of the Contact's name and/or email address, where you can search on any part of the name/address.

This would be more like the Alfred Contact search than using the simple AppleScript choose from list.

But it sounds like @cyberskier already has the Alfred tools he needs -- just needs to connect the results with KM.

@cyberskier, one point of clarification: Do you need to search on the email address field, or the Contact's name? If you still need help, could you please provide your detailed manual workflow, starting with the source data (what is it, and where do you get it from?).

I had this working perfectly about two hours ago, and suddenly Alfred is throwing up an error (“App cache not yet initialized”). I’m trying to work through that in the Alfred forum.

Here is the Alfred workflow, that finds the contact, places the email address on the clipboard, and then launches my Keyboard Maestro macro:

Here is the Keyboard Maestro macro that Alfred launches:

Thanks for posting that Alfred to KM article. Pretty amazing stuff.

I saw Dan's macro actually but thought it used Spotlight's index. I see you have to supply your own data (in one of three formats) instead, though. So I installed it and gave it a whirl.

This example builds the menu options from a list of names compiled in AppleScript by Contacts then passes the selection on. Didn't bother with the regex to extract something from the email address, so it just displays the selected name and associated email address.

Dan's custom HTML prompt, which supports type-ahead like Alfred, only requires installing a single .pkg which is not too onerous.

Keyboard Maestro “Find Contact II” Macro

Find Contact II.kmmacros (5.7 KB)

Cool. I'll take a look at you new macro.

Since you are so industrious today, and you came up with a very compact solution earlier, I'd love to have your thoughts on search for a string in the email address.

For example, I want to find all Contacts that use a specific domain in their email address.
I need a list of Contact Full Name, with associated emails that match.

I've done a lot of searching on the 'net, and have not found anything useful.
Here is the script I have so far. I know I can continue it and brute-force a solution, but surely there must be a better method.

Here is what I have so far:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

use BPLIB : script "BridgePlus" version "1.3.2"

set emailFilter to "gmail"

-- {"Full Name", {"email address1", "email address2"}
set desiredResult to {¬
  {"John Doe", {"jdoe_home@gmail.com", "jdoe_work@gmail.com"}}, ¬
  {"Jack Smith", "jsmith@gmail.com"}}

tell application "Contacts"
  set emailList to value of every email of every person whose (value contains emailFilter)
  #  set peoList to name of every person whose email's value contains emailFilter
  # which returns ALL Contacts
  # when I just want those with matching emails
  set peoList to name of every person
end tell

-- how to I associate Contact Name with each email?

set emailClean to BPLIB's listByDeletingBlanksIn:emailList

Example Results

JMichaelTX wrote:

Since you are so industrious today, and you came up with a very
compact solution earlier, I'd love to have your thoughts on search for
a string in the email address.

Here you go:

set gmailList to ""
set myCount to 0
set emailFilter to "gmail"
tell application "Contacts"
     repeat with thisPerson in people
         set theseEmails to ""
         repeat with thisEmail in every email of thisPerson
             if the value of thisEmail contains emailFilter then
                 set myCount to myCount + 1
                 if (myCount > 1) then
                     set theseEmails to theseEmails & ","
                 end if
                 set theseEmails to theseEmails & "\"" & (value of 
thisEmail) & "\""
             end if
         end repeat
         if theseEmails is not equal to "" then
             set gmailList to gmailList & "{\"" & (name of thisPerson) & 
"\",{" & theseEmails & "}}" & "\r"
             set myCount to 0
         end if
     end repeat
end tell
display dialog gmailList

Mike Pasini
mikepasini.com http://www.mikepasini.com
mrpasini@gmail.com mailto:mrpasini@gmail.com


Been thinking about this “Spotlight prompt” issue.

While Dan’s macro does the job, it depends on jQuery, which it hides in the .pkg file installed with the macro. You can, alternately, use Alfred if you buy the PowerPack.

But then I noticed Trigger Macro by Name uses a Spotlight-like prompt itself. So the functionality is already somewhere in Keyboard Maestro, just not (apparently) user accessible.

It would be nice (no potential multiple jQuery libraries or PowerPack requirement) to have an Action to access it with a required dataset (perhaps in any of Dan’s three formats) and optional icon (default: KM icon).

1 Like


1 Like

Not sure you ever saw the solution (below). I may not have replied correctly.