Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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?

share|improve this question
    
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
add comment

1 Answer

up vote 1 down vote accepted

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
share|improve this answer
    
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? –  newenglander 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)? –  newenglander May 10 '12 at 20:25
show 13 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.