Applescript to add sender of message to specific group in Contacts

Solution 1:

Try this, it will create a contact with proper first name, last name and email address:

tell application "Mail"
set theMessages to selection
if theMessages is not {} then -- check empty list
    set theSenderName to extract name from sender of item 1 of theMessages
    set nameArray to my split(theSenderName, " ")
    set theFirstName to item 1 of nameArray
    set theLastName to last item of nameArray
    set theEmail to extract address from sender of item 1 of theMessages

    tell application "Contacts"
        set theGroup to group "_TEST"
        set thePerson to make new person with properties {first name:theFirstName, last name:theLastName}
        make new email at end of emails of thePerson with properties {label:"Work", value:theEmail}
        add thePerson to theGroup
        save
    end tell
    end if
end tell

on split(theString, theDelimiter)
    set oldDelimiters to AppleScript's text item delimiters
    set AppleScript's text item delimiters to theDelimiter
    set theArray to every text item of theString
    set AppleScript's text item delimiters to oldDelimiters
    return theArray
end split

There were a few issues with your original attempt, here's how I worked around them.

  • For starters, selection gives you a list of items (even if it's just a list of one), so you need to pick the first element from the selection.
  • In mail, sender gives you a not very useful string with the name and email combined. extract name from and extract address from give you useful strings.
  • The name string is the full name, but Contacts.app expects separate first and last names, so I split that string (using a handy function found here) to make a decent guess at first and last names. This may give unexpected results from strangely formatted names in emails.

If you have any problems with this one, let me know and I'll see if I can fix them. In the future, it may be helpful to run the scripts in AppleScript Editor, and check the Event Log for details on what's failing (error messages are useful, if only to put into Google or give others a starting point for solving your problem).

Solution 2:

I have a more extended version of this idea that presents a list of groups and lets you select from it; it also handles multiple messages. You can add the sender of several different messages to a single group, or you can add one or more senders to multiple groups.

My script uses a keyboard shortcut to add the sender to your Contacts: Cmd-Shift-Y (executed by the script, but I bet you didn't know there was a keyboard shortcut that did this!! It is in the Message menu when a message is selected).

tell application "Mail" to set theSelection to selection
  if theSelection is {} then return beep 2
  if length of theSelection = 1 then
    set thePrompt to "Select the group(s) to which to add the sender of the selected message."
  else
    set thePrompt to "Select the group(s) to which to add the senders of the selected messages."
  end if
tell application "Contacts" to set theList to name of groups
set R to choose from list theList with prompt thePrompt with multiple selections allowed
if R is false then return

tell application "Mail"
    activate
    set theSenders to {}
    repeat with thisMessage in theSelection
        set theSender to extract name from sender of thisMessage
        copy theSender to the end of theSenders
    end repeat
    tell application "System Events" to keystroke "y" using {shift down, command down}
end tell

tell application "Contacts"
    activate
    repeat with thisSender in theSenders
        delay 0.1
        set thePersons to (people whose value of emails contains (thisSender as text))
        if (count thePersons) = 1 then
            repeat with theGroupName in items of R
                add (item 1 of thePersons) to group theGroupName
            end repeat
        else
            repeat with theGroupName in items of R
                repeat with thisPerson in thePersons
                    add thisPerson to group theGroupName
                end repeat
            end repeat
        end if
    end repeat
    save
    set selected of group theGroupName to true
    tell application "System Events" to keystroke "0" using {command down}
end tell