The beginning
My computers are usually turned on all day long. They idle while I am away having something to eat, am in a meeting, on the phone (which I do walking around) or sleep. At night there are scripts to run to download, synchronize or backup data. If they are not with me on the road or in the office they at least provide the infrastructure for accessing the data I might need. In short: I don't turn them off — only a few of them are allowed to go to sleep.
All of my data is stored in my private cloud — pictures, e-books, films, documents, financial reports and the like — and continuously synchronized across all of the machines.
It is thus in my utmost interest to have all of them secured, that way securing the data on them.
At home the office door could be locked, though that is annoying if done in short intervals. In the office — or while at a customers' site — the desk I am working at is usually open to everybody.
Securing the screen saver with a password and defining a screen corner to activate it is a perfectly good solution.
But it has been getting tiring to continuously run from one machine to the next, move the mouse in the corner, check the screen saver starting, and hurry off to do whatever needs to be done.
Then reversing all of this by again running from one computer to the next, entering the keyword (often more than once if I happen to make a typo) and all of this just for a(nother) cup of coffee.
After a while I got the uneasy feeling that it takes more time locking and unlocking the machine than it does getting — and drinking — the larger part of the coffee...
So automating this has been a long term goal, and geeky as a nerds solution needs to be, it should work automagically.
The need
That means the machine (all of them) need to detect if I am with them — or nearby — or not, alas gone. At least something associated with me…
That could be my fingerprint, my voice, my DNA, a hair sample, whatever.
The only things that I always take with me wherever I go are my body, my keys and my mobile. They would provide the signal for the machines to know when to lock and when to unlock.
Body characteristics needs a fingerprint scanner, a DNA sampler etc. and drivers on each machine, keys are just dead weight, but my mobile phone has had bluetooth.
Since bluetooth is a short range wireless technology, it is destined to be used as proximity trigger.
I've been experimenting with different apps during that time, among others Salling Clicker, HomeZone, MarcoPolo, Proximity and BluePhone Elite.
Some of those were or are free, most offer a vast array of additional features, like setting the network parameters according to a nearby wireless LAN or some dedicated DNS server.
All very helpful, but not what I was looking for. Going with the philosophy of UNIX, I was looking for small, robust programs that do one thing and do it well and programs that work together.
Not surprisingly, the winner was not one single program, but a combination of apps and scripts.
As soon as the Proximity detection was working and the machine locked and unlocked them according to my mobile (and me) leaving the vicinity or coming back, I began expanding the scripts to do a lot of additional things.
The solution
The final script does (in the following order)
- offer the user the option to stifle all actions if the connection just got lost randomly (stopping any active screen saver first)
- inform the user via speech output and Growl messages (if available) about what is going to happen for him to see when he returns
- log its actions in the system log
- mute the system volume to avoid any alarm etc. annoying others
- set the state of the instant messaging clients Adium, Skype and iChat to away/busy/not available
- set the status message/mood of the instant messaging clients to a predefined value
- pause the media players DVD Player, VLC, QuickTime Player and iTunes
- post a message on twitter
- set the status on facebook
- start taking pictures from whatever is in front of the computer in defined intervals via an iSight camera, saving the pictures for later inspection
- start watching for movement so as to detect a possible theft and alarm the surrounding (on machines equipped with the necessary sensors)
- enable the screen saver password option to prevent anybody messing with the machine (10.5-only!!)
- start the screen saver, so the machine is locked
- and turn down the brightness of the screen to save battery (if it happens to run on that)
- while saving the current states of everything that is being changed, so as to be able to revert it when the user returns
- in a way suitable for the machine to go to sleep or shut down as well.
The prerequisites
- Proximity.app OS X app that checks a paired bluetooth device (could be a mobile but also a headset or anything else that can be paired with the computer)
- Proximity_Device_Out_of_Range.scpt AppleScript that is called by Proximity.app when the bluetooth device leaves the detection range
- Proximity_Device_In_Range.scpt AppleScript that is called by Proximity.app when the bluetooth device enters the detection range
Some optional features require additional apps, scripts and binaries:
- Growl.app (optional) OS X app that unintrusively pops up messages from all sorts of sources and intelligently closes them after a while (or not)
- isightcapture (optional) universal binary used to capture pictures from the internal iSight camera at defined intervals and save then to disk for later inspection
- fbcmd (optional) perl script to post status messages to facebook
- take_isight_screenshots_in_intervals.sh (optional) bash script to take pictures unobtrusively as a background job
- motion_detection_and_alarm.sh (optional) bash script to detect motion while the machine is unattended and sound an alarm, if necessary
The inspiration and sources
… about proximity scripts in general:
- for Proximity.app: http://www.technocrat.ca/?p=44
- for Proximity.app: http://digg.com/apple/Bluetooth_Proximity_Detection_on_OS_X#c5752438
- for Bash: http://www.goitexpert.com/general/use-your-bluetooth-cell-phone-as-a-proximit...
… about AppleScript and bash in respect to what has been used in this script:
- for Date formatting: http://www.fischer-bayern.de/applescript/html/datum.html
- for Screen Saver Password Option: http://forums.macosxhints.com/showthread.php?p=447731#post447731
- for Table of key codes: http://hohabadu.de/?APPLESCRIPT/UIScripting
- for GrowlHelper.app: http://growl.info/documentation/applescript-support.php
- using Bash from AppleScript: http://developer.apple.com/mac/library/technotes/tn2002/tn2065.html
- the machanics behind amstracker http://osxbook.com/book/bonus/chapter10/ams/
… additional binaries used:
- Growl http://growl.info/
- isightcapture http://www.intergalactic.de/pages/iSight.html
- brightness http://mattdanger.net/2008/12/adjust-mac-os-x-display-brightness-from-the-ter...
- amstracker http://osxbook.com/software/sms/amstracker/
- fbcmd http://fbcmd.dtompkins.com/
… additional information that helped:
- twitter http://www.commandlinefu.com/commands/view/176/update-twitter-via-curl
Additional information
For additional information please see the comments in the Proximity_Device_Out_Of_Range-Script.
It provides information about the various settings for each of the controlled applications in addition to explanation about why and how certain things have been designed.
If you happen to expand the script, let me know. I'd be happy to add your expanded version here.