Set Screensaver using a script

tfenna
New Contributor II

Hi all,

I'm aware similar posts have been made before but none seemed to work for the way I wanted to do this so hopefully I can help out some others too.

A while back I was tasked with setting our corporate screensaver on all of our Mac's, when first asked about this I though it would be pretty simple, turns out it's not quite so easy as you may have found already!

I finally got around to putting a script together the other day to do this, we have around 10 different images that we use for our screensavers so this will work with either a photo library or just a single image.

I've set the script to copy our screensavers folder from an SMB share into the /Screen Savers/Default Collections folder on the Mac however this could be removed if you already have the images there or edited to pull from another location.

I have then taken the screensaver plist files from my Mac with the desired settings set such as idle time before it kicks in, screensaver theme, source and so on. You could also move the plist files elsewhere and edit via Xcode. You MUST make sure you copy and distribute all three screensaver plist files even if you don't think you need them all, I found that when I didn't they overwrote each other back to the previous settings.

Anyways, I could go into it more but here's the script, take a look, see what you think, apologies if it's a little rough around the edges... I'm by no means a programmer!
Any feedback/improvements much appreciated.

#!/bin/bash

  # -----------------------------------------------------------
  # Created by Toby Fenna

  # README:
  # If the script is failing to execute, check that the server IP and path are correct

  # -----------------------------------------------------------


  # The below line sets the variable for the currently logged in user - uncomment the below and change $3 to $Current_User if you wish to run this script locally
  # Current_User=$(stat -f %Su "/dev/console")

  # Sets the variable to retrieve the Macs UUID
  macUUID=`ioreg -rd1 -c IOPlatformExpertDevice | grep -i "UUID" | cut -c27-62`

          # Mounts the network drive on the logged in users account authenticating via a sys account (using AppleScript)
          sudo -u $3 osascript -e 'mount volume "smb://userName:Password@pathToShare"'

                # Remove the current plist files
                rm /Users/$3/Library/Preferences/ByHost/com.apple.screensaver.$macUUID.plist
                rm /Users/$3/Library/Preferences/ByHost/com.apple.ScreenSaverPhotoChooser.$macUUID.plist
                rm /Users/$3/Library/Preferences/ByHost/com.apple.ScreenSaver.iLifeSlideShows.$macUUID.plist

                     # Copy the screen saver folder from SMB share to Default Collections & modified plist files to the ByHost folder - /volumes/apps... is just the path on my SMB share so this will be different for you
                     cp -r /Volumes/apps/Screensaver/SlideShow /Library/Screen Savers/Default Collections
                     cp /Volumes/apps/Screensaver/Plist files for mac screen saver/com.apple.screensaver.xxx.plist /Users/$3/Library/Preferences/ByHost/com.apple.screensaver.$macUUID.plist
                     cp /Volumes/apps/Screensaver/Plist files for mac screen saver/com.apple.ScreenSaver.iLifeSlideShows.xxx.plist /Users/$3/Library/Preferences/ByHost/com.apple.ScreenSaver.iLifeSlideShows.$macUUID.plist
                     cp /Volumes/apps/Screensaver/Plist files for mac screen saver/com.apple.ScreenSaverPhotoChooser.xxx.plist /Users/$3/Library/Preferences/ByHost/com.apple.ScreenSaverPhotoChooser.$macUUID.plist

                           # Gives the logged on user access to the SlideShow folder & plist files
                           chmod -R 755 /Library/Screen Savers/Default Collections/SlideShow
                           chmod 755 /Users/$3/Library/Preferences/ByHost/com.apple.screensaver.$macUUID.plist
                           chmod 755 /Users/$3/Library/Preferences/ByHost/com.apple.ScreenSaverPhotoChooser.$macUUID.plist
                           chmod 755 /Users/$3/Library/Preferences/ByHost/com.apple.ScreenSaver.iLifeSlideShows.$macUUID.plist

                                # Sets sleep to 11 minutes & displaysleep to 10 - inside my plist file I have the idle time set to 300 seconds, equal to 5 minutes
                            pmset sleep 11
                                pmset displaysleep 10

        umount /Volumes/apps

    killall cfprefsd

Credit to @Look for help with mounting the SMB share using osascript

Thanks!

1 REPLY 1

Captainamerica
Contributor

Hi,

Just trying to play a little arround with your script. Is there a way to mount a smb share with current user credentials and password that are already logged in on the mac, so it don´t prompt for the credentials