Skip to content

Instantly share code, notes, and snippets.

@m2ym
Created April 1, 2012 16:28
Show Gist options
  • Save m2ym/2276798 to your computer and use it in GitHub Desktop.
Save m2ym/2276798 to your computer and use it in GitHub Desktop.
Sleep MacBook when a bluetooth device turned off
#!/bin/bash
bluetooth-device-is-connected() {
system_profiler SPBluetoothDataType \
| grep $1 -A 10 \
| awk '/Connected/ { print $2 }' \
| head \
| grep -q Yes
}
interval=5
address=xx-xx-xx-xx-xx-xx
onturnoff="osascript -e 'tell app \"Finder\" to sleep'"
connected=1
while true; do
bluetooth-device-is-connected $address
current=$?
if [ $connected -eq 0 -a $current -ne 0 ]; then
sh -c "$onturnoff"
fi
connected=$current
sleep $interval
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment