How to export multiple emails to PDF with the date of receipt in the name ?

Hi everyone,


From Mail, I would like to export my thousand of emails as PDF, rename them with the date of receipt and the subject. I’m looking for this format [YYYY-MM-DD_Subject].


I tried to find a way with Automator.

First I exported my old mails to PDF then I used the action « rename finder items ».



The problem, Automator will use the date when I created the PDF so the date of today and not the date of receipt. I didn’t find any action to rename an email before exporting it.


So I need an AppleScript in Automator or Mail to fix this problem.

It seems like the best way is to add a new rule in Mail and to run an Applescript.


I found an interesting article but this Applescript will only save the email attachments :

http://www.macosxtips.co.uk/index_files/automatically-save-mail-attachments-using-applescript.php


I’m not the only one looking for an Applescript recently :

https://stackoverflow.com/questions/60921232/applescript-to-save-email-as-pdf



I don’t know how to write an AppleScript, if you can help me, I will be very grateful.


Thank you in advance for your answers.

MacBook

Posted on Apr 21, 2020 2:40 AM

Reply
14 replies

Apr 22, 2020 12:07 PM in response to Camelot

Thank you very much Camelot for your answer !


I'm a complete beginner in scripting so your explanations are very helpful.


I tried your UI script in Script Editor, the emails selected are well exported in PDF, but It doesn't add the date before the subject.


A screenshot when I tried the UI script :


My configuration :

MacBook Pro / Early 2011

macOS Sierra / version 10.12.6

Script Editor / version 2.9 (AppleScript 2.5)


Do you have any clue why this doesn't work ?


Thanks again for your help !

Apr 23, 2020 5:00 PM in response to Camelot

Yes, you were right !

With a delay of 1 second then it works

I tried with 1 email and I got a good result but when I tried with multiple emails it didn't work.


My result with one email :



My result with multiple emails :


• First I selected 3 emails


Then this window open :


If I click on "Choose", it will save the emails as PDF with the subject in the name but without the date in front of it :


Do you have any idea how we can repeat your script for multiple emails ?


Thank you very much Camelot for your help !

Apr 24, 2020 7:38 AM in response to Camelot

By introducing a select verb before the keystroke command, keystroke now has selected text that it will replace in the Save as: field, and the PDF gets exported with the constructed filename with the date prepended.


use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

tell application "Mail"
	activate
	delay 0.2
	set i to item 1 of (get selection)
	set dr to date received of i
	tell dr to set date_string to its year & "-" & (its month as integer) & "-" & its day as rich text
	set sbj to subject of i
	tell application "System Events"
		tell application process "Mail"
			click menu item "Export as PDF…" of menu "File" of menu bar 1
			delay 1
			select
			keystroke date_string & "_" & sbj
			keystroke return
		end tell
	end tell
end tell




Apr 21, 2020 3:11 PM in response to Space_jeff

There is no inherent AppleScript command for the 'Export as PDF' option in Mail.app.


This means you cannot script this directly (e.g. tell application "Mail" to export as pdf...")


The best you can do is a UI script that takes the currently highlighted message and effectively clicks the relevant menu item for you. Kludgy, at best but it's the only way I can see to do what you want.


This should get you started, but be warned that UI scripting is fraught with problems.


use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

tell application "Mail"
	activate
	delay 0.2
	set i to item 1 of (get selection)
	set dr to date received of i
	tell dr to set date_string to its year & "-" & (its month as integer) & "-" & its day as rich text
	set sbj to subject of i
	tell application "System Events"
		tell application process "Mail"
			click menu item "Export as PDF…" of menu "File" of menu bar 1
			keystroke date_string & "_" & sbj
			keystroke return
		end tell
	end tell
end tell

Apr 22, 2020 3:31 PM in response to Space_jeff

The script is pretty straightforward - about the only thing I can think of is a race condition, where the script selects the menu and starts typing the file name before the dialog is ready for input - that would cause the filename to get mistyped, although it's oddly consistent if it always misses exactly the date component.


Either way, one thing to try: Amend the script to add a small delay:


			click menu item "Export as PDF…" of menu "File" of menu bar 1
			delay 1 -- add a delay here to allow the dialog to appear
			keystroke date_string & "_" & sbj

Apr 23, 2020 11:39 PM in response to Space_jeff

> Do you have any idea how we can repeat your script for multiple emails ?


My script was specifically written to affect the first-selected item only:


	set i to item 1 of (get selection)


That's because I'm using UI scripting and it wasn't clear to me what would/should happen when you try to export multiple emails to PDF at a time, so I forced it to work on one.

Checking it now, it appears that Mail.app automatically saves multiple emails under their respective subject line and does not offer any option to name the resulting PDFs. That's a significant hinderance.


It means with UI scripting you are going to need to select each message individually. Unfortunately, at first glance, 'selection' is read-only, which means you cannot instruct Mail.app to select a specific message prior to invoking the menu option.

Ultimately it's one of the reasons I dislike UI scripting. Things we find so easy to do manually rarely translate well. It would be far easier if Mail.app just added an AppleScript command to export a given message as PDF.


Ultimately, it may mean that another mail app might be the answer. Ironically, Microsoft Outlook looks like it has a way to 'print' to PDF, although thanks to MS's b0rked AppleScript Implemenation I can't seem to find the right syntax yet - I've only tried about a dozen ways, so I have a few hundred more before I get it right. grrrr


Apr 24, 2020 6:41 AM in response to Camelot

Thanks Camelot for this full explanation !

I understand the problem better.


It would be so nice if Apple provide an easy way to save and organise our emails, email attachments and other documents. There is no way to ask Apple to improve it?


I haven't use Microsoft Outlook on my Mac yet, but I have it in case you find another solution.


Thanks again for your time and your help !

I discovered what scripting is and I will continue to learn about it.

Apr 24, 2020 11:31 AM in response to VikingOSX

It still doesn't work.

From what I understand, in Mail.app when you export one PDF, there is this window that opens, then you can modify the name in "Save As:" field and next click on the "Save" button. For this action the Applescript works perfectly.


When you select multiple emails and click on "Export as PDF...", there is another window, in this one you can't change the names of your PDF (there is no "Save As"), I can only decide in which folder I want to save my PDFs and then click on "Choose".


So I need to change this Script and to tell him the following :

In my selection of emails, to export each emails as PDF one by one and to rename each of them !

So by this way, I'll be able to modify the name of each PDF in "Save As:" field.


As a beginner, I don't know how to write this, but do you think is possible this way ?

Apr 24, 2020 12:42 PM in response to Camelot

> I'm curious as to why exporting to PDF is your preferred solution anyway. What's wrong with leaving them in Mail.app? - it's searchable and sortable...?


I believe PDF is the best format for storing important information over the long term. It will certainly be readable by many software in the future.


I don't want to keep my emails in Mail.app because we never know if one day I'll change my laptop for another brand than Apple.


As well I don't want to keep my emails on the servers in case of hacking. Less information on the cloud, less information for the hacker. I always make copies of my files on different hard drives, of different brands.


I organize my Finder in a way to quickly find all the documents on my laptop, and each document are name in this format "YYYY-MM-DD_001_Name_Subject.pdf" (same for other format like .jpeg, .xls, ...). 


In this way I can organise my documents per year. 


By example I want to find a specific email from my bank, I go to Finder, open the folder Finance > Bank > "Name of the bank" > Correspondence > 2020 > "2020-04-24_003_Bank_Mr Smith_New contract.pdf"


I don't want to switch between Mail.app or other software, and in this way I can copy with a USB stick a folder or many documents to other device like my Samsung mobile on Android, without thinking how to access to my Mail.app. And If I want I can save all my documents on an encrypted usb key.


And thanks Camelot for the link, I'll have a look.

Apr 24, 2020 1:24 PM in response to Space_jeff

When you select two or more Mail messages, and attempt to Export to PDF, it opens a standard File chooser where you pick the filesystem destination for the exported PDF items, and Mail briefly exports each message with the default subject line as the filename. This subverts your date oriented PDF naming convention.


The Export PDF panel, with its Save As: field that you see with one item is never presented, and thus the current script cannot work.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How to export multiple emails to PDF with the date of receipt in the name ?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.