Suspend script

Via Clark’s Tech Blog, here’s a clever way to lock your Mac screen from the Art of Geek blog. The program that does it is called CGSession, and it’s buried way down Core Services. Apparently it’s been mentioned about a million times on Mac OS X Hints, but I’d missed it because the discussion of it was either tied to Fast User Switching (which I don’t use on my machines) or buried in the Hints comments.

In any event, the Art of Geek article uses Automator to create a Service that calls CGSession, which is then bound to a Keyboard Shortcut. That seemed like too much work to me. I just created a shell script called Suspend,

#!/bin/bash

/System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend

put it in my ~/Library/Scripts/ folder, and use FastScripts to call it via a keystroke combination of ⌃⌥⌘F19 (Control-Option-Command-F19). On my iMac keyboard, the F19 key is way out at the upper right corner, which makes it fairly easy to remember.

(Also, by using FastScripts instead of Services, I can have this command on my G4 iBook, which cannot run Snow Leopard and cannot use the new improvements to Services.)

When invoked, this command suspends your user session and displays the login screen. It’s just like what happens under Fast User Switching, but you don’t have to have Fast User Switching enabled to use it. As far as I can tell, Launch Services actions associated with that user are still performed during the suspension. I’ve also found that I can still log in remotely via Screen Sharing. I’ve only been using this for a day, so there may still be a showstopper that prevents me from using this whenever I leave my computer. I’ll update the post if I find anything like that.

Update 10/29/09
No showstoppers, but I did add display sleep to the script.

You can, of course, get something like this by requiring a password when the computer wakes up from sleep or comes out of screensaver mode.

But I prefer suspension to be a deliberate thing, not something that just happens when I stop using my computer for a while.

Tags: