2011-07-23 12 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ブロックの障害です。ここに示すように、エラーを修正するには(とスクリプトの実行を維持するために)、ちょうどtryブロックにifブロックを変更します。

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サブルーチンを参照)。

関連する問題