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

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

问题描述

我正在尝试创建一个遍历"邮箱的脚本,检查地址簿以查看电子邮件的发件人是否已经存在,如果找到,则将联系人添加到地址簿组中.如果未找到电子邮件的发件人,则会在将其添加到群组之前创建一个新联系人.

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

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天全站免登陆