You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

AppleScript To Save Messages As PDFs With Specific Naming Convention

I am looking for an Applescript that:

1: Prompts and allows the user to select one or more Apple Mail email messages from within Mail (10.4, 10.5, 10.6).

2: Prompts and allows the user to select a save-to folder.

3: For each message, saves it as a PDF file in the save-to folder, using the following naming convention for the PDF file:

"YYYY-MM-DD HH.MM.SS Email From FIRSTNAME LASTNAME - SUBJECT",

"YYYY-MM-DD HH.MM.SS Email To FIRSTNAME LASTNAME - SUBJECT",

where in the email date-sent stamp (if I am the sender) or date-received stamp (if I am the recipient):

YYYY = the year
MM = the two-digit month
DD = the two-digit day
HH = the two-digit hour (in 24-hour time)
MM = the two-digit minute
SS = the two-digit second
FIRSTNAME is the email sender's first name for email that I receive (or is the email recipient's first name for email that I send)
LASTNAME is the email sender's last name for email that I receive (or is the email recipient's first name for email that I send)
SUBJECT = the email's subject line

For example: 2010-04-10 16.32.48 Email From Kris Ryan - Status Of Payroll Updates.pdf
For example: 2010-04-10 16.33.55 Email To Sue Anderson - RE Status Of Payroll Updates.pdf (Colon omitted after "RE".)

4: For each attachment to the message, saves it as a PDF file in the same directory using the naming convention:

"YYYY-MM-DD HH.MM.SS Email From FIRSTNAME LASTNAME Z Attachment - NUMBER - FILENAME",

where:

YYYY-MM-DD HH.MM.SS Email From FIRSTNAME LASTNAME = as above
NUMBER = an integer representing the attachment number (1, 2, ...) sorted according to filename alphabetical order
FILENAME = the name of the file attached to the email, including its extension (.docx, .xlsx, etc.)
The letter "Z" sorts the attachment PDFs after its respective parent email in the directory.

For example: 2010-04-10 16.32.48 Email From Kris Ryan Z Attachment - 1 - List Of Suggestions To Moore.doc.pdf
For example: 2010-04-10 16.32.48 Email From Kris Ryan Z Attachment - 2 - Proposed Salary Adjustments.xls.pdf
For example: 2010-04-10 16.32.48 Email From Kris Ryan Z Attachment - 3 - Salary History.pps.pdf


Note: The email attachment may consist of a PDF or MS Office file (Word, Excel, Power Point); however, a smart implementation will be able to handle any attachment kind. If the attachment is a PDF file, then save it "as is" using the stipulated naming convention, without passing it through Distiller.

Thank you.

Kurt Todoroff

iMac 27-inch Quad-Core i7 2.93GHz, Mac OS X (10.6.4)

Posted on Nov 29, 2010 4:51 PM

Reply
5 replies

Nov 30, 2010 5:34 AM in response to Kurt Todoroff

Well, that's quite a spec sheet.
Here's a start, you can easily edit the script to get what you need:

<pre style="
font-family: Monaco, 'Courier New', Courier, monospace;
font-size: 10px;
font-weight: normal;
margin: 0px;
padding: 5px;
border: 1px solid #000000;
width: 720px; height: 340px;
color: #000000;
background-color: #E6E6EE;
overflow: auto;"
title="this text can be pasted into the AppleScript Editor">
(*
Set a Mail Rule to Save Message and Attachment to Desktop.
To save attachments to another another folder on the desktop (i.e. Attachments) create the folder and then
change tell application "Finder" to set pathToAttachments to (path to desktop folder as string) & "Attachments:"
*)

using terms from application "Mail"
on perform mail action with messages theMessages
tell application "Finder" to set ptd to (path to desktop folder) as string
tell application "Finder" to set pathToAttachments to (path to desktop folder) as string
tell application "Mail"
repeat with theMessage in theMessages
set d_recd to date received of theMessage as string
set d_recd to ReplaceText(d_recd, ":", " ") of me
set d_recd to ReplaceText(d_recd, ",", " ") of me
set theText to content of theMessage
if theMessage's mail attachments is not {} then
repeat with theAttachment in theMessage's mail attachments
set theFileName to pathToAttachments & (theMessage's subject) & " (Attachment From " & (theMessage's sender) & " Sent " & d_recd & ")" & space & theAttachment's name
try
save theAttachment in theFileName
on error errnum
end try
end repeat
end if
set theFile to ptd & (theMessage's subject) & " (From " & (theMessage's sender) & " Sent " & d_recd & ")" & ".txt"
set theFileID to open for access file theFile with write permission
write theText to theFileID
close access theFileID
end repeat
end tell
end perform mail action with messages
end using terms from

on ReplaceText(theString, fString, rString)
set current_Delimiters to text item delimiters of AppleScript
set AppleScript's text item delimiters to fString
set sList to every text item of theString
set AppleScript's text item delimiters to rString
set newString to sList as string
set AppleScript's text item delimiters to current_Delimiters
return newString
end ReplaceText</pre>

Nov 30, 2010 10:45 AM in response to Tony T1

Hi Tony,

Thank you for providing the script. I launched AppleScript Editor and pasted your code into a new script file. Here is a dumb question that I had not considered. Where do I store the script? I created the folder "Mail Scripts" inside ~Library/Scripts, and then move the script file that contains your code into it. Then I quit all applications, restarted my Mac, then I launched Mail. The script menu does not appear next to the "Help" menu as I expected it to.

Thank you.

Kurt Todoroff

Message was edited by: Kurt Richard Todoroff

Nov 30, 2010 1:06 PM in response to Kurt Todoroff

Prompts and allows the user to select one or more Apple Mail email messages from within Mail (10.4, 10.5, 10.6).


I just realized that you want to Prompt the user. A Mail Rule is not what you want as it will process the files without user intervention (In my example, you could save emails and attachments from a specific email address, or an email with a keyword in the subject, etc.)

My example is not what you're looking for, sorry.

AppleScript To Save Messages As PDFs With Specific Naming Convention

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