How To: Set OSX’s Screen Saver via Script

Standard

Throughout my career as a Mac Admin i’ve often been asked to enforce a screen saver. One environment required a screen saver to always be set, but to allow people to change the desktop background. Another required a screen saver to be set to the corporate standard at each login. This script has been used in both environments to great affect.

MCX is usually a good point to set the screen saver, but if you do not lock down the Desktop & Screensaver preference pane then these settings can be changed.

The below script has been updated & adapted many times since I 1st  posted it on 18/11/2010 I have now tested this script & updated it for 10.8.

When trying to enforce that a screen saver is always in place, i’ve used this script by hardcoding the values of the variables & running it with an every5 trigger in Casper.

When trying to set the corporate screen saver at login, I’ve used a LaunchAgent to run a local copy of the script again with the values hardcoded.

30 thoughts on “How To: Set OSX’s Screen Saver via Script

  1. myronjoffe

    Lovely script! Whats the benefit of using this script as a policy in Casper over using the Screensaver template in MCX?

    • Ben Toms

      If you’re managing this via MCX you will also need to block access to the desktop & screen saver preferences pane.. else users can change the timeout for the screen saver.

      The MCX will re-inforce the setting.. but only at login. So by running this as an ongoing policy keeps setting the timeout to the value you’ve specified.

      Does that make sense?

  2. myronjoffe

    Makes perfect sense!
    If you wanted to specify a particular Screensaver to be used on the client. Would you be able to help me amend the script?

    • Ben Toms

      Hey fella,

      I’ve finally updated the script & looks like i’ve answered your question.

      Have a ganders @ the repo if still interested.

  3. alternapop

    thanks for this. i just tried it on a 10.6.8 client and it gave an error, ” DS Error: -14136 (eDSRecordNotFound)” and doesn’t appear to be modifying either plist

    • Ben Toms

      Hmmm… Can you try it on another client?

      I’ve got some 40 10.6.8 clients running it without issue.

      • My test client is bound to AD and I’m logged in with an AD account. I think that’s the problem. It didn’t give that error on another 10.6 client that isn’t bound.

      • Andrea Vail

        Been trying to run this on 10.8 and 10.9 clients and getting the same DS Error on AD accounts, except the .plist is being written to, but it’s entering the hard coded info twice, which I’m guessing is what is rendering it useless. Any ideas?

  4. Ken

    What is the best way to run this script if I’m not using Casper? I’ve set the script as an executable command and it doesn’t seem to change anything or if it does it takes awhile before i see the change.

    • Ben Toms

      Hmmm.

      It should work outside of Casper.

      Have you entered variables into the hardcoded section of the script?

  5. lashomb

    When grep finds NFSHomeDirectory, I also pickup an OriginalNFSHomeDirectory entry. This could be because I use Centrify to bind to Active DIrectory. Changing awk to ‘NR==1 {print $2}’ limits it to the first result, and then it works for me.

  6. lashomb

    Also, I changed startTime to be named idleTime, to make it more clear to me. I was confused about your use of the startTime variable under the requirePassword section. That integer should be set to 0 or 1, so I wasn’t sure why you didn’t use requirePassword there.

      • Gary

        Hi there, this script does run on 10.9 and creates the .plists in the ByHosts folder, however the screensaver settings only seem to ‘stick’ the first time a clean user account is created. After that, once the user changes the screensaver themselves we can’t force it to change back! Has anyone else had this issue?

  7. Louis Treger

    Guys, question, what does the justMain part refer to? I have all there rest setup but not that part and I am not too sure what parameter it is referring to…

  8. Mike

    Thank you!! FWIW although this post is old, I spent some time trying to set a corporate screensaver, and out of all the different methods (plist, defaults, mobileconfig) this was the only one I could get to work. I did have success with this on 10.11 and 10.12. The only thing I had to do was remove lines 110 and 115. I assume those two were needed on older OS versions, but it was breaking it for me.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.