Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save spring-raining/6fb9e1280b7e9553e1c96c2428721810 to your computer and use it in GitHub Desktop.

Select an option

Save spring-raining/6fb9e1280b7e9553e1c96c2428721810 to your computer and use it in GitHub Desktop.
Guard My Macbook When I'm Away
#!/bin/bash
#
# When you are working on your macbook sitting in cafe and you have to go pee,
# you need some way to guard you machine.
#
# Start this script, remove any earphones, and go do the job.
# The assumption is the thief will close the lid of the laptop before taking it away.
# This script detects the closing of the lid and plays some loud audio that will
# likely distract the thief and/or grab attention of nearby people, making the
# thief abondon his/her attempt of larceny
#
COUNTDOWN_FROM=5
LAUGHTER_LENGTH=5
PLAYLIST="ノーポイッ!"
checkNoSleep()
{
if [[ ! -f `which NoSleepCtrl` ]]
then
echo "Please install NoSleepCtrl from "
echo " https://github.com/integralpro/nosleep/releases"
echo " "
echo "Install it with CLI included in it. It's necessary to prevent"
echo "your macbook from going to sleep when somebody closes the clamshell."
echo "When you launch this script it will automatically turn the 'NoSleep'"
echo "mode ON and when you exit the script with Ctrl-C, the 'NoSleep' mode"
echo "will automatically be turned OFF."
echo "(You are not required to be running it in the tray)"
echo " "
exit
fi
}
turnNoSleepOn()
{
NoSleepCtrl -a -s 1
NoSleepCtrl -b -s 1
}
turnNoSleepOff()
{
NoSleepCtrl -a -s 0
NoSleepCtrl -b -s 0
}
onCtrlC()
{
turnNoSleepOff
# Reset the volume to medium value
osascript -e "set Volume 5"
exit
}
main()
{
checkNoSleep
turnNoSleepOn
trap onCtrlC SIGINT
let INTRUSION_DETECTED=0
while true
do
CLAMSHELL_OPEN=`ioreg -r -k AppleClamshellState -d 4 | \
grep AppleClamshellState | \
head -1 | cut -d = -f 2`
if [ $CLAMSHELL_OPEN == "Yes" ] || [ $INTRUSION_DETECTED -eq 1 ]
then
# Turn Volume Up all the way
osascript -e "set Volume 10"
STATE=`osascript -e 'tell application "iTunes" to player state as string'`
if [ "$STATE" != "playing" ]
then
#osascript -e 'tell application "iTunes" to stop'
osascript -e 'tell application "iTunes"' -e "play track 1 of user playlist \"${PLAYLIST}\"" -e 'end tell'
fi
INTRUSION_DETECTED=1
fi
sleep 1
done
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment