How to build Mac OS X services with Automator and shell scripting

I recently switched to Mac OS X as my number one desktop working device after spending over a decade on Linux. even though Apple’s working machine resources nearly all the command line equipment I know and love, I want to spend much less time in a terminal window and begin cultivating workflows that integrate higher with the Mac user experience.

In my quest to tear the energy of the command line out of the terminal, I’ve discovered that Apple’s Automator tool is a powerful ally. although it’s not as strong as the command line for improvisational automation, it is useful for outlining stand-on my own operations which you need in an effort to repeat. I’ve used Automator over the past week to construct easy packages that replace some of my personal shell scripts.

one of the maximum compelling features of Automator is support for constructing services—headless programs which can be pervasively on hand at some stage in the operating device. Many services are context-touchy and designed to manner or function on person input. A short and unscientific poll of Mac lovers found out that few simply use the services’ menu. regardless of its relative obscurity among users, I’ve learned to realize its fee.

Apple offers some of its own offerings—like one that supports dictionary lookup on a selected phrase—that work with the platform’s wellknown applications. 0.33-birthday celebration developers can also create offerings to deploy with their packages. Automator makes it really easy for normal end customers to create their own services with specialised behaviors.

As a Linux refugee, one of the functions that makes Automator mainly compelling is that it allows me to combine command-line operations, commands, and pipelines into my Automator workflows. in this educational, I am going to expose you of the ways that I take advantage of shell scripting in Automator offerings with a view to simplify my work.

A trivial instance

I write clearly all of my articles in the Vim textual content editor and use Markdown syntax for formatting. On Linux, I used a Markdown processing tool from the command-line to convert my articles to HTML and then piped the output into the xclip command in order that I should just paste the completed article at once into Ars Technica‘s content material management gadget. I had an easy shell script that I should name at once from within Vim itself to carry out those steps.

This same approach is still viable on Mac OS X, but I wanted to explore a greater Mac-native technique to the same problem. extra importantly, I wanted a solution that wasn’t tied entirely to Vim. it’s wherein Automator comes into play. I built a trivial service that wraps a command-line Markdown processor. I will pick out a block of editable textual content with Markdown formatting in any Mac utility and use the Markdown carrier to convert it to HTML in place.




To create a service, you start through choosing New from Automator‘s file menu. Automator will show you a list of available templates and set off you to pick out one on your new mission. You have to select the service choice, that is observed by an equipment icon. in your new carrier, you will see a bar at the pinnacle of the Automator float pane. It has combination containers that let you set filters that establish the conditions wherein your provider should be made accessible. You need to make a service that receives decided on textual content and will perform in any software.
below those mixture boxes is a checkbox that you may toggle to specify whether you need the output of your carrier to replace the selected text block. We surely want that to be checked for our Markdown service, because we’re replacing the Markdown-formatted enter textual content with the HTML output furnished with the aid of the Markdown processing engine.

The conduct of our Markdown workflow is simply simple, so it’ll simplest require one movement. From the left-hand action library pane, drag the Run Shell Script motion out into the workflow pane. it’ll routinely create a connection with the top bar, indicating that it’ll use the person’s decided on text because the preliminary enter.

The shell script motion has some simple alternatives. you could pick out the shell environment that you want to use for the operation and you may pick how you need it to address the enter. For the reason of this example, we want to use the standard Bash shell. We also need to configure the motion to bypass the input into stdin, the UNIX fashionable enter movement. this is an actually useful functionality in Automator, because it makes it possible to seamlessly mix Automator workflows with shell pipelines.

in the foremost textual content region of the shell action, we want to specify what command-line expression we want Bash to execute. In this case, all we need it to do is run the markdown command. I’ve already hooked up the markdown command the usage of the applicable Homebrew bundle, but Automator‘s Bash shell doesn’t appear to locate it in my command path. The paintings around Automator‘s difficulty locating the command, I without a doubt positioned the full direction into the text box, as you may see underneath.
Now that the workflow is complete, you simply have to shop it underneath the call Markdown. by way of default, Automator will save your custom services in the ~/Library/offerings directory. Any Automator provider that is copied into that route might be made to be had through the offerings’ menu underneath applicable conditions. To run the brand new carrier, you just must choose some textual content after which click the Markdown item in the offerings’ menu.

There are some of the ways that you can customise this service to achieve trade behaviors. as an instance, you could use the copy to Clipboard action on the end of the workflow to make the output visit the user’s clipboard rather than replacing the chosen textual content.