Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I’d like to be able to send to people from my address book where a dialogue would say who would you like to send the email to and I would write in like peter, john, George etc and that would populate my send to field and then I would write the email and send it to these people.

Kerry

share|improve this question
4  
Isn't this close to what creating a mail in Mail.app and typing the names into the To line does already? –  patrix May 10 '13 at 6:09
add comment

1 Answer

This should do what you asked for. It will ask you to choose from a list if the user name you enter matches more than one contact and also if the selected contact has more than one email address to choose from.

set recipientList to {}

tell application "Mail"
    activate
    repeat
        set userResponse to display dialog "who would you like to send the email to?" default answer "type address here" buttons {"Cancel", "Add another", "Done"}
        if button returned of userResponse is "Done" then exit repeat
        set the end of recipientList to my findContact(text returned of userResponse)
    end repeat
    set newMessage to make new outgoing message with properties {subject:"", content:"" & return & return}
    tell newMessage
        set visible to true
        repeat with i from 1 to length of recipientList
            make new to recipient at end of to recipients with properties {name:name of item i of recipientList, address:contactAddress of item i of recipientList}
        end repeat
    end tell
end tell

on findContact(userResponse)
    set contactChoiceNames to {}
    set pIDs to {}

    tell application "Contacts"
        set possibleContacts to every person whose name contains userResponse
        repeat with l from 1 to length of possibleContacts
            set the end of contactChoiceNames to name of item l of possibleContacts
            set the end of pIDs to id of item l of possibleContacts
        end repeat
    end tell

    activate --clarify which user you mean
    set contactChoice to item 1 of (choose from list (contactChoiceNames))

    tell application "Contacts"
        repeat with nameIndex from 1 to length of contactChoiceNames
            if contactChoice is item nameIndex of contactChoiceNames then
                set contactData to item nameIndex of possibleContacts
                exit repeat
            end if
        end repeat
        set thisContactData to {name:name of contactData, contactAddress:(value of email of contactData)}
    end tell

    if length of contactAddress of thisContactData > 1 then --clarify which address you want to use
        set addressChoice to item 1 of (choose from list (contactAddress of thisContactData))
    else
        set addressChoice to item 1 of contactAddress of thisContactData
    end if
    set contactAddress of thisContactData to addressChoice
    return thisContactData
end findContact
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.