On most of my other computers I have a keystroke which will lock the screen which I can use when I am leaving the keyboard unattend, particularly leaving the keyboard when other people are around.

In earlier versions of Mac OS X implementing something like this required third party tools (eg, Dockables and QuickSilver, as described in Mark Wheadon's Guide to "Locking your session in Mac OS X; although note that it's important to run the Lock Screen Dockable manually before trying to run it via QuickSilver, and QuickSilver appears to need to see Dockables via a path that is /Application/Dockables/Lock Screen.app to find it -- which can be fixed with a symlink if you don't want them directly there).

With OS X 10.6 (Snow Leopard) there is a built in option using Automator to create a Service, and then binding a keystroke to it. Based on a comment from Jim on 2009-09-17 in Mark Wheadon's guide, and this Mac OS X Hint it can be set up like this:

  • mkdir /usr/local/bin (if it doesn't exist already)

  • Create /usr/local/bin/lock-screen containing:

#! /bin/sh
# Lock screen, per comment on 2009-09-17 here:
# http://www.markwheadon.com/blog/2009/02/lock-screen-mac-os-x/
#
# Written by Ewen McNeill <ewen@naos.co.nz>, 2009-11-04
#---------------------------------------------------------------------------
exec /System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend
  • Make the shell script executable, and test it (viz lock-screen)

  • Run "Automator" in (Applications, or possibly Applications/Utilities)

  • Create a new "Service", and indicate that it receives no input

  • For the action, pick Utilities -> Run Shell Script, and enter the full path of the shell script, viz /usr/local/bin/lock-screen (you could probably run the full lock CGSession command directly, but it seems more flexible to store the complexity in a separate shell script)

  • Save the Service as "Lock Screen" (File->Save)

  • At this point all applications will have a new entry in their "Services" menu (ApplicationName->Services), which is "Lock Screen"; test it out and make sure it locks the screen as expected (the files are saved in ~/Library/Services if you want to examine them)

  • To bind a keystroke to it, go to Apple -> System Preferences -> Keyboard -> Keyboard Shortcuts, in the "Services" section find "General"/"Lock Screen" and double click at the right hand end of the line with "Lock Screen" (ie, where the short cut would be displayed if there were one), and hold down Alt-Cmd-L (or your preferred keystroke)

  • At this point your keystroke should lock the screen (hopefully in all applications -- there's a suggestion that Services don't work in some applications, but the comments at the Mac OS X Hint suggest that this is only Services that need input).

For more details on Services see, eg, this MacTech article on Services and this blog post on Services; the feature has been in NeXT (on which Mac OS X is based) for 15 years (I think the only new feature in Mac OS X 10.6 is Automator to help set them up).