解析客户名称和电子邮件地址

时间:2015-12-29 06:49:45

标签: email applescript

我目前有一个脚本,它将在Airmail 2中创建一封新电子邮件,然后将此电子邮件发送到预定义的收件人姓名和电子邮件地址。我想更新脚本,以便它将解析传递给它的文件的名称(附加到电子邮件),并从文件名中提取客户端名称和客户端电子邮件地址。将传递给此脚本的文件的名称包含需要通过电子邮件发送附件的客户端的名称以及客户端的电子邮件地址(例如," FirstN LastN-2015-12- 28-client@example.com.pdf")

方便地,文件名的格式化方式是客户名称和电子邮件地址都被设置为(#分别在文件名的开头和结尾) - " - &#34 ;

以下是当前形式的脚本:

set theAttachment1 to (POSIX path of theFile)
set clientName to item 1 of inputAttributes
set clientEmail to item 1 of inputAttributes
tell application "Airmail 2"
    activate
    set theMessage to make new outgoing message with properties {subject:"New Invoice", content:"Please find attached, infra, the current month's invoice."}
    tell theMessage
        set sender to "billing@example.com"
        make new to recipient at end of to recipients with properties {name:"clientName", address:"clientEmail"}
        make new mail attachment with properties {filename:theAttachment1}
        sendmessage
    end tell
end tell

第二行和第三行是需要更新的行。我不确定在" - "之前如何精确地拉动钻头?并将clientName设置为该值以及如何在 second 之后拉出该位" - "并将clientEmail设置为该值。

1 个答案:

答案 0 :(得分:1)

解决方案是使用applescript文本项分隔符。删除文件扩展名后,脚本bellow将FileName解析为名称和地址:

set FileName to "FirstN LastN—2015-12-28—client@example.com.pdf"

-- remove the extension (last item when separated by ".")
set AppleScript's text item delimiters to "."
set Myitems to every text item of FileName
set last text item of Myitems to ""
set FileName to Myitems as text

-- parse the name with the "—"
set AppleScript's text item delimiters to "—"
set myName to first text item of FileName
set myAddress to last text item of FileName

变量myName将是“FirstN LastN”。

变量myAddress将是“client@exemple.com”。