如何确定通讯录中是否存在与给定电子邮件地址的联系人? [英] How can I determine whether or not a contact with a given e-mail address exists in the Address Book?

查看:89
本文介绍了如何确定通讯录中是否存在与给定电子邮件地址的联系人?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

,用于遍历所选消息:

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

我还没有弄清楚"repeat with eachMessage ..."块中被注释掉的部分.

The commented-out part inside the "repeat with eachMessage ..." block is what I haven't figured out yet.

使用AppleScript可以在通讯簿中搜索电子邮件地址的哪些方法?Mac上是否存在更适合此类任务的替代脚本语言?

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?

推荐答案

下面是最终使我获得所需结果的脚本.感谢@ fireshadow52的协助:

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

这篇关于如何确定通讯录中是否存在与给定电子邮件地址的联系人?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆