double password entry to access account

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?


Solution 1:

The following script should do what you are needing.

TESTED ON:

  • 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.

#!/bin/bash

USER_PATH="/Users/YOUR_USER"

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
        CONVERT_TIME="${CHOSEN_TIME}"
    fi
fi

if [[ ${ELAPSED_TIME} -lt ${CONVERT_TIME} ]]; then
    /System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend   
fi