Skip to content

Instantly share code, notes, and snippets.

@jay3sh
Created March 30, 2016 16:46
Show Gist options
  • Star 48 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save jay3sh/41d5f10293ba2aaa4019ec93766f4fdf to your computer and use it in GitHub Desktop.
Save jay3sh/41d5f10293ba2aaa4019ec93766f4fdf 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
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
# Turn Volume Up all the way
osascript -e "set Volume 10"
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
say -v Fiona "Please stay away from this laptop"
say -v Fiona "This machine will auto destruct in"
let count=$COUNTDOWN_FROM
while [ $count -gt 0 ]
do
say -v Fiona "$count"
let count-=1
sleep 1
done
for i in `seq 1 $LAUGHTER_LENGTH`
do
say -v "Hysterical" "Ha Ha Ha"
done
INTRUSION_DETECTED=1
fi
sleep 1
done
}
main
@adminrobert
Copy link

This line:

osascript -e "set Volume 10"

needs to happen after an Intrusion was detected, otherwise, simply pressing mute defeats this mechanism.

Otherwise, awesome script.

@Sawaba
Copy link

Sawaba commented Mar 31, 2016

I know this is probably just for fun, and I hate to be that guy to make a serious comment and ruin the joke, but if anyone's serious about using this as intended...

If I wanted the data on your Macbook, I would NOT close the lid, because I'd want to avoid locking the current session. I'd probably lower the lid somewhat, but short of closing it all the way, so maybe you could use the light sensor instead? In fact, no one should be touching the thing at all while you're gone, so you could also maybe use the accelerometer to detect the laptop being picked up. A combination of all three of these things (I'm not sure if light sensor and accelerometer are exposed via API) should be pretty solid!

@jay3sh
Copy link
Author

jay3sh commented Apr 1, 2016

@Sawaba excellent suggestion about accelerometer input. On some investigation I found it's called 'Sudden Motion Sensor' and apparently there's a CLI utility for that. I'll give it a try when I get chance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment