【问题标题】:Extracting sender and date sent from .eml file using applescript使用applescript从.eml文件中提取发件人和日期
【发布时间】:2015-05-16 04:10:38
【问题描述】:

我正在使用拖放功能将电子邮件从 Mail 拉到共享服务器上的相关工作子文件夹中。然后我使用脚本按创建日期排序(这是我拖过它们的日期),然后根据容器的名称重命名文件。

但我想使用脚本从每个 .eml 文件中提取发送日期、发件人和主题,然后使用该信息重命名文件。

之前有一篇帖子回答了类似的问题,但我不清楚(因为我是脚本新手)如何将它放入我的脚本中。 它可以在以下位置找到: "AppleScript - 获取 .eml 文件的信息"

答案是:

set fromField to text 7 thru -1 of (do shell script "cat /test.eml | grep From:")
set dateField to text 7 thru -1 of (do shell script "cat test.eml | grep Date:")
set toField to text 5 thru -1 of (do shell script "cat /test.eml | grep To:")
set subjectField to text 10 thru -1 of (do shell script "cat /test.eml | grep Subject:")

我想我需要有人来编写实际的 shell 脚本并告诉我放置它的位置。

【问题讨论】:

    标签: email applescript


    【解决方案1】:

    试试:

        set delim to " - "
    
    set myFiles to (choose file with multiple selections allowed)
    set TID to text item delimiters
    
    repeat with aFile in myFiles
        set pFile to quoted form of (POSIX path of aFile)
        set {mDate, mFrom, mSubject} to every paragraph of (do shell script "cat " & pFile & " | grep -e Date -e From: -e Subject:")
    
        set mDate to trim(mDate, 7)
        set AppleScript's text item delimiters to " "
        set myDate to text item 3 of mDate & space & text item 2 of mDate & ", " & text item 4 of mDate
        set AppleScript's text item delimiters to {""}
        set myDate to (date myDate)
        set myDate to day of myDate & (month of myDate as integer) & year of myDate as text
    
        set mFrom to trim(mFrom, 7)
        set mSubject to trim(mSubject, 10)
    
        set newName to myDate & delim & mFrom & delim & mSubject
    
        -- You will have to edit newName so it meets file naming standards
        --tell application "System Events" to set aFile's name to mSubject
    end repeat
    set text item delimiters to TID
    
    on trim(myText, firstChar)
        try
            set myText to text firstChar thru -1 of myText
        on error
            set myText to "None"
        end try
    end trim
    

    【讨论】:

    • 首先感谢您的帮助。我尝试了脚本,它正在获取信息。但是,当我添加一行将 aFile 重命名为 newName 时,它​​出现了堆栈溢出错误。还有没有办法将日期转换为 ddmmyyyy 格式?
    • 查看日期格式的编辑。您需要操作 newName 使其符合文件命名标准以避免错误。
    • 我收到 Finder 错误:无法在第一次修剪时继续修剪 (mDate,7)。如果我删除所有修剪,它将重命名文件(大量文本)但主题丢失。非常感谢您的所有帮助
    【解决方案2】:

    我会使用正则表达式,这将使您的脚本少于 10 行并且更易于阅读。

    Granted AppleScript 本身不支持正则表达式,您只需在http://www.satimage.fr/software/en/downloads/downloads_companion_osaxen.html 安装 Satimage Osax 扩展

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-07
      • 2015-12-28
      • 2020-03-20
      • 1970-01-01
      • 1970-01-01
      • 2014-07-22
      相关资源
      最近更新 更多