alex kuang

professional yak shaver

Baby’s First Alfred Workflow

Lately I’ve been spending a lot of my work time staring at CloudWatch metrics. Particularly, we have a Kinesis application that publishes metrics on how long a record spends hanging around in the stream before getting picked up by our processor. The problem is that the metric is published in milliseconds, which while precise, is not exactly human-readable for large enough durations.

I’ve been a huge fan of Alfred for a while, and I use its quick-launch and custom search functions nearly constantly. I only recently got around to ponying up for the powerpack, which lets you define custom “workflows”, so I figured–why not use a workflow to convert the milliseconds to something more readable?

The Alfred workflow UI is basically a fancy flow chart. It allows you to string together elements for input, actions, and output–Kind of like a graphical representation of a unix pipe. The final product of the workflow looked something like this:

The first step is to define a keyword, which is how this workflow will be invoked in the alfred menu:

All this means is that I can use mr (x) to invoke the workflow, where x is some parameter. {query} is just Alfred’s magic string for–you guessed it–injecting argv. In this case, it updates the ‘preview’ item in the dropdown as the argstring is entered.

The actual script itself is simple and super hacked together in python, but the UI here is still worth showing. Note the use of {query}:

In addition to python, the “run script” element also supports bash, ruby, and all manner of things up to and including Perl.

And, the final product:

There are a few optimizations I can see immediately (for example, the ‘large print’ output seems a bit kludgy), but it’s not bad for a few minutes’ work!

And while this script was relatively simple, there’s tons more to be explored. In addition to running a script, Alfred also supports other actions such as opening an application, running a web search, running iTunes, and then some. And for output, Alfred can do things like copy to clipboard or post to the Mac’s Notification Center. Really, it’s like a graphical scripting language on steroids with easy interaction with OS X. I’ve seen some projects lately for more accessible coding via graphical interfaces; Alfred’s approach is a pretty good start as far as I’m concerned.