Extract Date and Time from Apple Mail

I want to print email messages to pdf and name the files based on the date and time the messages were received or sent.

My final goal is to have the file name look like 2022-01-25 1400.pdf (1400 = 2:00 PM)

I am uncertain how to extract the date and time from Apple mail. Any insight would be appreciated.

David Sparks has a video doing exactly this, using Keyboard Maestro and AppleScript:

AppleScript Email Information Extractor

Hope this helps.

Thanks wightwizard, this helped me get the date but not the time. It appears the time is a bit harder to get, if it is even possible. Does anyone know how to extract the time from Apple Mail?

I will say I found a solution, though it is not all in Keyboard Maestro. I have Keyboard Maestro save the eMail as a pdf using a random name (to make sure it is unique to the folder) and then use Hazel to watch the folder and evaluate the content of the pdf. Hazel then extracts the date and time and renames the file to a format of my liking such as 2020-11-08 0900 (for 9:00 AM).

I would still love to see a Keyboard Maestro only solution

# Auth: Christopher Stone
# dCre: 2022/11/08 21:21
# dMod: 2022/11/08 21:21 
# Appl: Mail
# Task: Extract Date String and Time String of the Selected Message.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Mail, @Extract, @Date, @Time, @String

tell application "Mail"
   set selectedMessageList to selection
   if selectedMessageList ≠ {} then
      set selectedMessage to item 1 of selectedMessageList
      tell selectedMessage
         set messageSentDate to its date sent
      end tell
   end if
end tell

set messageSentDate to messageSentDate as «class isot» as string


From there you can find/replace to format the string to your liking.

Alternatively you could use Keyboard Maestro's %MailRawSource% token and parse the date out yourself.

** Note that the token uses AppleScript behind the scenes.