使用applescript提取从.eml文件发送的发件人和日期

时间:2012-12-19 07:07:49

标签: email applescript

我正在使用拖放功能将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脚本并告诉我在哪里放置它。

2 个答案:

答案 0 :(得分:0)

尝试:

    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

答案 1 :(得分:0)

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

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