"Can't get last text item of alias" 运行 AppleScript 时出错

"Can't get last text item of alias" Error When Running AppleScript

我有一个 AppleScript,它作为监视文件夹的 Hazel 例程的一部分运行。当脚本运行时,它会分离出 Hazel 例程所针对的文件,然后将文件附加到电子邮件中,并使用文件名中的信息对电子邮件进行寻址。不幸的是,脚本中似乎某处有错误,但我似乎找不到它。

来自控制台的唯一 semi-useful 信息在标题中(即 "Can't get last text item of alias")。这是脚本:

on hazelProcessFile(theFile)

    set theAttachment1 to (POSIX path of theFile)
    set FileName to theFile

    --remove trailing slash
    set SansSlash to quoted form of text 1 through -2 of FileName
    set FileName to SansSlash as text

    -- remove path from FileName
    set AppleScript's text item delimiters to ":"
    set SansPath to last text item of FileName
    set FileName to SansPath as text

    -- remove extension
    set AppleScript's text item delimiters to "."
    set SansExtension to every text item of FileName
    set last text item of SansExtension to ""
    set FileName to SansExtension as text

    -- parse using —
    set AppleScript's text item delimiters to "—"
    set clientName to first text item of FileName
    set clientEmail to last text item of FileName

    tell application "Airmail 2"
        activate
        set theMessage to make new outgoing message with properties {subject:"New Invoice from ", content:"Please find attached, infra, the current month's invoice.    If you have any questions, please feel free to respond to this email.  One-time payments may be made using the following secure form on our website: https://.  Thank you for your continued business."}
        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}
            compose
        end tell
    end tell
end hazelProcessFile

代码已注释,因此每个部分应该做什么应该很明显。我想问题出在 "remove path from FileName" 部分,因为这是给我带来最多麻烦的部分。

theFile 显然是一个 alias 说明符。
text itemtext thru——顾名思义——需要纯文本

您必须先将 alias 强制转换为 text,然后再处理文本并删除 quoted form of,这仅在与 do shell script 结合使用时才需要。

--remove trailing slash
  set FileName to theFile as text
  set SansSlash to text 1 through -2 of FileName
  set FileName to SansSlash

但是 HFS 路径中没有尾部斜杠

alias 中删除不带扩展名的文件名要容易得多

tell application "System Events" to set {name:Nm, name extension:Ex} to theFile
set baseName to text 1 thru ((get offset of "." & Ex in Nm) - 1) of Nm

编辑:

试试这个优化的代码,Airmail 2 部分被跳过,我不知道 EM Dash 字符是否被正确处理,以防您复制并粘贴代码。

on hazelProcessFile(theFile)

    set theAttachment1 to (POSIX path of theFile)
    tell application "System Events" to set {name:Nm, name extension:Ex} to theFile
    set FileName to text 1 thru ((get offset of "." & Ex in Nm) - 1) of Nm

    -- parse using —
    set {TID, text item delimiters} to {text item delimiters, "—"}
    set clientName to text item -2 of FileName
    set clientEmail to text item -1 of FileName
    set text item delimiters to TID

    -- tell application "Airmail 2"
    -- ...

end hazelProcessFile