Automating Large Type with Alfred in OS X

Posted on December 24, 2013

Okay, so we’ve all been in this situation, right? As soon as you invoke that make test command or whatever it is that takes more than 5 seconds to run, your brain goes looking for something to do while you wait.

This isn’t itself the problem.

The problem is when you then forget to check for that long-running task’s completion. I find myself getting distracted by whatever I found to read on Twitter and then eventually wandering back to my terminal ten minutes after that test run completed.

I can do better.

Kevin has a habit of using Quicksilver’s Large Type feature to show a pop-up notification when a long-running shell command completes.

I liked this idea, but I use Alfred rather than Quicksilver. (I used to use Quicksilver but switched during its dark period of non-maintenance when it was a big memory hog.) Alfred also has a large-type feature, but it’s unfortunately slightly less scriptable than Quicksilver, so I had to shim together a few things to get it working the way I wanted.

Alfred and AppleScript

First, we need some AppleScript to tell Alfred what to do. At the time of this writing, the only thing Alfred exposes as an AppleScript command is the search functionality. This gets you the Alfred search window, pre-populated with whatever text you pass to it.

tell application "Alfred 2" to search "foo"

Once we have that open, we can use the System Events application to trigger a keystroke event for Alfred’s large-type shortcut (Command+Option+L):

tell application "System Events"
    keystroke "l" using {command down, option down}
end tell

Putting these together gets us an AppleScript file that will make Alfred display the word “foo” in large type. But we want to display arbitrary text, so let’s wrap the whole thing in AppleScript’s version of int main(int argc, char *argv[]):

on run argv
        tell application "Alfred 2"
                search item 1 of argv
                tell application "System Events"
                        keystroke "l" using {command down, option down}
                end tell
        end tell
end run

Save this to a script file with OS X’s built-in AppleScript Editor program, and you can invoke it from your shell with osascript /path/to/large-type.scpt some_text.

At this point, you might notice that the underlying Alfred search window doesn’t go away when you dismiss the large type window. There’s a setting in Alfred’s preferences to do that:

Shell script wrappers

We could stop here and just remember to invoke that osascript command with the word “done” or something every time we launch a long compilation, but that’s kind of clunky. Let’s do some shell scripting to sweeten the deal. First, write an alias into your shell’s rc file:

alias big="osascript /path/to/large-type.scpt"

Now you can just stick ; big done on the end of a command that you know will take a while. This is still too much effort for me, though, so I wrote a prefix function:

waitfor () {
    $@ ; big done

Someday I’ll probably tire of even this and write myself something that figures out whether the command that just finished took longer than N seconds and alert me that it’s done, but this is a good start for now.