You may try to run :
(*
https://macscripter.net/viewtopic.php?id=43790
2019/01/27
*)
my Germaine() # With this added step the script will not save globals in its file.
on Germaine()
# =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
set mySelf to {"Yvan KOENIG", "KOENIG Yvan"} # I USE BOTH FORMATS, EDIT TO FIT YOUR NEEDS
# =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
# Define the folder where files will be stored
set attachmentsFolder to (choose folder) as text
tell application "Mail"
activate
set theMessages to (get selection)
end tell
---------------------------------------
repeat with eachMessage in theMessages
# Open a mail and treat it
tell application "Mail"
open eachMessage
delay 0.5
set wName to name of window 1
tell eachMessage # extract datas from the selected message
set sender_Name to extract name from its sender
set sender_Mail to extract address from its sender
set mail_Subject to subject
set mail_Date to date received # will keep the time component
set target_name to (name of its to recipient)
end tell # the message
end tell # application Mail
# No longer speak to application Mail
tell mail_Date
set theYear to year
set theMonth to its month as number
set theDay to day
set theHour to its hours
set theMinutes to its minutes
set theSeconds to its seconds
end tell
set begName to (theYear as text) & "-" & text -2 thru -1 of ((100 + theMonth) as text) & "-" & text -2 thru -1 of ((100 + theDay) as text) & space & text -2 thru -1 of ((100 + theHour) as text) & "." & text -2 thru -1 of ((100 + theMinutes) as text) & "." & text -2 thru -1 of ((100 + theSeconds) as text) -- & " Email "
if sender_Name is in mySelf then
set begName to begName & " to " & target_name
else
set begName to begName & " from " & sender_Name
end if
set thePDFname to begName & " - " & my remplace(mail_Subject, {":", "/"}, "-") & ".PDF" # I know, I'm a bit paranoid
set posixFolderPath to POSIX path of attachmentsFolder
set pdfPath to POSIX path of (posixFolderPath & thePDFname)
-- Print the opened email as PDF
tell application "System Events"
tell process "Mail"
set frontmost to true
set windowName to name of window 1
keystroke "p" using {command down} # Issue the Print shortcut
tell window wName
repeat
try
if (exists pop up button 1 of sheet 1) then exit repeat
end try
delay 0.2
end repeat
tell sheet 1
set PDFButton to first menu button
click PDFButton
tell menu 1 of PDFButton
name of menu items
--> {"Ouvrir le PDF dans Aperçu", "Enregistrer au format PDFâŠ", "Enregistrer au format PostScriptâŠ", "Faxer le document PDFâŠ", missing value, "@ PDF-BAT.qfilter", "@ PDF-prĂ©presse CMJN.qfilter", "@ PDF-web.qfilter", "@ PDFX3-ISO.qfilter", "Add PDF to iTunes", "Envoyer le document PDF par courrier Ă©lectronique", "Enregistrer le document PDF dans le dossier de reçus web", missing value, "Modifier le menuâŠ"}
click menu item 2
end tell
repeat
if exists sheet 1 then exit repeat # the Print sheet exists, exit the loop!
delay 0.1
end repeat
end tell # sheet 1
tell sheet 1 of sheet 1
set wichElements to class of UI elements
if wichElements contains combo box then # maybe Mojave
get position of combo boxes --> {{910, 118}, {910, 148}}
set value of combo box 1 to thePDFname
else -- Yosemite, El Capitan, Sierra, High Sierra
--> {static text, text field, UI element, static text, text field, group, radio group, group, pop up button, text field, splitter group, text field, static text, button, text field, static text, text field, static text, static text, text field, button, button, button}
get position of text fields --> {{1262, 194}, {1262, 224}, {1458, 263}, {1086, 646}, {1086, 616}, {1086, 552}, {1086, 584}}
set value of text field 1 to thePDFname
end if
end tell # sheet 1 of sheet 1
keystroke "g" using {command down, shift down}
repeat until exists sheet 1 of sheet 1 of sheet 1
delay 0.02
end repeat
tell sheet 1 of sheet 1 of sheet 1
--name of UI elements
set wichElements to class of UI elements # According to system in use, may be
--> {static text, combo box, button, button} -- El Capitan, Sierra, High Sierra, maybe Mojave
--> {static text, text field, button, button} -- ⊠Yosemite
if wichElements contains combo box then
set SaveIndex to -1
set value of combo box 1 to posixFolderPath
else
set SaveIndex to 1
set value of text item 1 to posixFolderPath
end if
name of buttons --> {"Aller", "Annuler"}
click button 1
end tell # sheet 1 of sheet 1 of sheet 1
tell sheet 1 of sheet 1
--name of UI elements # According to system in use, may be
--> {"Enregistrer", "Nouveau dossier", "Annuler"}
--> {"Nouveau dossier", "Annuler", "Enregistrer"}
click button SaveIndex
end tell # sheet 1 of sheet 1
end tell -- window wName
end tell # process Mail
end tell # "System Events"
# Now speak again to application Mail
tell application "Mail"
try
# Do that only if the message has attacments
set num to 1
repeat with theAttachment in eachMessage's mail attachments
set fileName to begName & " Attachment - " & num & " - " & theAttachment's name
save theAttachment in file (attachmentsFolder & my remplace(fileName, {":", "/"}, "-")) # I know, I'm a bit paranoid
set num to num + 1
end repeat
end try
try
repeat while exists window wName
close window wName
delay 0.1
end repeat
end try
end tell # application Mail
end repeat # with eachMessageâŠ
end Germaine
#=====
(*
replace every occurences of d1 by d2 in text t
*)
on remplace(t, d1, d2)
local oTIDs, l
set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d1}
set l to text items of t
set AppleScript's text item delimiters to d2
set t to l as text
set AppleScript's text item delimiters to oTIDs
return t
end remplace
#=====
I tested it only under High Sierra 10.13.6
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 27 janvier 2019 15:41:52