"无法获得别名的最后一个文本项目"运行AppleScript时出错

时间:2015-12-29 18:59:02

标签: applescript

我有一个AppleScript作为监控文件夹的Hazel例程的一部分运行。当脚本运行时,它会挑选Hazel例程所针对的文件,然后将该文件附加到电子邮件中,并使用文件名称中的信息来处理电子邮件。不幸的是,似乎脚本中的某处出现了错误,但我似乎无法找到它。

来自Console的唯一半有用信息在标题中(即"无法获取别名&#34的最后一个文本项;)。这是脚本:

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

代码被评论,因此应该明白每个部分应该要做什么。我想这个问题出在"从FileName"删除路径。部分,因为那是一直给我带来最多麻烦的部分。

1 个答案:

答案 0 :(得分:1)

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