Why I Love Automater & Services in OS X 10.6 SnowLeopard

Since I first started using OS X at version 10.3 I’ve always felt that the services menu had great potential but badly needed some fit and polish to make it actually live up to that promise. It has been so bad that it is basically forgotten, and almost no one remebers that it even exists. In every application in OS X there is a menu item under the apps’s main menu (the one in bold with the same name as the app) called Services, that’s what I’m talking about. When it comes to the services menu both Tiger and Leopard were major disappointments because they didn’t bring any real improvement to the neglected services menu. SnowLeopard on the other hand is a totally different story. Similarly, when Automator first came out I thought it had great promise, but that it was a very 1.0 kind of offering, again, in need of some fit and polish to allow it live up to its obvious potential. SnowLeopard provides a lot of that fit and polish, and really brings Automator forward significantly. And what’s better, Apple have combined the fit and finish in these two apparently unrelated products together, to provide some exceptionally powerful functionality.

Firstly, to the services menu. It used to be a big long list where almost everything was almost always greyed out. Now, it is only ever a short list of relevant services broken down by the types of data they operate on. If you have text selected you’ll see all the things you can do with text, if you have an image selected, all the things you can do with an image, etc.. No more long lists of irrelevant stuff, just short lists of stuff that matters.

Now lets look at Automator. You’ll notice changes from the instant you launch it. In a similar way to Garageband it starts with a screen listing the types of things you can make with Automator, and letting you choose what it is you want to start creating. You can still just make an ordinary automator workflow, but you can also make folder actions, or services or many other things. What really peaks my interest though is the ability to create your own services.

Just like before you can drag and drop in actions to build a workflow, but now each action lets you see more detail about it. There is a triangle that you can click to expand the action and allow you to see the various options you can set for that action, the details of that action, of particular importance is the type of data it takes in and puts out, and when you run the workflow it lets you see the actual value the action outputted. The last option in particular makes debugging a lot easier since you can watch what’s happening at each stage of the process.

In an ideal world Automator would allow people with absolutely no programming knowledge to program anything they wanted. Realistically we’re not there yet, and probably never will be. However you can still do a hell of a lot by purely dragging and dropping actions into a workflow. To some extent the onus is now on developers to create actions for interacting with their software. Some vendors do this, and some don’t. A good example of a vendor who does this very well is Panic Software, who’s Transmit file transfer client includes many great Automator actions.

For now though, the argument can definitely be made that there are not enough actions to do everything you could possibly imagine doing with every app via automator. If people really start to use Automator then software vendors are going to be under increasing pressure to provide more and more actions for interacting with their software, but even then people will always be able to dream up even stranger and more unexpected things they want to be able to do. Function specific actions will never be able to do everything. This is where the generic AppleScript action comes in. What ever you can’t do with pre-existing actions you can do with a little AppleScript within an AppleScript action. If you can do it with a keyboard or mouse in OS X you can write an AppleScript to do it too. This is one of the things that makes OS X so great! You may need a little help from a friendly nerd to create more complex automator actions, but your friendly nerd can do it for you, and just send you the workflow when he or she is done.

In fact, Automator offers nerds are great tool for supporting family members on Macs. If you need to change something on your parents machine and dread the idea of having to explain it over the phone, just write an automator workflow for it and send it to them!

All this is great, and very exciting, but for me Automator has one even more exciting trick up its sleeve. I’m a Linux/Unix sysadmin by trade. A very important skill that all sysadmins have to develop is scripting. Unix-type operating systems are all head together by shell scripts of many forms, and when ever a sysadmin meets a tedious and repetitive task their response is to just script it. Scripting in languages like Bash, Perl, Ruby or Python is a great skill to have, and it can really make your computing life easier. However, until now those scripts were trapped in the Terminal. Automator’s Shell Script action lets your scripts break free from the Terminal, and become a part of the OS X GUI.

Allow me to demonstrate with an example. As my first experiment with all this I very quickly put together a service to solve a simple but regularly occurring problem for me. In work I mange our DHCP server. It runs Linux, so it expects MAC addresses to be all lowercase and separated by colons, e.g. 00:16:cb:b9:f7:cb. However, most of our users use Windows, so when they copy and paste their MAC addresses to send them to me they are in Windows format, i.e. all caps and separated by dashes, e.g. 00-16-CB-B9-F7-CB. It doesn’t take long to translate from one format to another manually, but when you’re doing it many times a day it gets tedious and annoying very quickly. Writing a Perl script to transcode them would be trivial. In fact, here is such a Perl script:

# get input
my $in = $ARGV[0];

# change to all lowercase
$in = lc($in);

# change all - to :
$in =~ s/\-/:/g;

# output
print $in;

Most of that code is actually spacing and comments, so really, in terms of actual programming it’s just:

my $in = $ARGV[0]; $in = lc($in);
$in =~ s/\-/:/g; print $in;

Clearly it’s not hard to write, but invoking it from the Terminal each time would be annoying. So, clearly it would make sense to create a service to do it.

My normal work-flow would be to copy the MAC address from the relevant call in our call-tracking system, and then paste it into our DHCP management system, and then manually fix the formatting. What if I could fix it while it’s in the clipboard? That way my workflow would be: copy, fix, paste. Much simpler, and no annoying and error prone manual fixing of the formatting. So, that’s the service I created. It consists of just three parts:

An action to get the contents from the clipboard

A Shell Script action with my Perl code to do the transcoding

An action to put the result into the clipboard

I have now trivially simply added my Perl code into the OS X GUI so I can interact with it without needing to venture into the Terminal. I could also email this workflow to anyone else who may also need to convert MAC addresses. This is just a simple proof of concept. I could also expand it to also take in Cisco formatted MAC addresses (e.g. 0016.cbb9.f7cb), or to take in a glob of text and replace all MAC addresses contained anywhere in the text in any format to the desired Unix format. Anything I could do in Perl, I can now do directly in the OS X GUI – and that’s a lot!

Below is a screenshot showing the creation of my service with each action expanded to show the debugging information. You can see that when the workflow started it took a windows formatted MAC address from the clipboard, that the Perl code converted it to a Unix formatted MAC address, and that that converted MAC address was then put into the clipboard.

Click to Enlarge

This is just the tip of the iceberg. This literally just took me a few minutes to do. I’m sure I’m going to find more and more uses for both the services menu and Automator as I continue to experiment with both on SnowLeopard. It’ such a pleasure to be able to simply and trivially integrate command-line scripting into the fantastic OS X GUI!