Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I'm trying to set up an Applescript to add the sender of a selected message in Apple Mail to a specific group in the Contacts app. By reconfiguring the code provided in this answer, I worked up the following but it isn't working. Any suggestions on what I am doing wrong?

    tell application "Mail"
    set theMessage to selection
    tell application "Contacts"
        set theGroup to "_TEST"
    end tell
    set theSender to sender of theMessage
    tell application "Contacts"
        set theName to name of theSender
        set thePerson to make new person with properties {first name:name of theSender}
        add thePerson to theGroup
    end tell
    tell application "Contacts" to save
    end tell
share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

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).

share|improve this answer
    
that did the trick, thank you. The error log suggestion is helpful too, thanks. Your solution worked, but I get an error of "missing value" when I run it in the editor. –  George C Nov 13 '12 at 19:23
    
Yeah, that's not an error so much as just not having a return value. The Applescript event log prints the return value of the last statement. In this case, it's just save, which returns nothing, if saved successfully. Nothing to worry about, but it does look ominous if you don't know why it's there. –  robmathers Nov 13 '12 at 19:42
    
Thanks for the clarification, slowly trying to understand AS syntax. That helps! –  George C Nov 13 '12 at 20:53
    
No problem. AppleScript is a pretty nasty language IMO. The idea of an English-like scripting language sounds good on the surface, but you quickly realize it has serious problems. That said, it's still a useful way to get a lot of OS X-specific things accomplished. –  robmathers Nov 13 '12 at 20:55
    
I've discovered a limitation with this method: it creates a new entry every time. I have two groups, Pending and Current. I'd like to be able to add a new entry to Pending, but then later move that same contact to Current if/when their project goes forward. –  George C Dec 10 '12 at 19:22
add comment

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
share|improve this answer
    
Nothing happens when I run your script, besides Contacts getting launched. –  George C Feb 3 '13 at 22:29
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.