Easily switch to login window

A while ago I came back to my home computer to find I had not logged out and things not as I left them. There were files on the Desktop with names like 'sdfgasfgafasz' and things moved or disappeared. It took this happening a couple of times before I connected it to my my three year old daughter and resolved to do something about it.

In OSX there are many ways ways to protect your login when you leave the computer, logging out, using a screen saver with a password etc. These all tend to have a shortcut (like ⌘⇧Q to log out of the current user or hot corners) but none of these quite fitted my usage.

I didn't want to have to log out every time I popped away from the machine as this meant starting up all the applications I was using when I came back. Using a screen saver didn't fit either as my home computer is shared with others that have separate logins. The best solution I found was to choose 'Login Window…' from the Fast User Switching menu. I'd tend to put the computer to sleep to save energy. Moving the mouse up to the User Switching menu, selecting the menu item then waiting to click the option to sleep in the login window was far too complicated though. I wanted to have a shortcut to do all of this in one.

With the revamped Services menu in Snow Leopard it's easy to have scripts always available with a keyboard shortcut. This is done by creating an Automator script (in this case using a 'Run Applescript' action), saving it as a Service and adding a a keyboard shortcut in System Preferences.

The actual script I used is pretty simple. The first line is the most complex and uses do shell script to run a command line script called CGSession stored in Menu Extras (the code for which I came across a while ago here at Mac OSX Hints). The next line delays the script execution to allow for the switch to the login window (if this delay is not long enough you may find that when you wake the computer there is a blue screen for a second or so, which can be disconcerting). The last line simply puts the computer to sleep. You can run this in Applescript Editor to see what it does.

do shell script "/System/Library/CoreServices/Menu\\ Extras/User.menu/Contents/Resources/CGSession -suspend"
delay 5 -- adjust this time as required
tell application "System Events" to sleep

Run Applescript image

In OSX 10.6 when you open Automator you are asked to choose a template. Select 'Service' and click 'Choose'. From Automator's Action list select 'Utilities' then 'Run Applescript' from the sub-list. Change the options in Action to read 'service receives no input in any application'. Then, leaving the first and last lines, copy the script above into the entry box for the Action to end up with something like this image.

From the File menu select Save to save the Automator Service. I called it 'Login Window & Sleep'. This saves it in your User's Library > Services folder. It should then be available from the Services menu of every application.

To link a keyboard shortcut go to the Application menu then Services and Services Preferences. I added the key combination ⌘⌃L to the item 'Login Window & Sleep'.

Now when you press the key combination the Service should be called and you will see the switch to the login window and your computer going to sleep.


Just discovered your site via a recommendation for "Dates to iCal 2". Looks like a fine piece of work, and I'll be sending some pounds your way shortly.

The opening to this blog post reminded me of a similar thing here — my machine seemed to suddenly forget all the setup and configuration info about every application. After the fourth or fifth application complained that it couldn't find what it needed, I started investigating, and discovered that my three-year-old had renamed ~/Library to ~/b,]]]]f

I'm happy with the hot-corner-starts-screensaver solution myself though, and that's what I use now.

Hi Tim,
To my mind it would make a lot of sense to not allow renaming of those folders expected to be there by the system without confirmation.