I have the Require password 4 hours after sleep ... option in Security and Privacy System Preferences set and am running a script with launchd to suspend my session at a certain time of day (see this question for details). The result is (when the time span and specific time of day overlap) is that my account is doubly-locked, one for each action, requiring password entry twice.

Is there a way to check (for example in a shellscript or Applescript) whether or not my account is already locked before calling /System/Library/CoreServices/Menu Extras/User.menu/Contents/Resources/CGSession -suspend command?

  • Since going to 10.8.2 I am getting this double login also. And I did not change any settings. And am not running any time-of-day scripts.
    – GEdgar
    Sep 21 '12 at 16:14

The following script should do what you are needing.


  • 10.6.x
  • 10.7.4

Prevent suspend command if Screensaver's forced login time has already been reached.

NOTE: Change the USER_PATH variable to match that of the user you wanting to manage.



ELAPSED_TIME=$(ps -eo etime,command | grep "ScreenSaverEngine.app" | grep -v "grep" | sed 's/:.*//' | awk '{print $1}')
CHOSEN_TIME=$(/usr/libexec/PlistBuddy -c "Print:askForPasswordDelay" ${USER_PATH}/Library/Preferences/com.apple.screensaver.plist | sed 's/\..*//')

if [[ ${CHOSEN_TIME} != 0 ]]; then
    if [[ ${CHOSEN_TIME} -ge 60 ]]; then
        CONVERT_TIME=$(expr ${CHOSEN_TIME} / 60)    
    elif [[ ${CHOSEN_TIME} -lt 60 ]]; then

if [[ ${ELAPSED_TIME} -lt ${CONVERT_TIME} ]]; then
    /System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend   
  • Thanks for the quick answer, just did a quick test of the ps and grep commands, nothing came up. Perhaps the 4 hour timer that locks my account is not controlled by ScreenSaverEngine.app? May 10 '12 at 19:47
  • @newenglander I made a quick edit to ensure that the script will also function if the set time is less than 1 min. As for the ps command, a result will only show up if the screensaver is in use. :–) To test this you will either have to run the script within your daemon or use a remote machine.
    – E1Suave
    May 10 '12 at 19:51
  • @newenglander You will likely have to change the USER_PATH. :–)
    – E1Suave
    May 10 '12 at 20:03
  • I apologize for the quick changes. :–(
    – E1Suave
    May 10 '12 at 20:05
  • No problem, thanks again for the updates, I will try this out. Do you think it will work if I put my computer to sleep manually, without any screen saver coming on (that's the way my computer usually sleeps)? May 10 '12 at 20:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .