How to lock OS X Leopard with a keystoke

Monday, November 09, 2009 07:22 PM

A couple of friends (mercurystate and blackclover) had many questions when making the switch from a PC running Windows to a Mac running OS X. The biggest gripe that Windows power users complain about (myself included) is that there is no way of using a keyboard shortcut to lock your computer. There is a way of adding a menu bar icon for keychain manager that will lock your computer, but from version to version of OS X there is still no keyboard shortcut.

There is one prerequisite ... for the keyboard shortcut part of this to work you must have Quicksilver installed. You can download Quicksilver here.

Here's the script...

#!/bin/bash
/System/Library/CoreServices/"Menu Extras"/User.menu/Contents/Resources/CGSession -suspend
/bin/sh kill -3 $(ps wax | grep "[T]erminal.app" | awk '{print $1}')
/usr/bin/osascript -e 'tell application "Terminal" to quit'

To install follow these steps:

In Terminal

  1. In terminal type: touch lock-screen.sh
  2. In terminal type: chmod 755 lock-screen.sh
  3. In terminal type: open lock-screen.sh
  4. In the editor that opens, paste the code in this post.
  5. To test the script type: ./lock-screen.sh

In Quicksilver

  1. Goto the Quicksilver Menu -> Catalog
  2. Then Add (+) File and Folder Scanner
  3. Navigate to where you created the lock-screen.sh script.
  4. Goto Triggers.
  5. Then under Custom Triggers, Add (+) a HotKey.
  6. Type lock-screen under 'Select an item' and click Save.
  7. Now click the 'Trigger' column to set the keyboard shortcut. I used Command+L, plus a 2 second delay. You can choose any key combination.

I hope you enjoy the keyboard shortcut goodness!