Screenshot of Mac notification from script

October 25, 2020 | Logan Williams

How to make a simple script to remind you of the 20-20-20 rule

Preventing digital eye strain is easy with the 20-20-20 rule, but it's also easy to forget to take breaks from the screen. Here's how you can set up a simple script on your Mac to remind you with notifications.

Step 1: Write the script

Copy this bash script and save it in a file somewhere. For the rest of this article, I'll assume you saved it in ~/Desktop and named it timer.sh.

#!/bin/bash
while :
do
    sleep 1200
    osascript -e 'display notification "" with title "Take a 20 second break."'
    sleep 20
    osascript -e 'display notification "" with title "Break time is over!"'
done

Step 2: Make it executable

Add execute permissions to the file by executing this command in your terminal (substituting your filename): chmod u+x ~/Desktop/timer.sh

At this point, our script works and can be run with ~/Desktop/timer.sh. But, this requires leaving a terminal window open and requires rerunning it every time you reboot your Mac. Let's make it a little better.

Step 3: Automatically launch at startup

Copy the following plist and save it as timer.plist in ~/Library/LaunchAgents/, replacing YOUR-USERNAME with your user name.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
   <key>Label</key>
   <string>com.user.loginscript</string>
   <key>ProgramArguments</key>
   <array><string>/Users/YOUR-USERNAME/Desktop/timer.sh</string></array>
   <key>RunAtLoad</key>
   <true/>
</dict>
</plist>

Now the script will be automatically run on login, thanks to OSX's process launcher and manager, launchd. To run it manually without logging out and back in, run launchctl load ~/Library/LaunchAgents/timer.plist

For more info about this step, check out this StackOverflow answer.

All done! 🚀 But what about more features?

Now you have an automatically launching script to remind you to take breaks and help prevent digital eye strain!

This script is great for simple reminders, but if you're looking for a smarter and more elegant timer, check out the app I built: 202020 Timer. It lives as an app in your menu bar and supports more features like sound notifications, pause/resume, and auto-resetting after you've been away!

I hope you enjoy it!