Lock Your Mac Based on Location

A couple of days ago I stepped out of the office in the middle of the day for an appointment. I have a screen saver on my Mac mini set with a password lock to activate after a few minutes and my Mac is secured in my office where I trust the people I work with so I'm generally not too concerned about security. But on this particular day, we had people coming in and out and as I drove away I thought about my Mac sitting open on my desk and how I wished I manually locked it before I left. The more I thought about it, the more I thought it would be nice if there was some way to automate this task so that whenever I step out of the office, my Mac would automatically lock.

I thought on this for a few days and with the recent updates to the IFTTT iOS App and a little Hazel geekery, I've come up with a solution. This trick was inspired by MacSparky's post on how to Put your Mac to Sleep using Drafts.

Before we start, a warning: IOS location data and Hazel isn't perfect - but it's pretty good. I've found mine action will sometimes be triggered when I drive by my office, or sometimes it will require me to get several blocks away before it goes off. This should not be your primary method of securing your Mac, but it's a good backup when you forget to secure your computer when you leave.


If you haven't already, head over to IFTTT.com and setup an account. You should also download the accompanying app for your iPhone. If you aren't familiar with IFTTT and web automation, we covered it in depth in episode 96 of Mac Power Users. For this particular recipe, you'l want to connect IFTTT to your Dropbox account and the iOS App.

Setup a Trigger Location

Select the option a new recipe based on an iOS Location.

Select the "you exit an area" as a trigger for the recipe and specify the address you wish to use as a trigger point.

If you want to lock a computer at your home, then set your home address, at the office, use your office address. Anytime you leave this area, your action will fire. You can optionally choose to have the action fire when you arrive at a specific location, or when you enter or exit a location. It's up to you.

Setup a Trigger Location

Setup an Action

For the "That" portion of our rule, we're going to have IFTTT create a text file and save it to Dropbox. You'll want to make sure that you have Dropbox running on the computer you want to control. The title and the contents of the text file aren't necessary important, but you do want it ot have a uniform name so in the next step Hazel will have something to look for.

For this example, I choose "lock_computer" and left the default {{OccurredAt}} provided by IFTTT which will add a date and time but this is optional and just provides some additional information which is good for future troubleshooting. Also make sure you note the location of where the file is stored in your Dropbox as Hazel will monitor this file in the next step.

Setup an Action

Configure Screen Saver to Require a Password

If you haven't already, you're going to want to make sure your Screen saver is set to require a password when deactivated. You find this setting in under the General Tab of the Security & Privacy System Preference. For good measure, you'll probably also want to set the password to be required immediately after sleep or screen saver.

So far, we've setup IFTTT to create a file in your Dropbox whenever you leave a location. But we still need your Mac to act whenever it sees the presence of that file. That's where Hazel comes in. We're going to create a Hazel rule to monitor the location where the file is saved (specified by you in the step above) and perform an AppleScript to start your screen saver. This means, whenever Hazel sees the file, the screen saver will activate. Because you've set your security preferences to require a password when the screen saver begins, your screen will be locked. (I'm assuming if you're concerned about security enough to implement these measures you've already disabled automatic login and require a password to login to your account - if not best to do that now too.)

Configure Screen Saver to Require a Password

Setup Hazel Rule

Now we're going to hand off to Hazel to activate the screen saver whenever it detects the presence of the newly created file in your Dropbox. Create a new Hazel rule that monitors the folder you specified above (mine is in /Dropbox/ifttt/ios location) with the following criteria:

If Name Contains "lock_computer" (or whatever you specified for the title of the file in the step above)

Move to folder Trash (so you don't accumulate unnecessary files)
Run AppleScript and embed the following AppleScript:

tell application "screen saverEngine"
end tell

Make sure you compile the script (click the little hammer in the Hazel window)

If you prefer the computer go to sleep rather than start the screen saver, you can use the AppleScript provided in David's post. I prefer the screen saver because it provides me with a method to lock my screen, but allows my computer ot continue to run actively in the background to finish up any tasks. It's up to you.

Tip: If you have trouble getting the Hazel rule to activate, you may not have the proper criteria set either for the location of the text file or the name. If that's the case, Simply leave the location (with your iPhone) in order to trigger the text file to be created. Then come back and you'll have the actual file to work with and compare to your Hazel rule until you get it right.

Setup Hazel Rule