Skip to content

Instantly share code, notes, and snippets.

@bitdivine
Last active August 12, 2022 06:49
Show Gist options
  • Save bitdivine/cb5e8f66683781c141940ba785526d21 to your computer and use it in GitHub Desktop.
Save bitdivine/cb5e8f66683781c141940ba785526d21 to your computer and use it in GitHub Desktop.
Lock Apple Mac screen when bluetooth device, such as a phone, goes out of range
# MAC=xx-xx-xx-xx-xx-xx
MAX_IDLE=10
while true ; do
idle=$(/usr/sbin/ioreg -c IOHIDSystem | /usr/bin/awk '/HIDIdleTime/ {print int($NF/1000000000); exit}')
if (( idle < MAX_IDLE )) ; then
sleep $(( MAX_IDLE - idle + 1 ))
else
blueutil --inquiry 4 | grep -E "$MAC" || {
echo "Locking screen..."
pmset displaysleepnow
sleep 2m
}
sleep 1
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment