Print Multiple Emails to PDF

From EggeWiki

I needed to print a large number of emails, with each going into their own pdf file. I could not simply select the emails and forward them. They needed to be printed individually. I modified a few scripts and came up with the following. I also put mail into 'classic' view, and did the search only on my Gmail All Mail folder.

<geshi lang="applescript"> global FlName set destinationFolder to "/Users/brianegge/Archive/2012/..." tell application "Mail" set theMsg to selection repeat with selectMsg in theMsg open selectMsg tell selectMsg --set background color to red --Show message processed set FlName to subject set check to count every word of FlName set messageDate to date received of selectMsg --display dialog check set FlName to (my date_format(messageDate)) & " " & my time_format(messageDate) & " " & (my FixFileName(FlName)) --strip bad characters end tell

set process_name to "Mail" activate application process_name tell application "System Events" tell process process_name --display dialog "proposed File Name" & return & FlName keystroke "p" using command down delay 2 set PDFref to sheet 1 of window 1 click menu button "PDF" of PDFref click menu item "Save as PDF…" of menu 1 of menu button "PDF" of PDFref delay 1.5 # keystroke "g" using {command down, shift down} # repeat until exists sheet 1 # delay 0.2 # end repeat # tell sheet 1 # set value of text field 1 to POSIX path of destinationFolder # click button "Go" #end tell #repeat while exists sheet 1 # delay 0.2 #end repeat keystroke FlName keystroke return -- save end tell end tell delay 5 close window 1 end repeat end tell

on FixFileName(str) --Deletes characters that cannot be used in file names set fixed_string to {} set bad_char to {":", "/"} repeat with c from 1 to (count every character in str) if bad_char contains (character c of str) then set end of fixed_string to "-" else set end of fixed_string to (character c of str) end if end repeat fixed_string as string end FixFileName

to date_format(old_date) -- Old_date is text, not a date. set {year:y, month:m, day:d} to old_date tell (y * 10000 + m * 100 + d) as string to text 1 thru 4 & "-" & text 5 thru 6 & "-" & text 7 thru 8 end date_format

to time_format(old_date) my FixFileName(time string of (old_date)) end time_format </geshi>

<geshi lang="bash"> tell application "Mail" close window 1 open message id 164149 of mailbox "[Gmail]/All Mail" of account "Gmail" --> missing value get subject of message id 164149 of mailbox "[Gmail]/All Mail" of account "Gmail" --> "Re: October invoice" get date received of message id 164149 of mailbox "[Gmail]/All Mail" of account "Gmail" --> date "Monday, November 7, 2011 9:11:54 AM" activate end tell tell application "System Events" keystroke "p" using command down get sheet 1 of window 1 of process "Mail" --> sheet 1 of window "Re: October invoice — All Mail" of application process "Mail" click menu button "PDF" of sheet 1 of window "Re: October invoice — All Mail" of application process "Mail" --> menu button "PDF" of sheet 1 of window "Re: October invoice — All Mail" of application process "Mail" click menu item "Save as PDF…" of menu 1 of menu button "PDF" of sheet 1 of window "Re: October invoice — All Mail" of application process "Mail" --> menu item "Save as PDF…" of menu "PDF" of menu button "PDF" of sheet 1 of window "Re: October invoice — All Mail" of application process "Mail" keystroke "2011-11-07 9-11-54 AM Re- October invoice" keystroke " " end tell tell application "Mail" close window 1 end tell </geshi>