You may try to run :
(*
# Define the folder where files will be stored
set storageName to "Attachments"
set p2d to path to documents folder from user domain
set attachmentsFolder to (p2d as text) & storageName & ":"
do shell script "/bin/mkdir -p " & quoted form of (POSIX path of attachmentsFolder)
*)
# =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
# OF COURSE YOU MAY USE: set attachmentsFolder to ( choose folder) as text
set attachmentsFolder to (choose folder) as text
# =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
# =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
set mySelf to {"Kurt YourLastName", "YourLastName Kurt"} # I USE BOTH FORMATS, EDIT TO FIT YOUR NEEDS
# =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
tell application "Mail"
activate
set theMessages to (get selection)
end tell
---------------------------------------
repeat with eachMessage in theMessages
--Open an mail and treat it
tell application "Mail"
open eachMessage
delay 0.5
set wName to name of window 1
tell eachMessage # extracts 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 # Mail
# No longer speak to 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 supprime(mail_Subject, {":", "/"}) & ".PDF"
set pdfPath to (path to desktop as text) & thePDFname
-- Print the opened email
tell application "System Events"
tell process "Mail"
set frontmost to true
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
--name of menu buttons of sheet 1 of window wName
--> {"PDF"} # but I don't know if it's spelled this way worldwide
tell sheet 1
set PDFButton to first menu button
click PDFButton
-- name of menu items of menu 1 of PDFButton
--> {"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 of menu 1 of PDFButton
repeat
if exists sheet 1 then exit repeat # the Print sheet exists, exit the loop!
delay 0.1
end repeat
tell sheet 1
keystroke "d" using {command down} # Set the Desktop as destination folder
set value of text field 1 to thePDFname # Set the name of the new PDF
keystroke return
end tell # sheet 1 (the Save As one)
end tell # sheet 1 of window wName
end tell
end tell # process "Mail"
repeat
if exists disk item pdfPath then exit repeat # Now the pdf is really available on the Desktop
delay 0.1
end repeat
end tell # System Events
# We may move the PDF to its final location
set pdfPath to quoted form of POSIX path of pdfPath
do shell script "mv " & pdfPath & space & quoted form of (POSIX path of attachmentsFolder)
# Now speak again to Mail
tell application "Mail"
try
# Here 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 supprime(fileName, {":", "/"})) # I know, I'm a bit paranoid
set num to num + 1
end repeat
end try
close window wName
end tell
end repeat # with eachMessage.
#=====
(*
removes every occurences of d in text t
*)
on supprime(t, d)
local oTIDs, l
set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
set l to text items of t
set AppleScript's text item delimiters to ""
set t to l as text
set AppleScript's text item delimiters to oTIDs
return t
end supprime
#=====
I forgot that I also wrote :
Don’t forget to insert your true name(s) in the instruction defining myself.
Yvan KOENIG (VALLAURIS, France) lundi 30 mars 2015 15:03:45