Верьте или нет, вы вроде как написали ответ на вопрос!
--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
Как всегда, если вам нужна помощь, просто спросите. :)
P.S. Если вы получили ошибку в подпрограмме, скорее всего, это ошибка блока if
. Чтобы исправить ошибку (и сохранить выполнение скрипта), просто измените блок if
на блок try
, как показано здесь:
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
- зарезервированное слово для Address Book
. Измените person
на другую переменную, и она будет работать (имеется в виду подпрограмма addPersonToGroup
).