The Problem
Hour after hour, day after day staring at the same set of glowing rectangles takes a toll on your long-distance eyesight.
Most optometrists recommend taking a 15-30 second break to focus into the distance every 15 minutes or so to keep your eyes from “locking” into nearsighted mode.
Easy enough to follow, eh? Not really—we all know how easy it is to get lost in a project and pass hours without consideration of the outside world.
The Solution
Something needed to be done to remind me to look up and out every once in a while. I figured there had to exist an OS X alert command-line API.
Enter terminal-notifier: a simple CLI written in Ruby for triggering alarms. Plug this into a cron job, et voila, we have our solution. A couple of lines bugging you every 15 minutes is all you need.
The full implementation is dead-easy. Step by step:
- Install terminal-notifier:
sudo gem install terminal-notifier
- Setup the notification script:
vi ~/.my_assets/lookaway.sh
I keep a hidden folder in my home directory containing various assets that I refer to globally. Inside this file:
/usr/local/bin/terminal-notifier \
-title "Protect Your Eyes" \
-message "Look into the distance for 15 seconds" \
-timeout 15 \
-actions "dismiss"
# allowed to look back again (15 second previous message timed out)
/usr/local/bin/terminal-notifier \
-title "15 Seconds Have Passed" \
-message "OK to look at the screen again" \
-timeout 15 \
-actions "dismiss"
Note: AppleScript can also create system alerts, but you don’t have as much control over them as with terminal-notifier, so I opted for the third-party solution. AppleScript goes as such:
# using native applescript
osascript -e \
'display notification "Look into the distance for 15 seconds" with title "Protect Your Eyes"'
- Add a trigger cron job to run every 15 minutes:
[EDITOR=/usr/bin/vim] crontab -e
Insert a new line:
0,15,30,45 * * * * /bin/bash /Users/gordonhart/.my_assets/lookaway.sh >/dev/null 2>&1
The format for crontab entries is as follows:
* * * * * <command>
| | | | |
| | | | +-- Day of the Week (range: 1-7, 1 standing for Monday)
| | | +---- Month of the Year (range: 1-12)
| | +------ Day of the Month (range: 1-31)
| +-------- Hour (range: 0-23)
+---------- Minute (range: 0-59)
(source) where comma-separated lists representing multiple values for a single field.
In the crontab entry you’ll also notice that the stdout and stderr output streams from running this shell script are redirected to /dev/null. This is done to suppress the /var/mail clutter that would otherwise accumulate every 15 minutes.
That’s it! Simple stuff can go a long way.