Varied OS X ‘Energy Saver’ scripts based on time of the day

Apple OS X Energy Saver Preference Pane

Here is the scenario: I use my MacBook Pro at work all day, frequently being interrupted by someone at my desk for 5-10 minutes at a time. I generally don’t want my screen to turn off because I’ve noticed that people take it passive-aggressively and either get agitated in their tone or start to trip over their words in a rush to exit the conversation. So I’ve set the “Put the display to sleep when the computer is inactive for” setting to 15 minutes. I never put the computer itself to sleep while on the power adapter.

On the other hand, when I get home, I use my MBP at my desk in the bedroom; sometimes answering e-mails prior to turning in for the night. In such a case, I don’t want to wait 15 minutes with the monitor flooding light into the room. Yes I know this is a trivial and mostly esoteric problem, but thankfully the UNIX foundation that OSX is built on can solve it.

Using crontab, the OS X command line utility pmset, bash scripting and a freeware application Caffine, we can solve this problem and create a custom Energy Saver profile based on various times of the day.

OS X includes an amazingly powerful command line utility pmset that allows the user to set any aspect of power management through the command line. A at look at the man page shows us that the command:

pmset -c displaysleep 15

will cause the display to go to sleep after 15 minutes. Combined with crontab, we can set the various display timeout lengths based on time of day.

Opening a terminal window, use your favorite text editor to make a new shell script. I prefer nano:

nano -w ~/bin/display_sleep.sh

in the editor’s new window type:


#!/bin/bash

if [ -z "$1" ]; then
	echo "You forgot to pass a 'time to sleep integer'"
	exit 1;
fi

time_to_sleep="$1"

pmset -c displaysleep "$time_to_sleep" > /dev/null

save the file, and set execute permissions on it.

chmod a+x ~/bin/display_sleep.sh

Now we can run sudo ~/display_sleep.sh <integer> to set the display sleep time as we wish. You must sudo the script, as pmset is only allowed to run as root for obvious security reasons.

On that note, we must now modify root’s crontab to run out script at a set time.


sudo su
Password: <enter your password>

crontab -l > ~/cront

this will dump a copy of root’s current crontab to file, so we don’t override it. Now open the cront in a text editor:

nano -w ~/cront

and add the following rules to bottom of the file:


*/15 20-23,0-6 * * *    /<location to script>/display_sleep.sh 1
*/15 7-19 * * *         /<location to script>/display_sleep.sh 15

Save the file and type:

crontab ~/cront

To save the rule to the active crontab. The rule’s we’ve set will run every 15 minutes at :00, :15, :30 & :45. I have the screen go to sleep after 1 minute of inactivity from 10:00PM to 6:45AM otherwise it will go to sleep after 15 minutes. You can adjust the rules as you see fit.

Caffine in the menu bar, click one to turn it off, click again to turn it back on

This solves my problem… almost. What if I am up later than 10:00PM and I spend more than a minute reading a webpage or email without moving the mouse? Then the screen goes to sleep, which can be incredibly annoying. For those situations I use a freeware application called Caffine that will allow you to toggle power management on or off with a simple button click in your menu bar.

There you go, simple, easy and it makes life just a little bit easier.