Implement automatic screen locking prior to sleep

Jul 17, '03 10:06:00AM

Contributed by: atverd

The goal: lock the screen with standard Screen Effects automatically, right before switching the Mac to sleep state. This is a standard security precaution, and I don't know why it's not implemented in Jaguar, but here is a workaround.

There is nice free daemon called sleepwatcher; it can run some scripts on sleep or wake events. Install it and call /System -> Library -> Frameworks ->ScreenSaver.framework ->Resources -> ScreenSaverEngine.app -> Contents -> MacOS -> ScreenSaverEngine from its "sleep" script. Simple, but there is problem. If you already have ScreenSaverEngine running, a second instance will screw both of them (not deadly, but unpleasant). To prevent this, it should be called like this:

#!/bin/bash

SCRS=`ps xcopid,command|grep ScreenSaverEng|cut -b1-5`

if [ -z "$SCRS" ]; then
  # Next line is shown on TWO LINES; enter as one
  # without any added spaces...
  /System/Library/Frameworks/ScreenSaver.framework/Resources/
  ScreenSaverEngine.app/Contents/MacOS/ScreenSaverEngine &

  sleep 3
fi
That's it. Screen Effects should be configured to lock the screen, of course.

[robg adds: I haven't tested this one, but I took a look at the sleepwatcher package. I think (hopefully someone can confirm) that you make this work by creating the above script and saving it as .sleep in your home directory (remember to make it executable). sleepwatcher then checks for the .sleep file before the system goes to sleep and locks your screen. I also can't confirm that you're then asked for the password on wake -- anyone?]

Comments (18)


Mac OS X Hints
http://hints.macworld.com/article.php?story=2003071420174010