...
on GUIprintToPDF(tempFileName, OSMtLion)
tell application "System Events"
tell process "Mail"
set frontmost to true
-------------------------
keystroke "p" using {command down} -- opens Print dialog
repeat 20 times -- wait for print sheet to appear
delay 1
set frontmost to true
if exists sheet 1 of window 1 then exit repeat
end repeat
if not (exists sheet 1 of window 1) then error "Mail is not responding. The Print dialog failed to appear."
set frontmost to true
-------------------------
set PDFButton to (first menu button of sheet 1 of window 1)
click PDFButton
click menu item 2 of menu 1 of PDFButton -- Save as PDF...
delay 1 -- wait for the Print window
-------------------------
if OSMtLion then
try
set value of text field 1 of sheet 1 of sheet 1 of window 1 to tempFileName --** for Mt Lion only
on error
error "The pdf document was not created. (in GUIprintToPDF)"
end try
else
set value of text field 1 of window 1 to tempFileName --** for Lion and previous
end if
-------------------------
set frontmost to true
keystroke "d" using {command down} -- sets save location to Desktop
keystroke return -- click button "Save"
repeat
delay 1
if not (exists sheet 1 of window 1) then exit repeat
end repeat
end tell
end tell
end GUIprintToPDF ------------------------------------------