DEV Community

Yawar Amin
Yawar Amin

Posted on • Edited on

How to set up a 20-minute computer break on a Mac

DO YOU keep meaning to start following the 20-20-20 rule that your eye doctor told you about? Here's a tutorial for setting up your Mac to remind you to take that break every twenty minutes. It consists of the following:

  • A simple shell script that shows the notification
  • A Mac OS launchd agent that sets up the recurring reminder
  • Commands to initialize the agent.

First, the shell script:

# ~/scripts/twenty.sh
osascript -e 'display notification "Take a break!" with title "20-20-20 Rule"'
Enter fullscreen mode Exit fullscreen mode

This uses the Mac's built-in notification system to show the message reminding you to take a break.

Next, the launchd agent (this must be saved in ~/Library/LaunchAgents/twenty.plist):

<?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>twenty</string>

    <key>ProgramArguments</key>
    <array>
        <string>sh</string>
        <string>/Users/YOURACCOUNT/scripts/twenty.sh</string>
    </array>

    <key>StartInterval</key>
    <integer>1200</integer>
</dict>
</plist>
Enter fullscreen mode Exit fullscreen mode

(Remember to change YOURACCOUNT to your actual user account!)

And the commands:

$ launchctl load ~/Library/LaunchAgents/twenty.plist 
$ launchctl start twenty
Enter fullscreen mode Exit fullscreen mode

And voila, you now have an automated reminder on your Mac to take a twenty-minute break and help lessen eyestrain.

If you ever want to stop the agent:

$ launchctl stop twenty
Enter fullscreen mode Exit fullscreen mode

And if you ever want to change the agent settings, you'll additionally need to unload the agent and then reload and restart it to pick up the changes:

$ launchctl unload ~/Library/LaunchAgents/twenty.plist
Enter fullscreen mode Exit fullscreen mode

This should be portable to other OSs:

Top comments (0)