Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sleep the Display if the user has been Idle for 30 minutes or more. Required: OS X 10.9+
#!/bin/sh
###########################################
##
## Name: idleSleepDisplay.sh
##
## Description: Sleep Display if Idle
## Author: James W Mann <me@jameswmann.com>
##
## Notes: Minimum Idle time is set for 30 minutes ( 1800 seconds ) by default.
## Change MINIDLE to your desired wait period, in seconds.
##
## Requirements: OS X 10.9+
##
###########################################
# Define the minimum amount of Idle time (in seconds) to be considered no longer active
MINIDLE=1800
# Grab the system defined Idle time and convert it to seconds
# Source: https://www.dssw.co.uk/blog/2015-01-21-inactivity-and-idle-time/
IDLETIME=$((`ioreg -c IOHIDSystem | sed -e '/HIDIdleTime/ !{ d' -e 't' -e '}' -e 's/.* = //g' -e 'q'` / 1000000000))
echo "$0: Idle for $IDLETIME seconds.";
if [ $IDLETIME -lt $MINIDLE ]; then
echo "$0: Aborting, User is currently using the computer."
exit 1
fi
echo "$0: User is Idle, Sleeping Display..."
# This command only works in OS X 10.9+
# Source: http://apple.stackexchange.com/a/110137
pmset displaysleepnow
# Great Success!
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment