Title: Re: Importing Cced Contacts
On 8/12/02 11:17 AM, I wrote:

          So, it sounds as if you want to add not only CC recipients, but ALL recipients other than
          yourself, right? That makes much more sense - I thought you must have a number of weird
          correspondents that put everyone into the Cc. Why didn't you ask for that, or explain what
          you really wanted? i just did what you asked - you should be careful what you ask for, as
          they say, because you might get it. you got it. I planned to change the script to "All
          other recipients" before releasing it to ASC anyway. I'll change it to accommodate all
          other recipients.

Here it is. it gives you a confirmation and tally at the end now, and doesn't ask about contacts which already exist with the same email address when the sender has included just their address and no name at all. It also fixes a bug with the name of new and changed contacts (it didn't set the last name although it claimed to.)

-------------Add Recipients to Address Book -------------------------

-- Add Recipients to Address Book
-- Author: Paul Berkowitz <[EMAIL PROTECTED]>


tell application "Microsoft Entourage"
    
    try
       set theMsg to item 1 of (get current messages)
        if {class of theMsg} is not in {incoming message, outgoing message} then error number -128 -- might be new draft window in front
   on error
       if class of front window is draft window then
           save window 1
            set theMsg to displayed message of window 1
        else
           beep
           display dialog "This script only works if you first select a received or sent message, or have a new message in the front, or from a rule." buttons {"Cancel"} default button 1 with icon 0
            return
       end if
   end try
   
    set catNames to name of every category
   
    display dialog "Enter a name for a new category for these recipients, or use an existing category?" & return & return & "Enter new category name:" default answer "" buttons {"Cancel", "Existing Category", "New Category"} with icon 1
    if button returned of result = "Cancel" then
       return
   else if button returned of result = "New Category" then
       set catName to text returned of result
       if catName = "" then
           beep
           display dialog "You forgot to enter a new category name:" & return & return default answer "" with icon 2
            set catName to text returned of result
           if catName = "" then
               beep
               display dialog "Run the script again if you wish." & return & return buttons {"Cancel"} default button 1 with icon 0
                return
           end if
       end if
       if {catName} is in catNames then
           set theCat to category catName
       else
           set theCat to make new category with properties {name:catName}
            display dialog "You will have to set the color of your new category \"" & catName & "\" at the end of the script run." buttons {"OK"} default button 1 with icon 1
        end if
       
    else -- existing category
       set catName to item 1 of (choose from list catNames with prompt "Which category?")
        if catName = "false" then return -- canceled
       set theCat to category catName
   end if
   
    
    set theRecips to every recipient of theMsg
   set c to (count theRecips)
    if c = 1 then
       beep
       display dialog "There are no recipients of this message other than yourself." buttons {"Cancel"} default button 1 with icon 0
        return
   end if
   
    try
       set theAccount to account of theMsg
   on error -- happens sometimes
       set theAccount to the default mail account
   end try
   
    set myAddress to email address of theAccount
   
    set newContacts to 0
    
    repeat with i from 1 to c
       set theRecip to item i of theRecips
       repeat 1 times
           set {dName, eAddress} to theRecip's address's {display name, address}
            if eAddress = myAddress then exit repeat
           try
               set theContact to item 1 of (find eAddress)
                set conName to name of theContact
               set conCat to category of theContact
               if dNameconName then
                   
                    if dName = "" then exit repeat -- don't ask when contact has name and recipient doesn't                
                   display dialog "You already have a contact \"" & conName & "\" with the same email address as the recipient \"" & dName & "\"." & return & return & "Make a new contact, change the name of the contact, or leave as is?" buttons {"Don't Add", "Change Name", "Add Contact"} with icon 2
                    if button returned of result = "Don't Add" then
                       if conCat does not contain {theCat} then set category of theContact to ({theCat} & conCat)
                        exit repeat -- 1 times
                   else if button returned of result = "Change Name" then
                       set {fname, lName} to my MakeName(dName, eAddress)
                        set first name of theContact to fname
                       set last name of theContact to lName
                       if conCat does not contain {theCat} then set category of theContact to ({theCat} & conCat)
                        exit repeat
                   end if -- if new contact, keep going
                   
                else -- if same name and same address, contact exists already
                   if conCat does not contain {theCat} then set category of theContact to ({theCat} & conCat)
                    exit repeat -- go on to next recpient
               end if
               
            on error -- no existing contact with that email address
               
                try
                   set theContact to contact dName
                   set conCat to category of theContact
                   
                    set makeNewAddress to false
                   if (every email address of theContact) = {} then
                       set r to "with no email address."
                    else
                       set r to "but not this email address."
                    end if
                   display dialog "There is already a contact of the same name \"" & dName & "\"  " & r & return & return & "Add the email address to the existing contact, or make a new contact?" buttons {"Add Email Address", "Make New Contact"} with icon 2
                    if button returned of result = "Add Email Address" then set makeNewAddress to true
                   if makeNewAddress then
                       make new email address at theContact with properties {contents:eAddress}
                        if conCat does not contain {theCat} then set category of theContact to ({theCat} & conCat)
                        exit repeat -- 1 times                        
                   end if -- otherwise. keep going
               end try
           end try
           
            set {fname, lName} to my MakeName(dName, eAddress)
            
            make new contact with properties {first name:fname, last name:lName, email address:eAddress, category:{theCat}}
            set newContacts to newContacts + 1
            
        end repeat
   end repeat
   
    
    set q to "were already in your Address Book, but their category, email address and names are now all up to date."
    
    if newContacts = 0 then
       set p to "There were no new contacts to add: they " & q
       
    else if newContacts = 1 then
       set p to "One new contact was added."
        if c > 2 then set p to p & "  The other(s) " & q
       
    else
       set p to (newContacts as string) & " new contacts were added."
        if c > (newContacts + 1) then set p to p & "  The other(s) " & q
   end if
   
    beep
   display dialog p buttons {"OK"} default button 1 with icon 1
    
end tell


to MakeName(dName, eAddress)
    
    if dName = "" then return {"", eAddress} -- use email address as name if no display name
   
    set AppleScript's text item delimiters to {space}
    set lName to text item -1 of dName
   try
       set fname to (text items 1 thru -2 of dName) as string
   on error -- no -2, i.e. only one name    
       set fname to lName
       set lName to "" -- usually if only one name will be a first name
   end try
   set AppleScript's text item delimiters to {""}
    
    return {fname, lName}
    
end MakeName

--------------------end script ---------------------------------


--
Paul Berkowitz

Reply via email to