Since I wrote my earlier post about locking the screen on OS X I've found that while it mostly does what I want (keystroke, screen locks) there are few niggles:

  1. What it's actually doing is suspending the session (ie, like fast user switching), rather than just locking the screen

  2. So it takes a while to activate (often 5-10 seconds, sometimes more), which doesn't lead to a quick "press key, be sure that system is safe" when someone calls me away

  3. Certain things stop or get disconnected when you suspend the session including (a) music playing in iTunes and (b) dial up connections (including broadband modems)

Sometimes that's what I want, but sometimes I'd prefer to lock the console but, eg, have the music keep playing and not have to explicitly reconnect when I unlock the screen. (Even if lots of people recommend the suspend session approach without necessarily reporting on the caveats; even Dockables "Lock Screen" application is using the suspend session feature, based on the way it behaves.)

So I went looking to see if there is a better option, which is closer to just locking the screen. Preferably one that doesn't involve having to enter a screen saver password from the instant that the screen saver kicks in (since at least half the time when the screen saver kicks in, I'm just across the room -- or even at the desk doing something else).

The closest built in "Lock Screen" option is from the Keychain Access Preferences Menu Bar Icon which has a "Lock Screen" menu option. This reduces it to right-clicking (or control-clicking) on the icon, and then choosing the top menu option ("Lock Screen"). (And the tip seems to have worked for many years.)

But I'd still prefer to be able to trigger it with a simple key sequence that always works. As would some other people (found here, so they came up with some AppleScript to automate this navigation. (Someone managed to attach a keysequence to Lock Screen but it apparently only works if the icon already has focus -- so someone else uses two key-chords to give it focus then lock the screen; also similar two-chord approach with different keys.) I still wanted to be able to run this from the command line, hooked into my already existing key chord, so some searching gave me the hint that osascript can be used to invoke AppleScript from a file (see manpage for details). (QuickSilver had an Extra Script Plugin to lock the screen which seems to have used the Keychain Access feature, but it stopped working in Mac OS 10.6 (Snow Leopard) and the source seems to have been lost.)

So the approach I chose is:

  1. Enable the Keychain Access menu item: launch /Applications/Utilities/KeyChain Access.app, then go Keychain Access -> Preferences -> General -&gt Show Status in Menu Bar

  2. Enable access for assistive devices (needed for scripting via menus): Apple -> System Preferences -> Universal Access -> Enable access for assistive devices (at bottom of all preferences tabs)

  3. Create /usr/local/bin/lock-screen containing the attached text, and make it executable

  4. Follow my earlier directions to bind a key to running /usr/local/bin/lock-screen

The result works with or without the Keychain Access menu item being enabled, and with or without the Access for Assistive Devices, because it falls back to suspending the session if it wasn't able to invoke the Lock Screen option. However because it literally clicks on each menu bar item and checks its menu, it looks rather amusing when the Keychain Access menu item isn't present as it tries each in turn, only to fall back to suspending the session with the last menu still open. (ETA, 2010-01-28: With the help of this tip I've made the the process more likely to work by waiting 1/5th of a second for the menu to appear after clicking on the icon (otherwise about 1/3rd of the time it wouldn't see the menu option at all).)

I'd prefer a way of invoking this functionality which was less hackish than trawling through the menu bar items looking for a particular icon, but I've not found any other way to invoke that particular functionality. (Various people have reverse engineered enough of the ScreenSaver engine to be able to invoke that, even from a Objective-C program, but I've not found any source indicating how to invoke the same "Lock Screen" as is in the Keychain Access menu without going literally through the menus.)

While looking for all of this I also found:

  • Ctrl-Shift-Eject sleeps the displays, apparently as if the screen saver were invoked but without any graphics; it appears that it will prompt for a password on the same basis as the screen saver (eg, if you have the screen saver set to require a password immediately, then it'll require a password immediately, otherwise it'll require a password after whatever delay you set) -- so unfortunately it doesn't solve my problem of not wanting the screen saver to require a password immediately, but wanting a keystroke I can press when I'm leaving the machine that will require a password to unlock.

  • an older SleepTight which appears to be to ensure that a password is required when waking up from sleep (now built into MacOS), and a LockTight variant including source code, both available with source under the GPL (appears to be an applicaion that waits for its hot key, and then manipulates the screen saver, including changing the screen saver preferences to require a password before invoking it -- presumably reverting the preferences after it is done); also AppleScript to do something similar.

  • a huge Lock Desktop freewware application (800k to lock the screen!) which I haven't tried -- unfortunately many of these rely on the screen saver and/or session switching. And somewhat newer Lock My Mac freesware progrm, which I also haven't tried.

  • permanently disable bouncing icons in dock (to avoid distraction, via lifehacker)

  • this example of using Keychain Access in Perl but it appears to be limited to getting/storing things in Keychain, rather than triggering menu options.