Email Automation – Keyboard Maestro & AppleScript

During a recent episode of Mac Power Users, David Sparks and Katie Floyd discussed email handling. Specifically, they talked about how messages are moved from their Inbox and where they end up. David suggested that dragging messages from the Inbox to another folder is tedious and error prone. Agreed. His solution appears to be “tagging” the emails in such a way that they can found later after searching for the “tag.” My solution is to automate the tedious drag-and-drop procedure. For email automation, I use Keyboard Maestro and AppleScript to do the filing.

First, a little background. I use (primarily) Microsoft Outlook. However, the process described below is not specific to Outlook. As a family law attorney, I represent individuals. As a result I have many “folders” that contain client emails. For example, I have folders named “Smith, John”‘, “Jones, Mary” and so on. Each client has a folder in the Mail portion of Outlook as well as a contact card in the Contact section.

When I’m ready to file an email, I select the email and press command-e. This key combination triggers a Keyboard Maestro macro which does the following:

  1. Nothing, unless Outlook is the active window (“at the front”) – Command-E is not used by Outlook, but it is by other programs.
  2. If Outlook is the active window, an AppleScript is triggered.

 

The AppleScript does the following:

  1. Assigns the sender’s email address to a variable.
  2. Switches to the Contact view.
  3. Selects all Contacts and assigns them to a variable which is a list of all contacts (I have nearly 1000 contacts – but this process is instantaneous).
  4. A folder name (which I call “directoryName”) is assigned if the sender’s email address is one of a few addresses that have no contact. I do this for email I receive from Ruby Receptionists and Square. If no folder name is assigned in this way, then the sender’s email address is compared to each email address in each contact searching for a match. A single contact can contain more that one email address. For example, client John Smith may have separate work and home email accounts. Both of these email address are taken into account.
  5. If a match is found, another text variable is defined based other contact/client name in the format “Last Name, First name”. For example, if the sender’s email address is matched to contact John Smith, then variable is set to “Smith, John”.
  6. The selected email is moved to an email folder based on the value of the variable. So, in my example, the email would be moved to the folder “Smith, John”.
  7. If the email is successfully moved, an approving sound is played. I use glass.aiff. If no match is found, (or if no corresponding email folder exists) a disapproving sound is played. For that, I use submarine.aiff. (Both of these sounds are system sounds and can be found – along with others – in the System:Library:Sounds folder). You might think that searching through 1000 contacts and moving the email would take at least a few seconds or more. Wrong. This whole process takes about 1 second – much faster (and reliable) than drag-and-drop.

 

Screen Shot 2016-01-09 at 11.59.26 AM

I use a similar AppleScript to process the “Sent” folder. That script moves messages based on the recipient rather than the sender. If the recipient does not have an associated contact, then the message is left in the “Sent” folder. There is no hotkey for this process. I use Keyboard Maestro to trigger the AppleScript in the middle of the night on a Mac Mini that is always running.

Of course, this solution isn’t perfect. One of the problems is what happens with email from other lawyers. The lawyers are contacts, but not clients so they do not have an email folder. My solution to this problem is manually editing the AppleScript to assign a specific client name (i.e. “directoryName”) when the sender of the email is an opposing attorney. This edited portion of the AppleScript can be removed when the case ends or assigned to a different client if the opposing attorney is involved in a different case.

Another problem, that I’m still working on, is what happens if I have two cases against the same opposing attorney at the same time? Also, in Florida, lawyers get email from the Clerk when documents are filed. Can the filing of those emails be automated? I’m sure the answer is “yes,” but for now it’s drag-and-drop.

EDIT 2/4/2016: For some reason, the part of the script that selects all contacts was working slowly. I used the line: “keystroke “a” using command down” to select all contacts. To speed the script up, I replaced that line with:

tell menu item “Select All” of menu 1 of menu bar item “Edit” of menu bar 1
     click it
end tell

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *