0

I want to export selected emails from a given mailbox to Finder. I found this script on the internet and altered it to my likes. But it doen't seems to export. Does somebody know where it is going wrong?

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
4

2 回答 2

0

your following script works fine, I want to include it in Rules but I'm Total newbie to the scripting.

Thanks for answer

                __________________________________

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

                       ________________________________
于 2014-01-29T14:41:01.177 回答
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
于 2013-07-01T11:20:58.343 回答