Running commands at the local user

New Contributor II

Hi All

I'm trying to run a command to set a screen saver option as the current logged in user through a script but it's failing... I have tried looking around for ways to run as the local user instead of the root user used by Jamf and I can get it to add "/Library/Screen" the local user plist file but not the full location "/Library/Screen Savers/Default Collections/5 - Company", can any one shed some light on where i'm going wrong, I'm going square eyed looking at this over and over

My script is:


currentuser=$(/bin/ls -la /dev/console | /usr/bin/cut -d ' ' -f 4)
su -l $currentuser -c "defaults write /Users/$currentuser/Library/Preferences/ SelectedFolderPath '/Library/Screen Savers/Default Collections/5 - Company'"

Any help is very much appreciated....


New Contributor III
New Contributor III

Here's the script I've used previously for setting the Screensaver. It works great, there's some additional options in there for things like idle time, so just modify it based on your requirements.


# Get user logged into console and put into variable "user"
user=`ls -l /dev/console | cut -d " " -f 4`
osMajor=$(sw_vers -productVersion | awk -F"." '{print $2}')
osMinor=$(sw_vers -productVersion | awk -F"." '{print $3}')

sudo -u $user defaults -currentHost write CleanExit -string "YES"
sudo -u $user defaults -currentHost write PrefsVersion -int 100
sudo -u $user defaults -currentHost write showClock -string "NO"
sudo -u $user defaults -currentHost write idleTime -int 600

if [[ $osMajor -eq 14 ]] && [[ $osMinor -ge 2 ]]; then

sudo -u $user defaults -currentHost write moduleDict -dict moduleName -string "iLifeSlideshows" path -string "/System/Library/Frameworks/ScreenSaver.framework/PlugIns/iLifeSlideshows.appex" type -int 0


sudo -u $user defaults -currentHost write moduleDict -dict moduleName -string "iLifeSlideshows" path -string "/System/Library/Frameworks/ScreenSaver.framework/Resources/iLifeSlideshows.saver" type -int 0

sudo -u $user defaults -currentHost write tokenRemovalAction -int 0

sudo -u $user defaults -currentHost write SelectedFolderPath -string "/Library/Application Support/JAMF/bin/screensaver"
sudo -u $user defaults -currentHost write SelectedSource -int 3

sudo -u $user defaults -currentHost write styleKey -string "Classic"

sudo killall -hup cfprefsd