Worklet: Setting MacOS Screensaver

Note: This worklet changes the screensaver setting only for the current signed in user. The way this worklet was written creates a folder to save screensaver images to, and clears out that folder each time the worklet is run so you can add in new pictures if needed.
Evaluation:

Exit 1

Remediation:

#!/bin/sh
# Get user logged into console and put into variable "user"
user=`ls -l /dev/console | cut -d " " -f 4`

#Creates a /Library/Backgrounds folder if there is not one already
mkdir -p /Library/Backgrounds

# Removes the previously saved screensavers in the background folder
rm -rf /Library/Backgrounds/*

# This is where the new screensavers are referenced, add/remove as necessary
# Each image lasts for 5 seconds, so for a longer time, copy the image a few times
scp screensaver1.png "/Library/Backgrounds/"
scp screensaver2.png "/Library/Backgrounds/"
scp screensaver3.png "/Library/Backgrounds/"
scp screensaver4.png "/Library/Backgrounds/"
scp screensaver5.png "/Library/Backgrounds/"
scp screensaver6.png "/Library/Backgrounds/"
scp screensaver7.png "/Library/Backgrounds/"
scp screensaver8.png "/Library/Backgrounds/"

#This section sets the preferences to use for screen savers. 
sudo -u $user defaults -currentHost write com.apple.screensaver CleanExit -string "YES"
sudo -u $user defaults -currentHost write com.apple.screensaver PrefsVersion -int 100
sudo -u $user defaults -currentHost write com.apple.screensaver showClock -string "NO"
#This idle time is counted as seconds, change the 600 (10 minutes) as needed.
sudo -u $user defaults -currentHost write com.apple.screensaver idleTime -int 600
sudo -u $user defaults -currentHost write com.apple.screensaver moduleDict -dict moduleName -string "iLifeSlideshows" path -string "/System/Library/Frameworks/ScreenSaver.framework/Resources/iLifeSlideshows.saver" type -int 0
sudo -u $user defaults -currentHost write com.apple.screensaver tokenRemovalAction -int 0

sudo -u $user defaults -currentHost write com.apple.ScreenSaverPhotoChooser LastViewedPhotoPath -string ""
sudo -u $user defaults -currentHost write com.apple.ScreenSaverPhotoChooser SelectedFolderPath -string "/Library/Backgrounds"
sudo -u $user defaults -currentHost write com.apple.ScreenSaverPhotoChooser SelectedSource -int 3
sudo -u $user defaults -currentHost write com.apple.ScreenSaverPhotoChooser ShufflesPhotos -bool false

sudo -u $user defaults -currentHost write com.apple.ScreenSaver.iLifeSlideShows styleKey -string "Classic"

sudo killall -hup cfprefsd

Upload the images you want to use, if you don’t want to use the “screensaver_” naming scheme, be sure to use the correct image names where the new images are referenced.

1 Like

I had some issues with this worklet on our managed Macs. The best writeup I found about an issue with password-protected screensavers and a recent fix can be seen here: https://blog.kolide.com/screensaver-security-on-macos-10-13-is-broken-a385726e2ae2

Per their suggestions, I took an element of this worklet (Getting current logged-in user) and then applied a very simple screensaver policy using the default squiggly line screensaver:

#!/bin/sh
# Get user logged into console and put into variable user
user=`ls -l /dev/console | cut -d " " -f 4`
#This section sets the preferences to use for screen savers.
#Integers are counted as seconds, change as needed.  600 is 10 minutes
sudo -u $user defaults -currentHost write com.apple.screensaver askForPassword -bool TRUE
sudo -u $user defaults -currentHost write com.apple.screensaver loginWindowIdleTime -int 600
sudo -u $user defaults -currentHost write com.apple.screensaver askForPasswordDelay -int 1

If there is a better way to do this I’m open to it! But, for now, I can confirm this works on a non-AD managed Macbook running 10.14. I do not expect this to change for versions more recent than 10.14.

2 Likes

Thanks for that, my worklet seemed to work fine up until the 10.14 update.