Jump to content
s95hc8

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

 

RESULT:

- 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. 

Share this post


Link to post

This is really an AppleScript question bc as far as I know there’s no other way to do it with Mail.app .  This thread http://macscripter.net/viewtopic.php?id=43790 provides a script that would do more or less what you want.  It relies on AppleScript UI scripting (i.e. simulating keystrokes and the like) which tends to make it breakable anytime there’s an OS update. 

 

 

Share this post


Link to post

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.

Share this post


Link to post

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:

ObjC.import('Cocoa')
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...
    
  })
}

 

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...