Category filter
Script to set screen saver on Mac
Configured to be activated after a specific period of device inactivity, screen saver plays a crucial role in protecting confidential information when the users are away from the device screen. This doc helps you configure screen saver on macOS devices using a script that can be executed with Hexnode’s Execute Custom Script action. The script lets you set up a screen saver photo slideshow across the devices.
Scripting language – Bash
File extension – .sh
Set slideshow screen saver
Here’s a sample script to set a screen saver photo slideshow. You can set the images to be displayed, the idle time after which the screen saver needs to be enabled, the slideshow style, and also choose to shuffle the images
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
#!/bin/sh Style='Shifting Tiles' IdleTime='60' ShufflePhotos='true' PhotosFolderPath=$1 Style=$(tr -d ' ','-' <<< $Style) loggedInUser=`stat -f%Su /dev/console` echo "The currently logged-in user: $loggedInUser" sudo su $loggedInUser -c ' osVersion=$(sw_vers -productVersion) osMajor=$(echo $osVersion | cut -d '.' -f 1) osMinor=$(echo $osVersion | cut -d '.' -f 2) osPatch=$(echo $osVersion | cut -d '.' -f 3) defaults write com.apple.screensaver askForPassword -int 1 defaults write com.apple.screensaver askForPasswordDelay -int 1 defaults -currentHost write com.apple.screensaver 'CleanExit' -string "YES" defaults -currentHost write com.apple.screensaver 'PrefsVersion' -int "100" defaults -currentHost write com.apple.screensaver 'idleTime' -int '$IdleTime' defaults -currentHost write com.apple.ScreenSaverPhotoChooser SelectedFolderPath -string '$1' defaults -currentHost write com.apple.ScreenSaverPhotoChooser SelectedSource -int 4 defaults -currentHost write com.apple.ScreenSaver.iLifeSlideShows styleKey -string '$Style' defaults -currentHost write com.apple.ScreenSaverPhotoChooser 'ShufflesPhotos' -bool '$ShufflePhotos' defaults -currentHost write com.apple.screensaver moduleDict -dict-add moduleName 'iLifeSlideshows' if [[ $osMajor -lt 10 || ( $osMajor -eq 10 && $osMinor -lt 14 ) || ( $osMajor -eq 10 && $osMinor -eq 14 && $osPatch -lt 2 ) ]]; then defaults -currentHost write com.apple.screensaver moduleDict -dict-add path '/System/Library/Frameworks/ScreenSaver.framework/Resources/iLifeSlideshows.saver' else defaults -currentHost write com.apple.screensaver moduleDict -dict-add path '/System/Library/Frameworks/ScreenSaver.framework/PlugIns/iLifeSlideshows.appex' fi ## Removes the .plist LaunchAgent from inside the User Launch Agent Folder. rm -f ~/Library/LaunchAgents/set-screensaver.plist killall cfprefsd exit; ' echo "The Screen Saver has been set to $Style" |
Script to configure screensaver for Sonoma devices
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
#!/bin/bash FULL_PATH_TO_FOLDER="$1" FOLDER_NAME="$2" START_AFTER=$3 function getDefinedVariables() { desiredmacOSVersion='14' screenSaverBase64='YnBsaXN0MDDRAQJWbW9kdWxl0QMEWHJlbGF0aXZlXxBEZmlsZTovLy9TeXN0ZW0vTGlicmFyeS9FeHRlbnNpb25LaXQvRXh0ZW5zaW9ucy9pTGlmZVNsaWRlc2hvd3MuYXBwZXgICxIVHgAAAAAAAAEBAAAAAAAAAAUAAAAAAAAAAAAAAAAAAABl' getStarterVariables } function getStarterVariables() { echo "$(date) - Script will only run on macOS version ${desiredmacOSVersion} or later (macOS ${desiredmacOSVersion} - Present)." echo "$(date) - screenSaverBase64: ${screenSaverBase64}" currentRFC3339UTCDate="$(date -u '+%FT%TZ')" echo "$(date) - currentRFC3339UTCDate: ${currentRFC3339UTCDate}" loggedInUser=$(/usr/bin/stat -f%Su /dev/console) echo "$(date) - Logged in user: ${loggedInUser}" macOSFullProductVersion=$(sw_vers -productVersion) echo "$(date) - macOS Full Product Version: ${macOSFullProductVersion}" macOSMainProductVersion="${macOSFullProductVersion:0:2}" echo "$(date) - macOS Main Product Version: ${macOSMainProductVersion}" wallpaperStoreDirectory="/Users/${loggedInUser}/Library/Application Support/com.apple.wallpaper/Store" echo "$(date) - wallpaperStoreDirectory: ${wallpaperStoreDirectory}" wallpaperStoreFile="Index.plist" echo "$(date) - wallpaperStoreFile: ${wallpaperStoreFile}" wallpaperStoreFullPath="${wallpaperStoreDirectory}/${wallpaperStoreFile}" echo "$(date) - wallpaperStoreFullPath: ${wallpaperStoreFullPath}" wallpaperBase64=$(plutil -extract AllSpacesAndDisplays xml1 -o - "${wallpaperStoreFullPath}" | awk '/<data>/,/<\/data>/' | xargs | tr -d " " | tr "<" "\n" | head -2 | tail -1 | cut -c6-) echo "$(date) - wallpaperBase64: ${wallpaperBase64}" wallpaperLocation=$(plutil -extract AllSpacesAndDisplays xml1 -o - "${wallpaperStoreFullPath}" | grep -A 2 "relative" | head -2 | tail -1 | xargs | cut -c9- | rev | cut -c10- | rev) echo "$(date) - wallpaperLocation: ${wallpaperLocation}" wallpaperProvider=$(plutil -extract AllSpacesAndDisplays xml1 -o - "${wallpaperStoreFullPath}" | grep -A 2 "Provider" | head -2 | tail -1 | xargs | cut -c9- | rev | cut -c10- | rev) echo "$(date) - wallpaperProvider: ${wallpaperProvider}" checkmacOSVersion } function checkmacOSVersion() { echo "$(date) - Checking macOS version..." if [[ "${macOSMainProductVersion}" == "" ]]; then echo "$(date) - Could not determine macOSMainProductVersion variable." exitCode='1' finalize elif [[ "${macOSMainProductVersion}" -ge "${desiredmacOSVersion}" ]]; then checkUser else echo "$(date) - macOS is on version $macOSFullProductVersion; do not run." exitCode='0' finalize fi } function checkUser() { echo "$(date) - Checking valid user..." if [[ "${loggedInUser}" == "root" ]]; then echo "$(date) - Script should not be run as root user." exitCode='1' finalize elif [[ "${loggedInUser}" == "" ]]; then echo "$(date) - User cannot be defined." exitCode='1' finalize else setScreenSaverSettings fi } function setScreenSaverSettings() { echo "$(date) - Setting screen saver settings..." if [[ "${wallpaperLocation}" == "" ]]; then # Index.plist contents. aerialDesktopAndScreenSaverSettingsPlist="$(plutil -create xml1 - | plutil -insert 'Desktop' -dictionary -o - - | plutil -insert 'Desktop.Content' -dictionary -o - - | plutil -insert 'Desktop.Content.Choices' -array -o - - | plutil -insert 'Desktop.Content.Choices' -dictionary -append -o - - | plutil -insert 'Desktop.Content.Choices.0.Configuration' -data "${wallpaperBase64}" -o - - | plutil -insert 'Desktop.Content.Choices.0.Files' -array -o - - | plutil -insert 'Desktop.Content.Choices.0.Provider' -string "${wallpaperProvider}" -o - - | plutil -insert 'Desktop.Content.Shuffle' -string '$null' -o - - | plutil -insert 'Desktop.LastSet' -date "${currentRFC3339UTCDate}" -o - - | plutil -insert 'Desktop.LastUse' -date "${currentRFC3339UTCDate}" -o - - | plutil -insert 'Idle' -dictionary -o - - | plutil -insert 'Idle.Content' -dictionary -o - - | plutil -insert 'Idle.Content.Choices' -array -o - - | plutil -insert 'Idle.Content.Choices' -dictionary -append -o - - | plutil -insert 'Idle.Content.Choices.0.Configuration' -data "${screenSaverBase64}" -o - - | plutil -insert 'Idle.Content.Choices.0.Files' -array -o - - | plutil -insert 'Idle.Content.Choices.0.Provider' -string 'com.apple.wallpaper.choice.screen-saver' -o - - | plutil -insert 'Idle.Content.Shuffle' -string '$null' -o - - | plutil -insert 'Idle.LastSet' -date "${currentRFC3339UTCDate}" -o - - | plutil -insert 'Idle.LastUse' -date "${currentRFC3339UTCDate}" -o - - | plutil -insert 'Type' -string 'individual' -o - -)" else # Index.plist contents. aerialDesktopAndScreenSaverSettingsPlist="$(plutil -create xml1 - | plutil -insert 'Desktop' -dictionary -o - - | plutil -insert 'Desktop.Content' -dictionary -o - - | plutil -insert 'Desktop.Content.Choices' -array -o - - | plutil -insert 'Desktop.Content.Choices' -dictionary -append -o - - | plutil -insert 'Desktop.Content.Choices.0.Configuration' -data "${wallpaperBase64}" -o - - | plutil -insert 'Desktop.Content.Choices.0.Files' -array -o - - | plutil -insert 'Desktop.Content.Choices.0.Files' -dictionary -append -o - - | plutil -insert 'Desktop.Content.Choices.0.Files.0.relative' -string "${wallpaperLocation}" -o - - | plutil -insert 'Desktop.Content.Choices.0.Provider' -string "${wallpaperProvider}" -o - - | plutil -insert 'Desktop.Content.Shuffle' -string '$null' -o - - | plutil -insert 'Desktop.LastSet' -date "${currentRFC3339UTCDate}" -o - - | plutil -insert 'Desktop.LastUse' -date "${currentRFC3339UTCDate}" -o - - | plutil -insert 'Idle' -dictionary -o - - | plutil -insert 'Idle.Content' -dictionary -o - - | plutil -insert 'Idle.Content.Choices' -array -o - - | plutil -insert 'Idle.Content.Choices' -dictionary -append -o - - | plutil -insert 'Idle.Content.Choices.0.Configuration' -data "${screenSaverBase64}" -o - - | plutil -insert 'Idle.Content.Choices.0.Files' -array -o - - | plutil -insert 'Idle.Content.Choices.0.Provider' -string 'com.apple.wallpaper.choice.screen-saver' -o - - | plutil -insert 'Idle.Content.Shuffle' -string '$null' -o - - | plutil -insert 'Idle.LastSet' -date "${currentRFC3339UTCDate}" -o - - | plutil -insert 'Idle.LastUse' -date "${currentRFC3339UTCDate}" -o - - | plutil -insert 'Type' -string 'individual' -o - -)" fi makeScreenSaverDirectory } function makeScreenSaverDirectory() { # Create the path to the screen saver/wallpaper Index.plist. echo "$(date) - Creating screen saver directory..." mkdir -p "${wallpaperStoreDirectory}" createIndexPlist } function createIndexPlist() { # Create the Index.plist echo "$(date) - Creating screen saver Index.plist..." plutil -create binary1 - | plutil -insert 'AllSpacesAndDisplays' -xml "${aerialDesktopAndScreenSaverSettingsPlist}" -o - - | plutil -insert 'Displays' -dictionary -o - - | plutil -insert 'Spaces' -dictionary -o - - | plutil -insert 'SystemDefault' -xml "${aerialDesktopAndScreenSaverSettingsPlist}" -o "${wallpaperStoreFullPath}" - killWallpaperAgent } function killWallpaperAgent() { # Kill the wallpaperAgent to refresh and apply the screen saver/wallpaper settings. echo "$(date) - Restarting wallpaper agent..." killall WallpaperAgent exitCode='0' finalize } function finalize() { echo "" if [[ "${exitCode}" == "0" ]]; then echo "$(date) - Mission accomplished!" else echo "$(date) - Abort mission..." exit 1 fi } echo "" getDefinedVariables current_user=$(/usr/sbin/scutil <<< "show State:/Users/ConsoleUser" | awk '/Name :/ && ! /loginwindow/ { print $3 }') current_user_id=$(/usr/bin/id -u "$current_user") echo "Current user is "$current_user"" echo "Current user ID is "$current_user_id"" sleep 10 ## Set key values for screen saver echo "Setting key values for screen saver" sudo -u "$current_user" /usr/bin/defaults -currentHost write com.apple.screensaver CleanExit -string "YES" sudo -u "$current_user" /usr/bin/defaults -currentHost write com.apple.screensaver PrefsVersion -int 100 sudo -u "$current_user" /usr/bin/defaults -currentHost write com.apple.screensaver showClock -string "NO" ## set value to start the screensaver in seconds (now set it to start at 5 minute [start at 299 seconds]) sudo -u "$current_user" /usr/bin/defaults -currentHost write com.apple.screensaver idleTime -int $START_AFTER ## Configure screen saver framework echo "configuring screen saver framework" sudo -u "$current_user" /usr/bin/defaults -currentHost write com.apple.screensaver moduleDict -dict moduleName -string "iLifeSlideshows" path -string "/System/Library/ExtensionKit/Extensions/iLifeSlideshows.appex" type -int 0 ## Additional configuration settings for screen saver framework echo "configuring screen saver settings" sudo -u "$current_user" defaults -currentHost write com.apple.screensaver tokenRemovalAction -int 0 echo "setting asset path" sudo -u "$current_user" defaults -currentHost write com.apple.ScreenSaverPhotoChooser SelectedFolderPath -string "$FULL_PATH_TO_FOLDER" echo "setting shuffle settings" sudo -u "$current_user" defaults -currentHost write com.apple.ScreenSaverPhotoChooser SelectedSource -int 4 sudo -u "$current_user" defaults -currentHost write com.apple.ScreenSaverPhotoChooser ShufflesPhotos -bool "true" sudo -u "$current_user" defaults -currentHost write com.apple.ScreenSaverPhotoChooser CustomFolderDict -dict identifier -string "$FULL_PATH_TO_FOLDER" name -string "$FOLDER_NAME" sudo -u "$current_user" defaults -currentHost write com.apple.ScreenSaver.iLifeSlideShows styleKey -string "Classic" ## Reset prefsd echo "relaunching prefsd..." /usr/bin/killall -hup cfprefsd |
Style
– This field takes into consideration the style of the slideshow to be deployed. Provide the name of the style you would like to set on the device. Apple provides a bunch of options you can choose from:
- Floating
- Flip-up
- Reflections
- Origami
- Shifting Tiles
- Sliding Panels
- Photo Mobile
- Holiday Mobile
- Photo Wall
- Vintage Prints
- Ken Burns
- Classic
IdleTime
– Provide the inactivity period (in seconds) after which the screen saver should activate. The value ‘0’ sets the screen saver to be never activated.
ShufflePhotos
– Provide ‘true’ or ‘false’ based on whether you want the images for the slideshow to be shuffled. If the value is set as ‘false’, the images will be displayed in the order they appear in the source folder.
Photosfolderpath
– Provide the path to the folder that contains the images for the slideshow as an argument. For example, to deploy a slideshow screen saver for images stored at ‘/Users/jacob/Desktop/ScreenSavers’, specify it in the Arguments field while executing the action.