Archive for AppleScript – Page 2

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

 

 

Using AppleScript to add an OmniFocus action from FileMaker Pro

I use a FileMaker Pro Advanced (FMP) database to keep my client information in my family law practice. I also use OmniFocus (OF) as my task management application. I thought it would be handy to be able to add an action to OF from FMP. I set up fields in FMP for the action’s name, due date and due time. The action is assigned to a client folder in OF based on the client record currently in use in FMP.

The following AppleScript is triggered by the press of a button in FMP:



tell application "FileMaker Pro Advanced"

       — this is to ensure that no field is selected when the script is run. A value cannot pass from FMP to OF if the field is "active"
       tell layout "Client Information"
		go to it
	end tell
	tell layout "Layout #2"
		go to it
	end tell


	tell current record
              
		set ClientName to (cell "DirectoryName")
		set ActionDate to (cell "ActionDate")
		set ActionTime to (cell "ActionTime")
		set ActionName to (cell "ActionName")

		tell application "System Events"
			set DateAndTime to ActionDate & " " & ActionTime
			set dueDate to date DateAndTime as date
		end tell
	end tell
end tell

tell application "OmniFocus"
	tell default document
		set theProject to first flattened project where its name = ClientName
		tell theProject
			set newAction to make new task with properties {name:ActionName, due date:dueDate}
		end tell
	end tell
end tell

tell application "FileMaker Pro Advanced"

       tell current record
               set cell "ActionDate" to ""
               set cell "ActionTime" to ""
               set cell "ActionName" to ""
	       display dialog "OmniFocus Action Added to Client Folder: " & ClientName
        end tell
end tell


.
 

The following is a description of what the script does:

  1. The switching of layouts is done to ensure that no field is selected (otherwise an error occurs in the script). In my database, the script is toggled from layout “Layout #2”. the layout is toggled to layout “Client Information” and back to layout “Layout #2”
  2. The values of four FMP fields are are assigned to variables in the script.
  3. The date and time of the action are converted from a text format to a time format.
  4. In OF, the correct client folder is selected. (The script assumes that the folder in OF has already been created).
  5. In the now selected OF folder a new task is created with the properties: name and due date (which includes the time).
  6. Back in FMP, the fields for the action name, due date and due time are erased.
  7. A dialog is displayed to tell me that the script was successful.