1

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?

newenglander
  • 1,527
  • 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

1 Answers1

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
E1Suave
  • 2,179
  • 2
  • 15
  • 18
  • 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
  • @newenglander I am not sure about that. The script utilizes the Security and Privacy in System Preferences which sets the com.apple.screensaver.plist. The script is searching to see if the Screen Saver is running and then checking the settings within the screensaver.plist to run the suspend as needed. – E1Suave May 10 '12 at 20:33
  • ok, I see the important part for me now, checking the com.apple.screensaver.plist file. Although isn't that better described as the ELAPSED_TIME? For me CHOSEN_TIME is the time described in the LaunchAgents plist. – newenglander May 10 '12 at 20:53
  • @newenglander CHOSEN_TIME is the chosen time you have selected when setting your Security and Privacy in System Preferences. ELAPSED_TIME is the amount of time the screensaver has already been running. :–) – E1Suave May 10 '12 at 21:16
  • @newenglander Have you had any luck in using the above script? – E1Suave May 12 '12 at 19:57
  • Unfortunately not, but I think it's the closest solution possible at this time, thanks! – newenglander May 15 '12 at 20:29
  • @newenglander I am sorry it isn't working for you. Is it the script that isn't working for you or a specific variation of the script? In testing the above script worked as expected. I know that you may have been attempting to utilize it in a slightly different way. – E1Suave May 15 '12 at 20:37
  • I think I'm getting a suspend after logging in, but the login screen never comes and I have to restart (2x so far). – newenglander May 16 '12 at 17:22
  • @newenglander I have made one final change in the script. At this point, are you using the script as given above or has it been modified? For troubleshooting purposes you can try the above script as given and (change the "USER_PATH") and let me know what happens. I will be glad to help. Also, what are your current settings for the "suspend" time and the "screensaver password time"? – E1Suave May 16 '12 at 17:52
  • Yeah I'm using the script above (without your last changes, what are they exactly?) with "~" as my USER_PATH. The timed suspend is 4 hours and I'm running this script at 9:30 in the morning. I always put my computer to sleep manually, perhaps that's why the script doesn't work. Although it could be something else entirely.... – newenglander May 17 '12 at 08:07
  • @newenglander FIRST THE CHANGE is if [[ ${ELAPSED_TIME} -lt ${CONVERT_TIME} ]]; then istead of if [[ ${ELAPSED_TIME} -lt ${CHOSEN_TIME} ]]; then. – E1Suave May 17 '12 at 12:46
  • @newenglander. The idea of the script that if your Security Setting (Screensaver password delay) is not 0 it will then check to see if this Screensaver password time (CHOSEN_TIME) is more than 60 seconds (which would indicate it is either equal to or greater than 1 minute). If it is then it will divide the time by 60 to convert the time into minutes. – E1Suave May 17 '12 at 13:08
  • @newenglander Now at this point the script would skip to the end and compare the time the screensaver had been running (ELAPSED_TIME) and the newly converted time (which came from your Security Setting for screensaver) and if the ELAPSED_TIME is greater. The suspend command will be launched. If the CHOSEN_TIME is not greater than a minute as in this case 5 (THIS IS STILL INCORRECT AS IT SHOULD be 5 seconds not 1/12 of a minute). Regardless in your case you should not see any issues with this because you are utilizing the 4 hour marker. – E1Suave May 17 '12 at 13:24
  • The four hour marker represents 240 minutes. Once the script sees that you have not elapsed the screensaver timer it will then and only then initiate the suspend command. Essentially the suspend command can not be given after it is above the ELASPED_TIME is more than Screensaver timer. – E1Suave May 17 '12 at 13:24