Preparing a Mail Rule Script
Mail doesn't have the ability to trigger Automator workflows from Mail rules, but it can run AppleScripts from Mail rules. So you'll need AppleScript's help to get to the next level.
- Launch Script Editor, which you'll find in the /Applications/AppleScript folder on your Mac.
- Enter the following script:
property theSecurityCode : "12345" using terms from application "Mail" on perform mail action with messages theMessages set theWorkflowFolder to getWorkflowFolder() if theWorkflowFolder = false then return repeat with aMessage in theMessages try set theSubject to getMessageSubject(aMessage) set theWorkflow to getWorkflow(theSubject, theWorkflowFolder) if theWorkflow is not equal to false then do shell script "automator " & theWorkflow end try end repeat end perform mail action with messages end using terms from on getWorkflowFolder() tell application "Finder" set theLibraryFolder to path to library folder from user domain as string set theWorkflowFolder to theLibraryFolder & "Workflows:Mail Rules:" if (folder theWorkflowFolder exists) = false then return false return theWorkflowFolder end tell end getWorkflowFolder on getMessageSubject(aMessage) tell application "Mail" to return subject of aMessage end getMessageSubject on getWorkflow(theSubject, theWorkflowFolder) if theSubject does not start with "Automator Workflow " & theSecurityCode & " - " then return false set theWorkflowName to text ((offset of "-" in theSubject) + 2) thru -1 of theSubject set theCurrentWorkflowPath to theWorkflowFolder & theWorkflowName & ".workflow" tell application "Finder" if (item theCurrentWorkflowPath exists) = false then return false end tell return quoted form of POSIX path of theCurrentWorkflowPath end getWorkflow
In case you're not familiar with AppleScript, let's go over what this script will do when run by a Mail rule (we'll discuss configuring a Mail rule shortly):
- First, the script locates the ~/Library/Workflows/Mail Rules folder. If this folder can't be found, then the script stops and does nothing.
- The script loops through any messages that are passed to it by the rule. For each message, the script retrieves the subject of the message and extracts the name of a workflow to be run.
- The script looks in the ~/Library/Workflows/Mail Rules folder for a workflow that matches the extracted name. If no workflow is found, the script stops and does nothing. If the workflow is found, the script runs it.
For security reasons, you'll want to be careful about when workflows can be run by Mail. For this reason, this script will only process messages whose subject begins with Automator Workflow, followed by a security code. This code is specified in the first line of the script as 12345. You should change this code to your own unique code, which can really be anything you want. It doesn't have to be a series of five numbers. It could be a series of 10 characters and numbers, or anything else you define.
Once you're finished entering the script, you need to save it:
- From the File menu in Script Editor, choose Save As.
- Name the script Launch Automator Workflow, choose Script from the File Format pop-up, and save it to the ~/Library/Workflows/Mail Rules folder (see Figure 4).
Figure 4 Saving a Mail rules script.