将Mail.app中选择的邮件导出为emlx

时间:2013-07-01 10:05:43

标签: applescript

我想将选定邮箱中的选定电子邮件导出到Finder。我在互联网上找到了这个脚本并将其改为我的喜好。但它似乎并没有出口。有人知道哪里出错了吗?

tell application "Mail"
    set msgs to message of mailbox "test1" of account "Info"

    if length of msgs is not 0 then
        display dialog "Export selected message(s)?"
        if the button returned of the result is "OK" then

            -- set up month parsing value for French Vanilla algorithm
            set fixedDate to current date --or any other date
            set month of fixedDate to January


            -- set theFolder to "Macintosh HD:Users:rajsingh:Desktop:" as alias
            set theFolder to choose folder with prompt "Save Exported Messages to..." without invisibles

            repeat with msg in msgs
                -- get path to message
                set mb to mailbox of msg
                set mba to account of mb
                -- mtype is returning 'constant **** ETIM' when it should be imap (for OGC account)
                -- set mtype to (account type) of mba
                set mtype to "imap"
                set accountpath to account directory of account of mb
                set fullpath to accountpath & name of mb & "." & mtype & "mbox:Messages:"

                -- figure out message name
                set msgfilename to id of msg
                set atts to number of mail attachments of msg
                if atts > 0 then
                    set msgfilename to msgfilename & ".partial.emlx"
                else
                    set msgfilename to msgfilename & ".emlx"
                end if
                set fullpath to fullpath & msgfilename

                set theFile to fullpath as rich text

                -- create new name prefix based on date
                set msgDate to date received of msg

                -- parse date
                -- use French Vanilla algorithm to get month number
                set theMonth to (2 + (msgDate - fixedDate + 1314864) div 2629728) as rich text
                if length of theMonth < 2 then
                    set theMonth to "0" & theMonth
                end if

                set theDay to day of msgDate as rich text
                if length of theDay < 2 then
                    set theDay to "0" & theDay
                end if

                set msgDate to (year of msgDate as rich text) & theMonth & theDay & "at" & hours of msgDate & minutes of msgDate & seconds of msgDate

                set comparison_string to ":/"
                set replacement_string to "->"
                set msgSubject to ""

                set msgSubject to my replaceText("Re- ", "", msgSubject)
                set msgSubject to my replaceText("Re-", "", msgSubject)
                --set msgSubject to text 1 thru 49 of msgSubject

                set newFile to msgSubject & "_" & msgDate & ".emlx" as rich text
                --set newFile to (msgSubject & "_" & msgDate & "_" & msgfilename) as text

                -- copy mail message to the folder and prepend date-time to file name
                tell application "Finder"
                    try
                        set intFile to ""
                        duplicate file theFile to folder theFolder

                    on error
                        display dialog theFile & theFolder
                        display dialog "couldn't duplicate " & intFile
                    end try
                    -- rename file
                    try
                        set name of intFile to newFile
                    on error
                        display dialog "couldn't set name of " & intFile & " to " & newFile
                    end try
                    -- reveal intFile
                    -- open theFile as alias
                end tell

            end repeat

            beep 2
            display dialog "Done exporting " & length of msgs & " messages."
        end if -- OK to export msgs
    end if -- msgs > 0
end tell

on replaceText(find, replace, subject)
    set prevTIDs to text item delimiters of AppleScript
    set text item delimiters of AppleScript to find
    set subject to text items of subject

    set text item delimiters of AppleScript to replace
    set subject to "" & subject
    set text item delimiters of AppleScript to prevTIDs

    return subject
end replaceText

2 个答案:

答案 0 :(得分:0)

10.8中的消息路径如下所示:

~/Library/Mail/V2/IMAP-example@imap.gmail.com/[Gmail].mbox/Trash.mbox/C523787F-3D53-4A6E-BA48-516ACC5B68DD/Data/2/Messages/2496.emlx

您可以按ID搜索邮件:

tell application "Mail"
    selected messages of message viewer 1
    repeat with m in result
        do shell script "find ~/Library/Mail/V2 -name " & (get id of m) & ".\\* -exec cp {} ~/Desktop \\;"
    end repeat
end tell
tell application "Mail"
    selected messages of message viewer 1
    repeat with m in result
        do shell script "find ~/Library/Mail/V2 -name " & (get id of m) & ".\\* -exec cp {} ~/Desktop/" & quoted form of my replace(subject of m, "/", "") & ".emlx \\;"
    end repeat
end tell

on replace(input, x, y)
    set text item delimiters to x
    set t to text items of input
    set text item delimiters to y
    t as text
end replace

答案 1 :(得分:0)

你的下面的脚本工作正常,我想把它包含在规则中,但我是新手 脚本。

感谢您的回答

                __________________________________

告诉申请“邮件”
    选择的消息查看器消息1     结果中用m重复
        做shell脚本“find~ / Library / Mail / V2 -name”&amp; (得到m的id)&amp; “。\ * -exec cp {}〜/ Desktop /”&amp;引用的替换形式(m,“/”,“”的主题)&amp; “.emlx \;”
    结束重复
结束告诉

关于替换(输入,x,y)
    将文本项分隔符设置为x
    将t设置为输入的文本项
    将文本项分隔符设置为y
    作为文本
结束替换

                       ________________________________