Applescript:使用用户输入从地址簿中获取联系电子邮件并添加到邮件消息

时间:2012-11-19 14:09:09

标签: applescript apple-mail

我想知道我可以用什么脚本将联系人的电子邮件地址添加到Mac Mail中?

这是我到目前为止,它要求各种输入,然后使用它们来编译消息。消息的其余代码工作正常,但我希望脚本在联系人中查找此人(基于输入),并将其中列出的邮件地址添加到To字段中的消息:

--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

这会创建消息并将“ClientName + LastName”添加到To字段,但我仍然需要进入并找到邮件地址。我希望地址簿通过名字和姓氏查找此人,然后在“收件人”字段中添加他的地址。

1 个答案:

答案 0 :(得分:2)

这很快失控了:

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