Lock your Mac when your iPhone is out of range
NOTE: This tutorial is now, as of Mac OS X Mavericks, very out of date and all but useless. But you might get some help with a little AppleScript if you read on, just don’t expect it to work very well ;)
Today I decided that I fancied having the computers on my desk (iMac and Macbok Air with a cinema display) lock when I walked away from them and unlock when I returned, you know, like magic! Turns out this is possible but is a little bit hackier in Mountain Lion than it used to be in the past.
So here is what you need to acheive this:
- Mac running OS X, I did it on Mountain Lion but I think this would work for Lion too.
- Phone that has Bluetooth, such as an iPhone 5 in my case.
- Very rudimentary skills in AppleScript.
Step 0 - Setup
First go in to System Preferences and set up your screensaver as you would like it, I use the Vintage Prints screensaver with Cosmos photos, and I have it set to come on after one hour.
Then in Security Prefs set Require Password after sleep… to five seconds, or immediately or whatever you fancy. I like five seconds as it gives me time to react if it comes on by accident when I don’t want it to.
Step 1 - Lock Script
Next we need to create an applescript that will lock the computer when the phone goes out of range, or more precicely in this case turn on the screensaver. I have called this script PhoneOutOfRange.scpt
You can install different voices, such as Fiona in this case, or you could just have the script say nothing to you but that would be boring so we’ll ignore that option.
As you can see on line 3 we just kick off the screensaver. Pre-Lion this didn’t involve delving down in to frameworks but now you have to. (Finding this was the tricky bit)
Step 2 - Unlock Script
The easy part about unlocking is stopping the screen saver. The tricky part is unlocking the machine with your password. Pre-Lion this was easy, you just set the password required variable to NO. But now you can’t do that so you’ll have to make the system enter your password for you. Create the script below which I’ve called PhoneInRange.scpt
Now you will observe that I have hard-coded my password in here. There are three options to deal with this:
- Save your script as Run-only to create an obfuscated version of it, that should be secure enough.
- Don't care. Who the hell is going to look in your scripts to find your password.
- Store your password in Keychain, fetch it, then send the keystrokes using this. I couldn't get it to work this way but if you google it you might have more luck.
Step 3 - Proximity App
Now you need some way of activating those scripts. The best thing for this is an old app - but it still works well - called Proximity.
Download Proximity from Google Code
Use the link above to download the app and then install it on to your system. You can now run this and set it to run on startup.
Step 4 - Put It All Together
You will notice Proximity has put a new item in your menu bar - click on this and open the preferences. Here is what I changed:
- Set the device monitoring interval to 20 seconds, or whatever you think best.
- Click *Change Device* and pair this with your iPhone, it may also work with Android phones, who knows!
- You can then *Check Connectivity* to make sure your phone is linked up correctly.
- Then simply set the Out of Range Script to the script you made in Step 1.
- And set the In Range Script to the script you made in Step 2.
- You can now test these scripts from the app.
Step 5 - Try It
Walk away from your computer to test if the process works, to be sure walk out of the room. hahaha, nice.
Or you could simply turn Bluetooth off on your phone, then turn it back on again after the screen has locked.
And now you can forget your password!
I hope this works for you and gives you a starting point for more scripting fun, enjoy :)