lesscake.com

Coding a simple pomodoro timer for Mac

In a nutshell, the pomodoro timer is a productivity technique that works like this:

I wanted to give this technique a try, but all the tools I found online didn't quite meet my needs. After a lot of Googling, I decided to build my own pomodoro timer in Bash. It's a simple command line tool for Mac that works with native notifications.

In this short article I'm going to cover how I made it and how it works.

Native notifications

Displaying native notifications is a non intrusive way to know when we should take a break or start working again. And it turns out it's really easy to programatically display one with AppleScript.

display notification "Foo" with title "Bar"

That's great, but how do we use that in the Terminal? There is a command that can run AppleScript: osascript. Here's how it works with our code.

osascript -e 'display notification "Foo" with title "Bar"'

Next, we transform that line into a function that takes 2 parameters: the title and the description to display. That will be handy later on.

function notif() {
  osascript -e "display notification \"$2\" with title \"$1\""
}

Once we copy-paste this code in a Terminal, typing notif "Bar" "Foo" will display a native notification.

Work timer

For the work timer, we will create a function that will wait for 25 minutes and then display a notification. The most basic way to do it would be like this.

function pwork() {
  # Wait for 60 x 25 seconds = 25 minutes
  sleep 60*25

  # Display a notification
  notif "work is over" "time to take a break"
}

This code does exactly what we want, but it's a bit rough. Let's display some messages in the Terminal to make it more user friendly.

function pwork() {
  echo "--- work starts ---"

  # Loop through all numbers between 1 and 25
  for i in {1..25}; do 
    # Wait for 60 seconds (1 minute)
    sleep 60

    # Display the elapsed time 
    echo "work: $i minutes elapsed"
  done

  # Display a notification
  notif "work is over" "time to take a break"

  echo "--- work ends ---"
}

Now after typing pwork, we can check how much time we've been working by looking at the Terminal.

Break timer

The break timer is going to be very similar to what we did earlier, the only differences are:

Here's the code:

function pbreak() {
  echo "--- break starts ---"

  for i in {1..5}; do 
    sleep 60
    echo "break: $i minutes elapsed"
  done

  notif "break is over" "time to start working"
  echo "--- break ends ---"
}

Configuration

Before we start using our program, there are 2 small things we should take care of.

First, copy-past the all the code we wrote (notif, pbreak, and pwork) in ~/.bash_profile. This is a must to always have our functions available in a Terminal.

Second, go to "System Preference > Notifications". There, find "Script Editor" in the sidebar, and select "Alerts" for the style. Now all our notifications will have to be clicked in order to be dismissed. This way we are sure to never miss a "time to start working" message, even when distracted.

Usage

We are finally ready to try the pomodoro technique!

It's possible to automatically call pbreak once pwork ends (and vice-versa), but I prefer to keep this process manual to have more flexibility.

Conclusion

In total we just wrote 20 lines of code but we get a real tool that can boost our productivity! The full source code of the project is available on GitHub.

If you have any questions or feedback: thomas@lesscake.com :-)