Automatic notifications for taking a break

Automate a notification to take a break

Working with computers all day is a recipe for eye strain, often leading to tired eyes by the night. The 20-20-20 rule is a guideline meant to reduce eye strain. According to the rule, one should look at something 20f away for 20s every 20 minutes. I’ve tried to follow it by memory, but mostly failed. So lets automate it! I thought of finding some app to remind me, but its more fun to do it with built-in OS features :)

Apply allows us to use osascript to create a notification. This can be a silent notification (display notification) or an “in your face” alert (display alert). I chose the in your face alert because I don’t want to miss it, lets see if i regret it during my first presentation when the alert goes off.

The one liner for the alert,

1
osascript -e 'display alert "20-20-20 Rule" message "Take a 20s break!"'

However, I want to also receive an audio cue. You can use afplay on mac to call a system sound. Putting the two together, gives you this script:

1
2
3
4
5
#!/bin/zsh
# script called eye-strain-reminder.sh
osascript -e 'display alert "20-20-20 Rule" message "Take a 20s break!"'; afplay /System/Library/Sounds/Funk.aiff
sleep 20
osascript -e 'display alert "20-20-20 Rule" message "20s done!"'; afplay /System/Library/Sounds/Funk.aiff

Now we have to wrap it inside a launch agent, this has to be saved in ~/Library/LaunchAgents/eye-strain-reminder.plist with the following content:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>eye-strain-reminder</string>

<key>ProgramArguments</key>
<array>
<string>sh</string>
<string><ABSOLUTE_PATH_TO>/eye-strain-reminder.sh</string>
</array>

<key>StartInterval</key>
<integer>1200</integer>
</dict>
</plist>

Don’t forget to update the path to the absolute path of your script!

Now you need to load and start the agent:

1
2
launchctl load ~/Library/LaunchAgents/eye-strain-reminder.plist
launchctl start eye-strain-reminder.plist

You can always stop and unload the above plist file if you want to get rid of the job.