Lock and unlock a Mac using Bluetooth detection

Dec 29, '09 07:30:00AM

Contributed by: mech14

I discovered a great little app, Proximity, and have now made my Mac's security very seamless. This approach requires:

  1. Bluetooth-enabled Mac and a Bluetooth-capable phone (including the iPhone).
  2. Some AppleScript writing or copying-and-pasting.
  3. Tested only on 10.5.6 and 10.5.8 install; untested in 10.4 and 10.6.
Read on for the background on the solution, as well as the step-by-step implementation.

I frequent various sites such as Gizmodo, Lifehacker, TUAW, macosxhints, and many others, and I was pleasantly surprised to discover that I could have an Ubuntu-like security feature with my iPhone. In Ubuntu, I paired my old Nokia Bluetooth phone with my netbook via BlueProximity, which was a great little app that detected how close I was to my netbook, and either logged me out if I were outside of the proximity field (the phone was not being registered by BlueProximity), or if I came within the proximity field, I’d be seamlessly logged in again without any user intervention. I tried to find a Fink or a MacPort for BlueProximity, but this doesn’t exist. So I left it at that.

I then stumbled across Airlock, which just came out, but unfortunately costs money (~$7) and doesn’t seem to function well -- it generates a lot of false positives where it thinks that you are out of range when in fact you are sitting right in front of the computer.

After a bit more searching, I came across the wonderful little app called Proximity while reading about OS X pranks. Having zero experience with AppleScript, this was a good opportunity to dabble with the code and to find out some great uses for Proximity and AppleScripts. 2br I did some searching for good AppleScripts, modified them, and made the following two scripts that work with Proximity. The first is the inrange script, which runs when your phone is detected:

--tell application "iTunes" to play
--This is optional; if you want to play music when you are in the proximity of your computer, it will play ur music. Just delete the 2 minus signs in front of '--tell application "iTunes" to play'

tell application "System Events"
tell security preferences
set require password to wake to false
end tell
end tell

tell application "ScreenSaverEngine" to quit
What the inrange script does is play my music and makes sure that my password-activated screen saver does not have the password turned on. Otherwise, if my computer were to go to the screen saver and I’m at the desk, I’d have to retype the password every time. With this setting while I’m at the computer, it disables the password so I seamlessly transition to my desktop.

Next, the outofrange script, which runs when the phone is no longer detected:
--tell application "iTunes" to pause
--again, this is optional, but if iTunes is playing, you might want to pause it; delete '--'

tell application "System Events"
tell security preferences
set require password to wake to true
end tell
end tell

-- start screensaver (which locks the screen)
activate application "ScreenSaverEngine"
The outofrange script pauses my music, activates my screen saver password, and turns the screen saver on. If someone were to try to access my computer, they’d have to know the password. When I come into Bluetooth range again, my music restarts, the screen saver disappears, and my Mail app is frontmost so I can check what I’d missed.

You can download both scripts (along with a Read Me) here.

In an earlier version of this solution, I created an inrange script that had my password explicitly written into the script, and the script simulated typing in a password. This in itself is a security hole, so I found the above method to be a better alternative. I forgot where on Google I've found all the pieces of code that helped me with this, but thanks to those folks that wrote the original scripts on which I based my approach.

I'm new to Applescript writing, but the potential has intrigued me enough that I am now trying to automate pretty much anything I can (things that don't get automated by QuickSilver, that is).

[robg adds: The download link wasn't working when I tried to test it (it's not hosted here on Mac OS X Hints), and I haven't tested these scripts.]

Comments (34)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20091221173111783