Jump to content

Workflow to print email to pdf and save in folder?

Recommended Posts

I would love to create a workflow to automate the following steps but I have no idea how to go about it and would love to get some help to find a basis to start from:


1) select email in mail.app

2) invoke Alfred

3) type keyword "email to desktop"

4) workflow creates new folder on desktop and rename folder to date of selected email in mail.app + mail subject line (format "2017-12-21 1045 subject line")

5) workflow prints selected email to pdf

6) workflow saves all email attachment files into the same folder



- one new folder on the desktop, named to the exact date of the original email and including the email as pdf and all email attachments

- when selecting multiple emails, each email should result in their own individual folder so that bulk archival is possible


I currently do all this manually for every single project related mail - it gets tiring :-(

I have basic knowledge how to modify Alfred workflows.

I have unfortunately no knowledge about Apple Script :-(

In the past I have built myself many Automator Workflows but I cannot get anywhere with Automator regarding this it seems. 

Link to comment

Hey, thanks for the link, I will check it out ;-)


So far the closest I get is:


Automator workflow saved as app does (this I have linked to a quick Alfred workflow that simply starts the automator workflow app upon a keyword):

- save all email attachments of the selected mail to the desktop

- create a folder on the desktop


I have to:

- manually print the mail as pdf to the desktop

- move all items into the folder

- rename the folder to the appropriate time of the email and subject line


I just could not find any way to extract the emails date and hand that info over to rename the folder - this alone would be of great help already.


Is there ANY WAY you guys know how I could initiate the menu command "Export as PDF …" from mail.app within the Alfred workflow?

This alone would bring me so much closer to my goal of automation and I only would be left to properly rename the folder and move the files.

Link to comment
  • deanishe changed the title to Workflow to print email to pdf and save in folder?

You can click on menu items using AppleScript. The tricky bit will be getting the save dialog to point to the right directory.


Here's  a JavaScript that will create the folder you want based on the message(s) selected in Mail.app:

mail = Application('Mail')

finder = Application('Finder')
finder.includeStandardAdditions = true

fm = $.NSFileManager.defaultManager

// Return array of {subject, date, msg} objects for message selected in Mail.app
function getSelectedMessages() {
  var sel = mail.selection(),
    msgs = []
  console.log(`${sel.length} selected messages`)
  for (var i=0; i < sel.length; i++) {
    var msg = sel[i],
      subj = msg.subject(),
      date = msg.dateReceived()
    console.log(`"${subj}" on ${date}`)
    msgs.push({subject: subj, date: date, msg:msg})

  return msgs

// Replace characters not allowed on filesystem
function fsSafeString(s) {
  return s.replace(':', '-').replace('/', '-')

// Format a Date to YYYY-MM-DD HHMM
function formatDate(date) {
  var y = '' + date.getFullYear(),
    m = '' + (date.getMonth() + 1),
    d = '' + date.getDate(),
    h = '' + date.getHours(),
    M = '' + date.getMinutes()
  if (m.length < 2)
    m = '0' + m
  if (d.length < 2)
    d = '0' + d
  if (h.length < 2)
    h = '0' + h
  if (M.length < 2)
    M = '0' + M
  return [y, m, d].join('-') + ' ' + h + M  

// Generate folder path (and create it) based on message subject and date
function getFolder(msg) {
  var date = formatDate(msg.date),
    subject = fsSafeString(msg.subject),
    name = `${date} ${subject}`
  var path = finder.pathTo('home folder') + '/Desktop/' + name
  if (!fm.fileExistsAtPath(path)) {
    fm.createDirectoryAtPathWithIntermediateDirectoriesAttributesError(path, true, $(), $())
    console.log(`created "${path}"`)
  return path

function run(argv) {
  var msgs = getSelectedMessages()
  msgs.forEach(function(msg) {
    var folder = getFolder(msg)
    // Do something with the message here...


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...