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
Best Answer
Try this, it will create a contact with proper first name, last name and email address:
There were a few issues with your original attempt, here's how I worked around them.
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.sender
gives you a not very useful string with the name and email combined.extract name from
andextract address from
give you useful strings.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).