Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
AppleScript + RSVP Emails = Wedding Guests Address Book Group
tell application "Mail"
set selectedMessages to selection
if (count of selectedMessages) is equal to 0 then
display alert "No Messages Selected" message "Select the messages you want to add to 'wedding guests' address book group before running this script."
else
set weddingGuests to {}
repeat with eachMessage in selectedMessages
set emailSender to (extract name from sender of eachMessage) as string
set emailAddress to (extract address from sender of eachMessage) as string
my addSender(emailSender, emailAddress)
end repeat
end if
end tell
on splitText(delimiter, someText)
set prevTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to delimiter
set output to text items of someText
set AppleScript's text item delimiters to prevTIDs
return output
end splitText
on addSender(theSender, theEmail)
tell application "Address Book"
set tmp to my splitText(" ", theSender)
set numOfItems to count tmp
set senderFirst to item 1 of tmp
if (numOfItems) is greater than 1 then
set senderLast to item 2 of tmp
else
set senderLast to ""
end if
try
set the check to get first person whose first name is senderFirst and last name is senderLast
--No error, sender exists
my addPersonToGroup(check)
on error --sender is not in Address Book yet; add sender and email to contacts and add the new contact to group
set newPerson to (make new person with properties {first name:senderFirst, last name:senderLast})
--Add Email
make new email at the end of emails of newPerson with properties {label:"Email:", value:theEmail}
--add the new person to the group
my addPersonToGroup(newPerson)
end try
end tell
end addSender
on addPersonToGroup(theSender)
tell application "Address Book"
add theSender to group "wedding guests"
save
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment