问题
I'm attempting to create a script that "walks" through a mailbox, checks the Address Book to see if the e-mail's sender is already there, and adds the contact to an Address Book group if found. If the e-mail's sender isn't found, a new contact would be created before it was added to the group.
So far, I've got this for the group adding part:
on addPersonToGroup(person)
tell application "Address Book"
add person to group "wedding guests"
end tell
save addressbook
end addPersonToGroup
and this for looping through selected messages:
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 emailAddress to extract address from sender of eachMessage
--if emailAddress is found in Address Book then
-- get the person from the Address Book
--else
-- create new person with emailAddress
--end if
--addPersonToGroup person
end repeat
end if
end tell
The commented-out part inside the "repeat with eachMessage ..." block is what I haven't figured out yet.
What ways are available for searching the Address Book for an e-mail address using AppleScript? Are there alternative scripting languages on the Mac that would be more suitable for such a task?
回答1:
Below is the script that ultimately got me the result I wanted. Thanks to @fireshadow52 for the assist:
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
end tell
end addPersonToGroup
回答2:
Believe it or not you kind of wrote the answer in the question!
--In your "looping through selected messages" script, add this line...
set emailSender to (get sender of eachMessage) as string
--...before this one...
set emailAddress to (extract address from sender of eachMessage) as string
--This will help you later
--You should add this subroutine at the bottom of your script to check whether the contact exists or not; invoke it by doing 'my addSender(emailSender, emailAddress)'
on addSender(theSender, theEmail)
tell application "Address Book"
set the check to (get first person whose first name is theSender) as list
if check is {} --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:theSender, last name:null}) --Sorry if you want the last name too
--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)
else --sender is in Address Book, add sender to group
my addPersonToGroup(check)
end if
end tell
end addSender
As always, if you need help, just ask. :)
P.S. If you get an error in the subroutine, it is most likely the if
block's fault. To fix the error (and to keep the script running), just change the if
block to a try
block, as shown here:
try
set check to (get first person whose first name is theSender) as list
--No error, sender exists
my addPersonToGroup(check)
on error --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:theSender, last name:null}) --Sorry if you want the last name too
--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
P.P.S. person
is a reserved word for Address Book
. Change person
to some other variable and it will work (referring to the addPersonToGroup
subroutine).
来源:https://stackoverflow.com/questions/6802895/how-can-i-determine-whether-or-not-a-contact-with-a-given-e-mail-address-exists