【问题标题】:How can I determine whether or not a contact with a given e-mail address exists in the Address Book?如何确定地址簿中是否存在具有给定电子邮件地址的联系人?
【发布时间】:2011-07-23 19:48:08
【问题描述】:

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

到目前为止,我已经为组添加部分准备了这个:

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 ...”块中被注释掉的部分是我还没有弄清楚的。

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

【问题讨论】:

    标签: email applescript addressbook


    【解决方案1】:

    下面是最终让我得到我想要的结果的脚本。感谢@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
    

    【讨论】:

      【解决方案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 子例程)。

      【讨论】:

        猜你喜欢
        • 2017-07-23
        • 2014-11-16
        • 2022-09-29
        • 1970-01-01
        • 1970-01-01
        • 2012-08-15
        • 2011-11-30
        • 2011-04-17
        • 1970-01-01
        相关资源
        最近更新 更多