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 operations, which you must repeat. I’ve used Automator over the past week to construct easy packages that replace some of my 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 personal input. A short and unscientific poll of Mac lovers found that few use the services’ menu.

Apple offers some of its 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 easy for normal end customers to develop 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 workflows. In this educational, I will expose you to how I take advantage of shell scripting in Automator offerings intending to simplify my work.

A trivial instance

I write all 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 clip command to paste the completed paper 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 for 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, choose New from Automator’s file menu. Automator will show you a list of available templates and let you pick one for your new mission. You have to select the service choice 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 do 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. Drag the Run Shell Script motion into the workflow pane from the left-hand action library pane. It’ll routinely create a connection with the top bar, indicating that it’ll use the person’s decision on text because of the preliminary entry.

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 entry. For 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 functionality is useful in Automator because it seamlessly mixes 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, we only need to 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. In the paintings around Automator’s difficulty finding the command, I undoubtedly, positioned the full direction into the text box, as you may see underneath.

Now that the workflow is complete, you must shop it underneath the call Markdown. Automator will save your custom services in the ~/Library/offerings directory by default. 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, choose some textual content, then click the Markdown item in the offerings menu. You can customize this service to achieve trade behaviors in some ways. 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.