Screensaver delay command?

junderwood
New Contributor III

Hello All,

I haven't found a definitive post on this, so can someone point a noob in the right direction? I need to set my smartboards to have a 60 min idle time before the screensaver kicks in. Preferably during imaging, but as a policy is ok too...

It seems "defaults -currentHost write com.apple.screensaver idleTime 3600" does not work in 10.10.3, nor does the managed preference in JSS.

Am I missing something?

Much appreciated.

1 ACCEPTED SOLUTION

yr_joelbruner
New Contributor III

The problem with writing to the plist is that it isn't picked up until reboot... this was less than ideal. The solution I found is actually with Applescript! Using Applescript causes the system to be aware of the change unlike using defaults.

Here's a summarized snippet of a script I have that is the meat of what you want to do...
The checks for time are essential, if the time is set to something other than what the GUI allows if will just change it to 20min when you check the GUI - and leave you scratching your head (as it did me!).

#!/bin/bash
#joel (at) brunerd.com

#supply idle time to change to
idleTimeMinutes=$4

if [ "$idleTimeMinutes" -ne 1 -a "$idleTimeMinutes" -ne 2 -a "$idleTimeMinutes" -ne 5 -a "$idleTimeMinutes" -ne 10 -a "$idleTimeMinutes" -ne 20 -a "$idleTimeMinutes" -ne 30 -a "$idleTimeMinutes" -ne 60 ]; then
    echo "Time in minutes MUST BE: 1, 2, 5, 10, 20, 30, or 60"
    exit
else    
    #convert to seconds
    idleTimeSeconds=$(( $idleTimeMinutes * 60 ))
fi

#get uid of console owner
eval $(stat -s /dev/console)
consoleUID=$st_uid
consoleUsername=$(id -un $st_uid)

if [ "$consoleUID" -eq 0 ]; then
    echo "No console user (login screen), exiting"
    exit
fi

#run commands as user
su $consoleUsername <<-EOS
osascript -e 'tell application "System Events" to set delay interval of screen saver preferences to '$idleTimeSeconds''
EOS

View solution in original post

7 REPLIES 7

calumhunter
Valued Contributor

Under what user context do you wish the screen saver activation to be set to 60 minutes?

This is a per user setting.

If you wish to set it for all users on the system at imaging time. Then you could script it with something like this

defaults write /System/Library/User Template/English.lproj/Library/Preferences/com.apple.screensaver idleTime 3600

If you wish to set it for the login window its a little different its:

defaults write /Library/Preferences/com.apple.screensaver loginWindowIdleTime 3600

If you wish to set a particular module for the loginwindow instead of flurry ( i hate flurry )

defaults write /Library/Preferences/com.apple.screensaver loginWindowModulePath "/System/Library/Frameworks/ScreenSaver.framework/Resources/Computer Name.saver"

Im sure you could use config profiles, but I'm an old school

junderwood
New Contributor III

Thanks @calumhunter, that's helpful. I actually asked the wrong question; I should have asked how to set this as policy for a logged in user after imaging. That would be more helpful for the problem we face.

In our environment, we have hundreds of Smartboards set up with Mac Minis. These macs are always on and always logged into a generic "smartboard" user. I need that user to have a 60 minute delay for screensaver. For new Macs, I suppose I can alter the user template or create a user folder to drop in, but I still need to deal with the existing macs. Overall, I'd prefer a policy I can run post-image so I can easily change the delay later if needed.

So basically, what's the best method for setting screensaver delay for a logged in user via policy? Do I need to quit cfprefsd first to get the "defaults -currentHost write com.apple.screensaver idleTime 3600" command to stick? Or is there a better method in JSS? Managed prefs doesn't seem to have an effect on this in 10.10.3.

yr_joelbruner
New Contributor III

The problem with writing to the plist is that it isn't picked up until reboot... this was less than ideal. The solution I found is actually with Applescript! Using Applescript causes the system to be aware of the change unlike using defaults.

Here's a summarized snippet of a script I have that is the meat of what you want to do...
The checks for time are essential, if the time is set to something other than what the GUI allows if will just change it to 20min when you check the GUI - and leave you scratching your head (as it did me!).

#!/bin/bash
#joel (at) brunerd.com

#supply idle time to change to
idleTimeMinutes=$4

if [ "$idleTimeMinutes" -ne 1 -a "$idleTimeMinutes" -ne 2 -a "$idleTimeMinutes" -ne 5 -a "$idleTimeMinutes" -ne 10 -a "$idleTimeMinutes" -ne 20 -a "$idleTimeMinutes" -ne 30 -a "$idleTimeMinutes" -ne 60 ]; then
    echo "Time in minutes MUST BE: 1, 2, 5, 10, 20, 30, or 60"
    exit
else    
    #convert to seconds
    idleTimeSeconds=$(( $idleTimeMinutes * 60 ))
fi

#get uid of console owner
eval $(stat -s /dev/console)
consoleUID=$st_uid
consoleUsername=$(id -un $st_uid)

if [ "$consoleUID" -eq 0 ]; then
    echo "No console user (login screen), exiting"
    exit
fi

#run commands as user
su $consoleUsername <<-EOS
osascript -e 'tell application "System Events" to set delay interval of screen saver preferences to '$idleTimeSeconds''
EOS

Josh_S
Contributor III

The command you're using should work, although it should probably be forced as an integer, and I tested it to work, providing you're running it as the user you intend to apply this setting to. If you have the username stored in the "consoleUser" variable and you're executing as root:

sudo -u "${consoleUser}" defaults -currentHost write com.apple.screensaver idleTime -int 3600

Also, make sure you do not have the "Desktop & Screen Saver" preference screen open when deploying this. Upon any preference change on this screen (even if you don't realize you changed anything) and upon closing, it will overwrite whatever value you script with what is shown in that preference screen.

calumhunter
Valued Contributor

@yr_joelbruner Interesting, defaults is CFPrefs aware. Making changes to plists with the defaults command should not require a logout or reboot. You could always kill cfprefsd after running the default command if your super paranoid that will force an update to the prefs cache.

If you run the script as a login script via policy then casper will pass the username of the user logging in as $3 to the script. So you don't need to worry about getting the consoleUser

theres always atleast 42 ways to skin a cat ;)

junderwood
New Contributor III

@yr_joelbruner That AppleScript worked for me. Thanks! Set that as a run-once policy for my SmartBoards to run at check-in and it seems to work smoothly. Marking this "solved."

yr_joelbruner
New Contributor III

Awesome, glad it came in handy for you @junderwood!

@calumhunter following up... yes, defaults is CFPrefs aware even the ScreenSaver.prefpane GUI will reflect the changes made via defaults however these changes are not really "live" The screensaver engine itself has not read these values back into itself. Try it, change the screensaver time via GUI to 20, close it, then run a defaults to set it to 60 seconds, wait ... and keep waiting! You can check the GUI it says 60 seconds but it's a liar :] It truly does require Applescript to remediate without requiring a reboot.

Further ramblings:
At a WWDC (long ago when they still had an IT track), this came up as a challenge for the Apple Enterprise Support team and a customer - the way around it was firing off a CocoaEvent to the service requiring the updated setting, one that surely only Apple knows about, they never said what it was... so until that secret sauce is found... Applescript :)