2011-07-23 35 views
1

我正試圖創建一個腳本,通過郵箱「散步」,檢查通訊簿以查看電子郵件的發件人是否已經存在,並將聯繫人添加到通訊簿組(如果找到)。如果未找到電子郵件的發件人,則會在添加到該組之前創建新的聯繫人。如何確定地址簿中是否存在與給定電子郵件地址的聯繫?

到目前爲止,我有這爲集團增加部分:

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 

裏面的「有重複註釋掉的部分eachMessage ...「塊是我還沒有想出來的。

有什麼方法可用於使用AppleScript在地址簿中搜索電子郵件地址?在Mac上是否有其他腳本語言更適合這樣的任務?

回答

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 
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子例程)。

相關問題