Automatically Trigger Automator through mail

automatormail.app

I want to trigger a workflow in automator every time I receive a new email in "mail.app". How can this be done?

Best Answer

Yes it can:

You can use the normal Mail rules with a perform action: Run Applescript.

Create a normal Mail rule applescript. But include code to call your automator workflow via the unix command /usr/bin/automator.

The /usr/bin/automator command will run the workflow you point it at and optionally can pass input onto the workflow using the -i option.

Here is a quick example of a Applescript Mail rule which gets the subject of the email/s and runs a automator workflow that speaks any text passed on to it. In this case the email subject.

The script MUST be saved in: your ~/Library/Application Scripts/com.apple.mail folder for mail.app to see them.

You can attach a script to a Mail rule. For example, you could have an incoming message trigger a script that copies information from the message and pastes it into a database that works with AppleScript.

Choose Mail > Preferences, then click Rules. Add a rule or select an existing rule to edit.

Choose Run AppleScript from the “Perform the following actions” pop-up menu.

Choose a script from the pop-up menu of scripts that are located in ~/Library/Application Scripts/com.apple.mail.

Or choose “Open in Finder” to open the folder so you can copy a script into the folder first.

If you later move or rename the script, your rule will not work.

Click OK to save the rule.

enter image description here


The example Applescript for the Rule

(Change the workFlowPath property to reflect the full path to your Automator workflow file)

property workFlowPath : quoted form of ("/Users/YourUserName/Library/Application Scripts/com.apple.mail/mailSpeakTest.workflow") --CHANGE THIS TO YOUR FULL WORKFLOW FILE PATH example  "/Users/joeblogs/Library/Application Scripts/com.apple.mail/mailSpeakTest.workflow"


    using terms from application "Mail"
        on perform mail action with messages theMessages for rule theRule
            repeat with eachMessage in theMessages
                (*Get the email Subject *)
                set input_Argument to subject of eachMessage
                (*Run subroutine for workflow *)
                my runAutomator(input_Argument)
            end repeat
        end perform mail action with messages
    end using terms from

    (*Workflow  Subroutine *)
    on runAutomator(input_Argument)

        (*Path to workflow *)


        (*Setup and Run the unix command *)
        set command to "/usr/bin/automator   -i " & quoted form of input_Argument & space & workFlowPath
        do shell script command
    end runAutomator

The Automator workflow is simply

enter image description here