Search code examples
applescriptapple-mail

Applescript: Use user inputs to get contact email from Address Book and add to Mail message


I would like to know what script I can use to add an email address of a person in Contacts to Mac Mail?

This is what I have thus far, it asks for various inputs and then uses these to compile a message. The rest of the code for the message works fine, but I would like the script to look for this person (based on the inputs), in Contacts and add the mail address listed there to the message in the To field:

--User Inputs to get Client details
display dialog "You are about to Create Outgoing e-mail for: Test

Please enter Client's First Name:" default answer "First Name"
set ClientName to text returned of result
display dialog "Please enter Client's Last Name:" default answer "Last Name"
set ClientLastName to text returned of result

--Create e-mail
tell application "Mail"
set theMessage to make new outgoing message with properties {visible:true, subject:"Test", content:"Hi " & ClientName & ",
"}

tell theMessage
    make new to recipient at end of to recipients with properties {address:ClientName  & ClientLastName}
end tell
end tell

This creates the message and adds "ClientName + LastName" to To field, but I still have to go in and find the mail address. I would like Address Book to look up the person by First and Last Name and then add his address in the To field.


Solution

  • Well this got out of hand quickly:

    repeat
        display dialog "You are about to Create Outgoing e-mail for: Test
        Please enter Client's First Name:" default answer "First Name"
        set ClientName to text returned of result
        display dialog "Please enter Client's Last Name:" default answer "Last Name"
        set ClientLastName to text returned of result
        tell application "Address Book" to set myContact to every person whose first name is ClientName and last name is ClientLastName
        if myContact ≠ {} then
            exit repeat
        else
            display dialog "There is not match for " & ClientName & space & ClientLastName & " in your Address Book." buttons {"Try again", "Cancel"} default button "Try again"
        end if
    end repeat
    
    tell application "Address Book"
        if (count of myContact) > 1 then
            repeat with aPerson in myContact
                display dialog "More than one person in your Address Book is named " & ClientName & space & ClientLastName & ". Please select your contact..." & aPerson's vcard buttons {"Select", "Next", "Cancel"} default button "Select"
                if button returned of the result = "Select" then
                    set myContact to contents of aPerson
                    exit repeat
                end if
            end repeat
        else if (count of myContact) = 1 then
            set myContact to first item of myContact
        end if
    
        repeat
            set contactEmail to emails of myContact
            if contactEmail ≠ {} then
                exit repeat
            else
                display dialog "There is not an email listed for " & ClientName & space & ClientLastName buttons {"Cancel"} default button "Cancel"
            end if
        end repeat
    
        if (count of contactEmail) > 1 then
            set theEmails to {}
            repeat with anEmail in contactEmail
                set end of theEmails to label of anEmail & ": " & value of anEmail
            end repeat
            set contactEmail to first item of (choose from list theEmails with prompt "Please select an email address:")
            set AppleScript's text item delimiters to ": "
            set contactEmail to text item -1 of contactEmail
            set AppleScript's text item delimiters to {""}
        else if (count of contactEmail) = 1 then
            set contactEmail to value of first item of contactEmail
        end if
    end tell
    
    tell application "Mail"
        set theMessage to make new outgoing message with properties {visible:true, subject:"Test", content:"Hi " & ClientName & ",
    "}
    
        tell theMessage
            make new to recipient at end of to recipients with properties {address:contactEmail}
        end tell
    end tell