Print multiple email messages from Apple Mail to individual PDFs

applescriptautomatormail.app

My goal is to be able to highlight a number of messages in Apple Mail then run a process to print them each to a PDF. Right now I can select multiple emails command + P Then select PDF -> Save as PDF but that saves all the highlighted emails as an single PDF file. I'm looking to get an individual PDF per highlighted email (with the subjst as the PDF file name if possible).

I looked at Automator's Print Plugin but couldn't find a way to create separate documents (can't say I'm any sort of Automator pro so I may have overlooked something obvious)

Is there some way for me to accomplish this using Automator or applescript?

Best Answer

This following AppleScript code will save single or multiple selected email messages as individual PDF files. Each PDF file will be named as the subject of the selected email message.

This solution works for me using the latest version of macOS Catalina.

tell application "Mail" to activate

tell application "System Events" to tell application process "Mail"
    repeat until frontmost
        delay 0.1
    end repeat
    tell menu bar 1 to tell menu bar item "File" to tell menu "File"
        tell menu item "Export as PDF…" to perform action "AXPress"
        delay 1 -- Value May Need To Be Adjusted
    end tell
    repeat until sheet 1 of window 1 exists
        delay 0.1
    end repeat
    if UI element "Choose" of sheet 1 of window 1 exists then
        -- When Multiple Email Messages Are Selected
        click UI element "Choose" of sheet 1 of window 1
    else
        -- When Only One Email Message Is Selected
        click UI element "Save" of sheet 1 of window 1
    end if
end tell