Running Automator and AppleScripts in Bash

November 14, 2014

Alfred 1 had an Automator runner for extensions, but that is missing from Alfred 2. Also, Alfred 2 does not run JavaScript Automation scripts directly. Some people have asked how to make use of these. The answer is Bash scripting! Unfortunately, many people are not comfortable at that level of programming. In this tutorial, I will try to make it more manageable.

Automator Workflow

Starting Automator

First, you have to have an Automator workflow. When you start Automator with a new document, you will see a requestor for the type of script. Simply select workflow and press choose.

Creating an Automator Script

To the blank Automator script, add a Ask for Text box with the question “What do you want?”. Save the workflow to a convient place on your harddrive with the name WhatYouWant.workflow.

Blank Alfred Workflow

Now, create a blank Alfred workflow as you see above. If you are not comfortable at making Alfred workflows, check out this series of tutorials:

Add a Keyword block with the name aj:question and attach it to a Run Script block.

Adding a Run Script block

By double clicking the Run Script block, you can add a script to it.

Opening the Workflow Directory

For now, just click the Open workflow folder button.

Workflow Directory Open

Finder will now open with that folder. It currently has the info.plist file which defines your workflow. Add the Automator workflow to this directory.

Running the Workflow

In the Run Script block, put the following where it says Script:

automator WhatYouWant.workflow

Once connected up, you can run the Keyword in Alfred. You can type your question, but it will not do anything right now because we did not try to capture the output.

The Automator workflow will put anything it receives from the user to the command line. Therefore, simply use the echo bash function to display it. Add to the script:

echo "You asked: $(automator WhatYouWant.workflow)";

What this does is run the Automator script and insert the output into our string for echoing to the user. The $() is Bash syntax for executing a command and returning the results in it’s place.

Now add a Notification block:

Adding Notification Block

When you run the aj:question keyword, you will get your question back to you in the notification.

JavaScript for Automation

With Yosemite edition of OS X, automation scripts can use JavaScript instead of AppleScript. For a more detailed description of Apple’s new version of JavaScript, see their JavaScript for Automation Release Notes.

To start off, open the Script Editor program and add these lines:

function run(args) {
    app = Application.currentApplication();
    app.includeStandardAdditions = true;
    app.displayNotification('Your question was: ' + args, {withTitle: 'Your Question'});
}

Save it to a file called WhatYouWant. It will automatically save it with the extension of .scpt as all AppleScripts have.

JavaScript in Script Editor

This file will take whatever is on the command line and put it in a notificaiton for displaying. Move this to your Alfred workflow’s directory and change the Run Script block to:

ans=`automator WhatYouWant.workflow`
osascript WhatYouWant.scpt $ans
echo $ans

This will run the automator script, pass the output to the JavaScript, and will be displayed in a notification. The notification will be displayed twice: once from Alfred and once from JavaScript!

This should get you started. Have fun coding!


comments powered by Disqus