6 votes

Création d'une fonction "enregistrer sous" pour le fichier .eml dans AppleScript

Je suis novice en AppleScript et j'essaie de comprendre comment enregistrer un message Mail.app en tant que message .eml. Idéalement, j'aimerais que l'action soit similaire à celle de la barre de menu de Mail, c'est-à-dire qu'elle enregistre le message et les pièces jointes ensemble.

Le flux de travail est le suivant : vous avez une sélection dans Mail, vous appuyez sur une touche de raccourci et la fonction crée un nom de fichier ( newFile ) pour que l'e-mail soit enregistré. J'ai juste besoin d'aide pour savoir comment sauvegarder le message dans le chemin ( theFolder ) au format .eml.

tell application "Mail"
set msgs to selection

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 theFolder to choose folder with prompt "Save Exported Messages to..." without invisibles

        repeat with msg in msgs

            -- determine date received of msg and put into YYYYMMDD format
            set msgDate to date received of msg
            -- parse date SEMversion below using proc pad2()
            set {year:y, month:m, day:d, hours:h, minutes:min} to (msgDate)
            set msgDate to ("" & y & my pad2(m as integer) & my pad2(d))

            -- assign subject of msg
            set msgSubject to (subject of msg)

            -- create filename.eml to be use as title saved
            set newFile to (msgDate & "_" & msgSubject & ".eml") as Unicode text

            -- copy mail message to the folder and prepend date-time to file name

            -- THIS IS WEHRE I AM COMPLETE LOST HOW SAVE THE EMAIL into theFolder           
        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 pad2(n)
return text -2 thru -1 of ("00" & n)
end pad2

2voto

Nana Benyin Points 21

Vous pouvez copier le fichier .emlx depuis votre dossier Mail.

tell application "Mail"
    set msgs to selection

    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 theFolder to POSIX path of (choose folder with prompt "Save Exported Messages to..." without invisibles)

            repeat with msg in msgs

                -- determine date received of msg and put into YYYYMMDD format
                set msgDate to date received of msg
                -- parse date SEMversion below using proc pad2()
                set {year:y, month:m, day:d, hours:h, minutes:min} to (msgDate)
                set msgDate to ("" & y & my pad2(m as integer) & my pad2(d))

                -- assign subject of msg
                set msgSubject to (subject of msg)

                -- create filename.eml to be use as title saved
                set newFile to (msgDate & "_" & msgSubject & ".eml") as text
                set newFilePath to theFolder & newFile as text
                set newFilePath2 to theFolder & newFile & "x" as text

                -- copy mail message to the folder and prepend date-time to file name
                set messageId to id of msg
                set myFolder to POSIX path of (account directory of account of mailbox of msg as text)
                do shell script "find " & quoted form of myFolder & " \\( -name \"" & messageId & ".eml\" -a -exec cp -a {} " & quoted form of newFilePath & " \\; \\) -o \\( -name \"" & messageId & ".emlx\" -a -exec cp -a {} " & quoted form of newFilePath2 & " \\; \\)"

            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 pad2(n)
    return text -2 thru -1 of ("00" & n)
end pad2

0voto

Il suffit d'exporter la source brute du message vers un nouveau fichier :

tell application "Mail"
    set msgs to selection

    if length of msgs is not 0 then

        set theFolder to (system attribute "HOME") & "/Downloads/"

        repeat with msg in msgs
            set msgContent to source of msg
            -- determine date received of msg and put into YYYY-MM-DD format
            set msgDate to date received of msg
            -- parse date SEMversion below using proc pad2()
            set {year:y, month:m, day:d, hours:h, minutes:min} to (msgDate)
            set msgDate to ("" & y & "-" & my pad2(m as integer) & "-" & my pad2(d))

            -- assign subject of msg
            set msgSubject to (subject of msg)

            -- create filename.eml to be use as title saved
            set newFile to (msgDate & " " & msgSubject & ".eml") as rich text
            set newFilePath to theFolder & newFile as rich text

            set referenceNumber to open for access newFilePath with write permission
            try
                write msgContent to referenceNumber
                delete msg
            on error
                close access referenceNumber
            end try
            close access referenceNumber

        end repeat

    end if -- msgs > 0
end tell

on pad2(n)
    return text -2 thru -1 of ("00" & n)
end pad2

SistemesEz.com

SystemesEZ est une communauté de sysadmins où vous pouvez résoudre vos problèmes et vos doutes. Vous pouvez consulter les questions des autres sysadmins, poser vos propres questions ou résoudre celles des autres.

Powered by:

X