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

Parse File Name for Client Name and Email Address

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

方便的是,文件名的格式设置为客户端名称和电子邮件地址都由“—”分隔(分别在文件名的开头和结尾)。

这是当前形式的脚本:

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 设置为该值.

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

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