arrow-left arrow-right brightness-2 chevron-left chevron-right facebook-box facebook loader magnify menu-down rss-box star twitter-box twitter white-balance-sunny window-close
Create a simple timer using LaunchBar and AppleScript
3 min read

Create a simple timer using LaunchBar and AppleScript

The problem

I often find myself at my computer waiting for a specified length of time to pass before taking action on something. These waiting periods and actions aren’t the type you’d normally associate with GTD or similar, and to that end really aren’t fit for OmniFocus (or whatever to-do app I’m using that day); they’re more ephemeral and immediate, and thus require a different approach. Examples include:

  1. Remembering to leave in 10 minutes to pick up the takeout order I just made.
  2. Remembering to get the coffee out of the french press in four minutes.
  3. Remembering to call someone back in 20 minutes.
  4. Remembering to take the pizza out of the oven in 15 minutes (when I’m not within earshot of the oven alarm).
  5. Etc.

You get the point, and I’m sure you’ve a billion use-cases of your own. The real problem with me is that sometimes I get so engrossed in what I’m doing on the computer that things like those listed above simply slip my mind, often to frustrating ends (e.g., a burnt pizza). In light of this, I wanted a simple and frictionless way to set up reminders for things that needed to be done in the very near future.

The solution

Truth be told, I didn’t even want to bother with this if I couldn’t find a solution involving LaunchBar, because, well, I use LaunchBar for everything. Fortunately, and due mostly to another project I’m working on (and eventually will discuss here), I knew LaunchBar was perfectly suited to the task, and that a solution using it and AppleScript would be fairly simple.

Since the second release candidate of LaunchBar 5, the application has included an optional after delay argument for the display in large type AppleScript command, which displays given text in a semi-transparent window across the center of your screen. The argument takes values like 1m, 5m30s, 1.5h, etc. (Note that you also can use values like 2 minutes, and 1.5 h, but for the purposes of my AppleScript (shown below) you’ll want to keep spaces out of the argument, because they’re used as delimiters.) As you probably already guessed, the argument causes the display in large type command to do nothing until the after delay period elapses, at which point it displays the string it’s been given.

Accordingly, all that needs to be passed to AppleScript is a period of time and the message you want to see once the period of time has elapsed (e.g., Get pizza out of the oven 15m). The AppleScript I whipped up simply grabs the last word of the string you pass in (i.e., the after delay value), waits for the specified time to elapse and then displays your reminder message (minus the after delay argument) until some positive action from you (i.e., either a keystroke or mouse click).

on handle_string(msg)

    set duration to last word of msg
    set mLength to (count characters of msg)
    set dLength to ((count characters of duration) + 1)
    set reminder to (characters 1 thru (mLength - dLength) of msg) as string

    tell application LaunchBar
        display in large type reminder after delay duration
    end tell

end handle_string

To get this up and running, paste the above code into a new AppleScript file and save it to ~/Library/Application Support/LaunchBar/Actions. Keep in mind that the name of the LaunchBar action will be the name of the file (e.g., I called my file timer.scpt, and so to launch the action I type timer into LaunchBar).

For those of you just starting out with LaunchBar, or heretofore using it only to launch applications, realize that in order to get your reminder message and timer value into the AppleScript action, you simply need to hit the spacebar once your action is highlighted. For example, in my case, I invoke LaunchBar, start typing out timer until the action is highlighted, hit the space bar and then punch in, say, Check on clothes in dryer 10m. Easy peasy.

And yes, you can run multiple timers at once.

You've successfully subscribed to P O L Y M A T H.
Success! Your account is fully activated, you now have access to all content.