Python Training by Dan Bader

A countdown timer extension for Alfred

I wrote a countdown timer extension for the Alfred application launcher for OS X. The extension is open-source, written in Python and uses Mountain Lion’s user notifications.

Timer extension screenshot

What is this?

I use countdown timers several times each day. Need to brew some tea? Setup a timer. Need to catch the bus in 20 minutes? Setup a timer. Waiting for the laundry to finish? Setup a timer. You get the idea. Because I use this functionality so much it has to be convenient. Getting out my phone, launching a timer app and telling it to start the countdown is not convenient. Much in the same way, launching applications on OS X is also not convenient if you do it by navigating to the Applications folder and double-clicking an icon. Luckily there is a nice solution for both problems. It is called Alfred.

Alfred is a Spotlight-based application launcher that uses a text-based interface. But this description does not really do it justice. Alfred is much more than that. Alfred not only launches applications it also allows you to navigate the filesystem and to access various contextual actions, for example deleting a file or emailing it to someone. Alfred is very flexible and you can extend it with custom commands called extensions. Extensions can either provide new contextual actions or additional commands.

For my daily countdown timer needs I wrote an Alfred extension. The extension allows me to start countdown timers quickly and without hassle. The extension uses Mountain Lion’s user notifications and sounds to tell you when the time is up.

All code for the extension is available on my GitHub. It is written in Python, so take a look if you are interested in extending Alfred with Python or if you want to find out how to work with Mountain Lion’s user notifications from Python.

What are the benefits?

  • Helps you make great tea.
  • Solves your Pomodoro needs.
  • Uses Mountain Lion’s User Notifications to tell you when time’s up.
  • Plays a non-intrusive alarm sound.
  • Allows you to run multiple timers at the same time.
  • Allows you to add an optional label to the timer, e.g. “Laundry is done!”.
  • Shows you how to write Alfred extensions in Python.

How to install it?

Depending on whether you’re running Alfred 1 or Alfred 2 you need different versions of the extension. Please also note that this extension / workflow requires OS X Mountain Lion (10.8) or greater to work. Additionally, you need the Alfred PowerPack.

For Alfred 2 download and double-click Timer.alfredworkflow to install the workflow.

For Alfred 1 download and double-click Timer.alfredextension to install the extension.

How to use it?

  • The general syntax is timer [minutes] [optional:title]
  • timer 5 sets a countdown timer that goes off after 5 minutes.
  • timer 0:30 or timer 0.5 sets a timer that goes off after 30 seconds.
  • timer 40 Laundry is done! adds an optional title to the timer.
  • timer displays usage information.

Update: Even more notification fixes

Thanks to some more hacking (GitHub issues #1 and #6) the problem where notifications would fail to display was resolved. Additionally, all notification windows now correctly display Alfred’s application icon. These fixes are included in the latest version of the workflow / extension.

Update: Support for Alfred v2

I’ve repackaged the extension into an Alfred v2 workflow. This means it’s now also possible to use the timer from Alfred v2. Functionality and usage are the same in both versions.

Update: Timer labels

Alexander Lehmann suggested that the timer could be improved by adding an additonal label argument. The label is displayed when the timer starts and when it fires. This helps running multiple timers at once without getting confused. Labels are added by simply by typing the label after the time interval. For example, timer 3:30 tea is done will get you the following result:

Timer labels

If you are interested in Scala or writing raytracers in Lisp then you should definitely check out Alexander’s blog.

Update: Notification fixes

Jay Zawrotny has reported an issue with the extension where notifications would not fire correctly. I believe that there is a codesigning problem on systems where Mountain Lion’s vanilla Python install is replaced. If you have issues with getting notifications to show up please try Jay’s suggested fix from this pull request on GitHub. Thanks Jay!

<strong><em>Improve Your Python</em></strong> with a fresh 🐍 <strong>Python Trick</strong> 💌 every couple of days

Improve Your Python with a fresh 🐍 Python Trick 💌 every couple of days

🔒 No spam ever. Unsubscribe any time.

This article was filed under: programming, and python.

Related Articles:
  • OS X notifications for your pytest runs – This article shows you how to use the pytest-osxnotify, a plugin for pytest that adds native Mac OS X notifications to the pytest terminal runner.
  • Monochrome font rendering with FreeType and Python – For my Raspberry Pi internet radio project I needed a way to render text suitable for a low resolution monochrome LCD. This article describes how to render 1-bit text using FreeType and Python.
  • Setting up Sublime Text for Python development – I recently started using Sublime Text 2 more and more as my main editor for Python development. This article explains my setup and some tweaks that make Python programmers happy.
  • Functional linked lists in Python – Linked lists are fundamental data structures that every programmer should know. This article explains how to implement a simple linked list data type in Python using a functional programming style.
  • Abstract Base Classes in Python – Abstract Base Classes (ABCs) ensure that derived classes implement particular methods from the base class. In this tutorial you’ll learn about the benefits of abstract base classes and how to define them with Python’s built-in abc module.
Latest Articles:
← Browse All Articles