Skip to content

Instantly share code, notes, and snippets.

@jkingsman
Created October 8, 2019 22:04
Show Gist options
  • Save jkingsman/56c72e80e342b8295b8a5e4e39572558 to your computer and use it in GitHub Desktop.
Save jkingsman/56c72e80e342b8295b8a5e4e39572558 to your computer and use it in GitHub Desktop.
#!/bin/sh
# add alias="bash ~/idleLock.sh" to your .bashrc
# thefted from https://apple.stackexchange.com/questions/206942/detect-mouse-move-or-keystroke
# Pause/Interval between checks
checkInt=.1
# Keep track of the idle timer
lastIdle=0
while true ; do
ioresp=`ioreg -w 0 -c IOHIDSystem | sed -e '/HIDIdleTime/ !{ d' -e 't' -e '}' -e 's/.* = //g' -e 'q'`
idleNow=`echo $(( ${ioresp} / 1000000000 ))`
re='^[0-9]+$'
if ! [[ $idleNow =~ $re ]] ; then
echo "error: ioreg did not return a number" >&2; exit 1
fi
# if idle decreased then action occurred so we do something...
if [ $idleNow -lt $lastIdle ] ; then
/System/Library/CoreServices/"Menu Extras"/User.menu/Contents/Resources/CGSession -suspend
exit 1
else
lastIdle=$idleNow
fi
sleep
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment