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 to repeat. I’ve used Automator over the past week to construct easy packages that replace some of my personal shell scripts.

Regardless of its relative obscurity among users, I’ve realized its fee. one of the maximum compelling features of Automator is support for constructing services—headless programs that 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.

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 specialized behaviors. As a Linux refugee, one of the functions that make 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 to the ways that I take advantage of shell scripting in Automator offerings intending to simplify my work.

A trivial instance

I write all of my articles clearly 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. I then piped the output into the xclip command to 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 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.

Mac OS


To create a service, you start by 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 your provider should be 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 decision on text because the preliminary enter.

The shell script motion has some simple alternatives. You could pick out the shell environment 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 actually useful functionality in Automator because it seamlessly mixes 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 to do is run the markdown command. I’ve already hooked up the markdown command to use 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 default, Automator will save your custom services in the ~/Library/offerings directory. Any Automator provider copied into that route might be made to be had through the offerings’ menu underneath applicable conditions. To run the brand new carrier, you must choose some textual content, after which click the Markdown item in the offerings’ menu. There are some of the ways that you can customize this service to achieve trade behaviors. For instance, you could use the copy to Clipboard action at the end of the workflow to make the output visit the user’s clipboard rather than replace the chosen textual content.