Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
@igorescobar

This comment has been minimized.

Copy link

commented Mar 30, 2016

LOL

@adminrobert

This comment has been minimized.

Copy link

commented Mar 30, 2016

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link
Owner Author

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
You can’t perform that action at this time.