Friday, January 20, 2012

Keyboard shortcut to lock screen in OS X

When I leave my computer unattended in the office I usually lock the screen. Usually going to the login window or screen saver requires the mouse. Here I show how you can set a keyboard shortcut that will lock your screen and login window. I tested this on OS X 10.7, but it probably works in other versions as well.

1. Open Automator and create a new Service. Set the service to receive no input and to be launched in any application. As an action add Run Shell Script and enter the following line:
/System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend
Save your new service.


2. Now go to System Preferences > Keyboard > Keyboard Shortcuts. In the menu on the left select Services and scroll to the bottom to find your new service. You can now assign your keyboard shortcut of choice.


The shortcut will take you to the Login Window from any application supporting services (i.e. most applications).

If you prefer to use the mouse, there are several alternatives. You can enable fast user switching in Users & Groups Preferences to get an item for login window in the menu bar, you can use the Keychain Access menu bar icon, or you use a hot corner to activate a screen saver.

8 comments:

Anonymous said...

Great tip! Coming from a windows world, and needing to lock my laptop quite a few times throughout the day, I was looking for a way to do this easily.

Thanks!

Andreas Sachse said...

Great tip/tutorial. Easy to follow - only took me a couple of minutes.
Thx.

Anonymous said...

Well done, sir. I could have sworn this was built-in in previous versions, years ago. I haven't really needed it at home, but now I have a work laptop and this way I can secure my screen without stopping all apps. Thank you!

Anonymous said...

Please remove this post! I followed these instructions, and now when I try to removed this, keyboard shortcuts doesn't show this automator script (whereas it shows the others).

The built-in, best way to do this is to use [CTRL][SHIFT][EJECT]. Do not do what this post suggests.

Anonymous said...

Worked like a charm. Thanks for taking the time to share with others!

Anonymous said...

Great tip. It works perfectly. It doesn't seem to like [CMD]L for me on 10.8, but [CMD][SHIFT]L works without issue.

The comment above that states
"The built-in, best way to do this is to use [CTRL][SHIFT][EJECT]"

--this isnot true.


[CTRL][SHIFT][EJECT] simply turns off your display, which is definitely NOT the same as locking the screen, or displaying the login window without logging out.

Anonymous said...

thanks for share.

Anonymous said...

or use Alfred -- after bringing up alfred type "lock" and it will lock the screen.