Please join me at my new location bryankyle.com

Tuesday, July 31, 2012

Locking Your Mac with a Keyboard Shortcut

I like to leave my MacBook Air on most of the time so that I can access it from where ever I go. But with 2 kids running around the house sometimes the keyboard will get stolen or have random keys mashed, the trackpad or mouse will get moved or buttons clicked. With all this entropy and Murphy’s law, the odds are not in my favor. So whenever I get up to leave the machine for extended periods of time I like to lock my screen.

Locking the screen basically just ensures that no one will accidentally delete a bunch of files. In a less secure environment it helps to protect the data that you have on the machine as well.

There are various ways to lock the screen on your Mac. The few ways that most people are familiar with are setting the screen saver timeout to be really low and installing the Keychain Access menubar extra. I’ve found that neither of these approaches really works well for me.

I tend to do a lot of thinking and not a lot of mindless clicking or moving the mouse. Having the screen saver turn on just because I haven’t touched the computer in a while doesn’t work for me. Installing the Keychain Access menubar extra is an option, but locking the screen from it is a hassle since it can’t be done with a keyboard shortcut.

To work around this, I decided to write my own custom OS X Service to start the screen saver. I then assigned a global keyboard shortcut to it, and Behold! A screen saver that turns on whenever I hit a certain key combination. Not a big deal, I know. But it’s the little things that count.

If you’re interested it doing something similar, follow the steps below.

Create a Service

  1. Start Automator.
  2. Create a new Service.
  3. Set the Service to not receive any input and be available in any application.
  4. Add a Run AppleScript Action.
  5. Set the script to be
    on run {input, parameters}
     tell application “System Events”
     start current screen saver
     end tell
     return input
    end run
  6. Save the service as “Lock Screen”.

Assign a Keyboard Shortcut

  1. Open System Preferences.
  2. Open Keyboard.
  3. Select Keyboard Shortcuts.
  4. Select Services from the list on the left.
  5. Scroll to the General section.
  6. Click on the Lock Screen service.
  7. Click on add shortcut.
  8. Assign the shortcut you want, I use Cmd+Opt+Shift+L.

Use it

  1. Invoke the service from the services menu; or
  2. Use the keyboard shortcut

Like a boss.