Video icon 64
Learn to code with practical video courses from Tuts+. Start your free trial today.
Advertisement

Use Your Phone to Automatically Lock Your Mac When You Walk Away

by

I’m bad about walking away from my computer and leaving it unlocked with my display clearly showing everything I was last working on. In an office environment, or even in a home amongst inquisitive children or nosy roommates, you probably want to keep that under wraps. I’ve got a couple of applications that can help you out, and we’ll go a bit further and look at how you can use AppleScripts to really tweak what your Mac does when you come and go. All you need is a phone or other Bluetooth device and we can get your Mac locking itself up in no time!


Securing Your Mac with Keycard

Keycard is a nifty application new to the Mac App Store that locks your Mac when you get up and walk away with your iPhone or other Bluetooth device.

It doesn't necessarily work out of the box, though, and may not work at all if you're not familiar with the Bluetooth preferences on both your Mac and Bluetooth device. We'll go through the steps to get Keycard and your Mac working together so you won't be left wondering why the app can't find your phone or why your Mac won't ever unlock itself.

Keycard doesn't necessarily work out of the box, though, and may not work at all if you're not familiar with the Bluetooth preferences on both your Mac and Bluetooth device.

Step 1: Add Your Device to Bluetooth Preferences

At this point, it doesn’t even matter if you have Keycard installed, because it’s not going to do you a whole lot of good. It might pair with your phone and even lock your Mac, but if you’ve got an iPhone with iOS 6 installed, you won’t be getting your Mac unlocked again unless you add your phone to your list of Bluetooth devices in System Preferences.

Bluetooth doesn’t play nice with iOS 6, but we’ll make them see eye to eye. (Keycard, while billed to unlock your Mac with your iPhone or other iOS device, will work with other Bluetooth-enabled phones and devices, too, and so will this tutorial.)

Before you get any further, make sure Bluetooth is turned on in your phone’s preferences and on your Mac and that both devices are discoverable. You won’t get very far pairing Bluetooth devices if Bluetooth isn’t turned on.

Find your device in your Bluetooth Preferences and add it to your remembered devices.
Find your device in your Bluetooth Preferences and add it to your remembered devices.

If your phone didn’t show up in the list, go back to the beginning and make sure you have Bluetooth turned on for your phone and your Mac and that both are discoverable.

Now, in your Mac’s System Preferences, open the Bluetooth preference pane and click Set Up New Device or hit the plus sign at the bottom of the window. The Bluetooth Setup Assistant will start searching for new devices; when it locates your phone, click Continue. If your phone didn’t show up in the list, go back to the beginning and make sure you have Bluetooth turned on for your phone and your Mac and that both are discoverable, make sure your phone is in range of your computer, and then try the search again.

Make sure the six-digit number checks out on your phone and your Mac and confirm on your phone.
Make sure the six-digit number checks out on your phone and your Mac and confirm on your phone.

If everything goes to plan, your Mac will attempt to pair with your phone. A six-digit code will flash on both your Mac and phone, and you will have to confirm on your phone that the codes match to complete the pairing. Because you can use Keycard with really any Bluetooth device, the final steps of the pairing procedure may vary as your device may not have a screen to display a code, but you’re quick and I bet you can figure it out from there if you’re using some device other than a phone.

Step 2: Add Your Device to Keycard

Once you’ve added your phone to your list of Bluetooth devices in your Mac’s System Preferences and have installed Keycard on your Mac, you’ll need to add your phone to Keycard’s list of connected devices.

You won’t be chained to your phone if that’s not what you want to use, and Keycard doesn’t discriminate against Android.

Keycard will search for your iPhone, even if it's searching for some other device.
Keycard will search for your iPhone, even if it's searching for some other device.

Click the Keycard icon in the menu bar and select Add a New Device. As of version 1.0, Keycard will say it’s searching for iPhones, but it will find other Bluetooth-enabled devices. You won’t be chained to your phone if that’s not what you want to use, and Keycard doesn’t discriminate against Android.

You can see your device, whether it's in range of Keycard, or replace it with a new device.
You can see your device, whether it's in range of Keycard, or replace it with a new device.

If you’ve done everything in step one, Keycard shouldn’t have any problems finding your phone, and it will appear in a list in the menu bar drop-down. You can’t have more than one device paired with Keycard at a time, but you can choose a new device to unlock your Mac by clicking Add a New Device again.

Step 3: Create a PIN

You might lose your phone. Your phone might run out of power. It could go through the wash or get stolen. And then where would you be, with a locked Mac and no way to get it unlocked? That’s why Keycard also let’s you set a failsafe PIN.

Make it a good PIN that can’t be figured out easily. Not your birthday obviously. Not your anniversary. You get the idea.

If you’ve been an adult in the last twenty years, you’re familiar with PINs. Four digits, something memorable, and unpredictable. If you’re setting up Keycard for security reasons, because you really want to keep someone out of your stuff and not just to keep someone walking past from taking a gander at your emails, make it a good PIN that can’t be figured out easily. Not your birthday obviously. Not your anniversary. You get the idea.

The lock screen will let you into your Mac with a PIN, so create one. Just in case.
The lock screen will let you into your Mac with a PIN, so create one. Just in case.

Your PIN will get you back into your Mac should you not have your phone on you. It also gets you into your Mac when you do have your phone on you. Hold the phone! Shouldn’t Keycard unlock my computer when my phone is in range? It should, but it doesn’t always.

My Mac has locked up several times while I’ve been writing this with my phone sitting next to me. Sometimes it unlocks after a few seconds, sometimes it doesn’t. The PIN is important to have if Keycard is going to be running in the background all the time.


Securing Your Mac with Proximity and AppleScripts

Proximity is a bit older and quite a bit less expensive as it's free to download. It hasn't been updated since 2009, however, so you probably won't get the same responsiveness as with the Keycard developers if you hit a big issue.

Proximity is great because rather than just locking and unlocking your screen depending on whether your phone is in range of your computer like Keycard, Proximity will do pretty much anything you’d like.

That said, Proximity doesn't just lock and unlock your screen whenever your phone comes into range. Instead, the application uses AppleScripts to tell your Mac what to do when your computer and phone are connected and then disconnect again. It can be as simple as turning on your password-protected screensaver (which we'll do) or playing music and launching your favorite applications.

Step 1: Install Proximity and Pair it with Your Device

Proximity is simple to download and install, and it’s another application that will live primarily in your menu bar. Click on its icon in your menubar and select Preferences to begin setting up Proximity with your phone. You should have already added your phone to your Mac’s list of paired devices in System Preferences as we did above when setting up Keycard.

Connect your device to Proximity and check your connectivity.
Connect your device to Proximity and check your connectivity.

Once you’ve paired your phone with Proximity, you can check connectivity in the application preferences. You can also tell if your phone is in range in the menu bar icon; it will say “Out” when Proximity can’t find your phone and “In” when it can.

Step 2: Create an AppleScript for Proximity

Proximity is great because rather than just locking and unlocking your screen depending on whether your phone is in range of your computer like Keycard, Proximity will do pretty much anything you’d like. We’ll create some really simple AppleScripts that will hide your screen for security, but there’s a lot more you can do.

If you just want to keep prying eyes off your display and out of your documents, try these two scripts:

Out-of-range script

[applescript]
tell application "ScreenSaverEngine"
activate
end tell[
[/applescript]

In-range script

[applescript]
tell application "ScreenSaverEngine"
quit
end tell
[/applescript]

The out-of-range script will just throw your screensaver up, and the in-range script takes it back down and returns you to your desktop. These two scripts work best if you don’t have your screensaver protected with a password. Proximity can’t return you to your desktop when your phone is in range if you have to input your password to get past the screensaver.

If you’re less concerned about someone glancing at your data in passing and think someone might actually sit down at your computer and start using it while you’re gone, these are not the AppleScripts you’re looking for. Also, you should probably think about talking to HR because that’s weird. I’ve got another AppleScript for you, though.

Out-of-range script

[applescript]
tell application "System Events"
set properties of security preferences to {require password to wake:true}
activate application "ScreenSaverEngine"
end tell
[/applescript]

Even if your system doesn’t require a password to turn off the screensaver, this AppleScript will fix that. It will not only keep passersby from seeing what you’re up to, but anyone who sits down at your desk will need to know your password to get any further than the login window.

There’s no accompanying in-range script for this setup, because there’s not really anything Proximity can do, at least for our purposes, with your screen locked up like that. Older versions of OS X would allow you to shut off the screensaver password with a script, which would be a nice thing to launch with Proximity when your phone comes into range, but since 10.6 it’ll just cause your screensaver to stick if you try that.

Step 3: Test Your Scripts

It’s easy to test the out-of-range scripts in Proximity’s preferences by clicking the Test button. If you’re using one of the AppleScripts from above, it should launch your screensaver, and depending which script you used, check whether it requires a password.

Once you've added your scripts, you can test them in Proximity's preferences.
Once you've added your scripts, you can test them in Proximity's preferences.

You’ll find it a bit more difficult to check that the in-range AppleScript is working, since the screensaver disappears as soon as you try to do anything anyway. To test the in-range script, first make sure that your phone is in range of Proximity by taking a peek at the menu bar. Then, simply walk your phone to the other end of the office and leave it there. (If you work at the kind of place where someone may be using your computer without your permission, as described before, you might not feel safe just stashing your phone on the other side of the office; in that case, get a buddy to stay at your computer and signal when things change.)

Check that the screensaver has launched, then go back and grab your phone. When you get back to your computer, your screensaver should have disappeared even without touching the mouse or trackpad, and you should see your desktop.

If things haven’t worked out like you wanted, for either the in-range or out-of-range scripts, check that you’ve selected the correct scripts and that Proximity can find your phone.

Step 4: See What Else You Come up With

As mentioned before, there are lots of other ways to use Proximity, especially if you don’t spend a lot of time in a passive aggressive (or aggressive aggressive) workplace. You can use Proximity to pause your music or update your status on Adium, for instance, or even dim your screen to save battery life on your laptop.

A quick web search will yield dozens more AppleScripts for you to try. Mix them and match them and play with them until Proximity and your Mac are doing exactly what you want.

Because AppleScripts are just so cool, you don’t have to choose, either; you can create an AppleScript that makes your Mac do everything you want it to do when your phone is in range. And then it will undo it all when you leave and take your phone with you.

The Proximity website has a few great AppleScripts to get you started, and a quick internet search will yield dozens more for you to try. Mix them and match them and play with them until Proximity and your Mac are doing exactly what you want.


Final Thoughts

No matter what security settings you have in place, you should probably never walk off and leave your computer in a public place, because Bluetooth or no Bluetooth trickery, you’re leaving yourself open to losing your data and a whole lot more. If you really want to secure your Mac, you’re going to have to go further than what we’ve talked about in this tutorial, but these steps will keep prying eyes off of what you’re working on at the moment.

With a bit of know-how and effort, you can go much further, though. More than locking and unlocking your screen, you can launch applications, change your status in your chat program, and get your Mac to do all those things you wish you’d remember to do every time you walk away from your desk or step out to lunch. And all with your phone.

Advertisement