如何确定通讯簿中是否存在与给定电子邮件地址的联系人?

时间:2011-07-23 19:48:09

标签: email applescript addressbook

我正在尝试创建一个“遍历”邮箱的脚本,检查通讯簿以查看电子邮件的发件人是否已经存在,并将联系人添加到通讯簿组(如果找到)。如果找不到电子邮件的发件人,则会在将新联系人添加到该组之前创建新联系人。

到目前为止,我已经为小组增加了部分:

on addPersonToGroup(person)
    tell application "Address Book"
        add person to group "wedding guests"
    end tell
    save addressbook
end addPersonToGroup

这用于循环选择的消息:

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

“with eachMessage ...”块中的注释部分是我还没想到的。

使用AppleScript在地址簿中搜索电子邮件地址的方法有哪些? Mac上是否有其他更适合此类任务的脚本语言?

2 个答案:

答案 0 :(得分:3)

下面是最终得到我想要的结果的脚本。感谢@ fireshadow52的帮助:

    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

答案 1 :(得分:2)

信不信由你,你在问题中写下了答案!

--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。 personAddress Book的保留字。将person更改为其他变量,它将起作用(参考addPersonToGroup子例程)。