1

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

  • Probably best done with a folder action. – woff Oct 17 '14 at 21:47
  • @mehmenmike What is a folder action? Can you elaborate? – Kevin Vert Oct 18 '14 at 12:00
  • Read the first couple of paragraphs here – woff Oct 18 '14 at 13:18
  • This is NOT best done with folder Actions. Maybe @mehmenmike can explain why they think that is the case?? – markhunte Oct 18 '14 at 14:27
5

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

  • I have done all the steps that you have done above but was not able to get any result. Now, the path that you mentioned "/Users/USERNAME/Library/Application Scripts/com.apple.mail/mailSpeakTest.workflow" does not exist in my OS X Mavericks. I went with the closest path "/Library/Scripts/Mail Scripts" and saved my script there. Also, is it required that I save my workflow file there too? Also just to clarify, will this script automatically start my workflow every time I receive an email in my Mail app without my notice as that is exactly what I want to achieve. Thanks in Advance @markhunte – Kevin Vert Oct 18 '14 at 12:17
  • @kevin You just need to change USERNAME to your username. And mailSpeakTest.workflow to what ever you name your workflow, You can save your workflow anywhere. I just did it there for convineance of being with the applescript file. And yes mail rules run as long as mail is running. – markhunte Oct 18 '14 at 13:18
  • @kevin i have update the code for clarity and also the post – markhunte Oct 18 '14 at 14:28
  • Thank you!! This really made my day. Thanks for the clear explanation. @markhunte – Kevin Vert Oct 18 '14 at 17:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .