Having trouble getting a script to run as the logged in user instead of root. I've searched here pretty thoroughly but can't find a working solution.
The script checks the screen saver idle time of the logged in user; if less than 10 minutes it does nothing. If more than 10, it sets it to 10.
It works fine when run as the user but I can't get it to work when run by root / casper.
Here's what I've got so far. I've had luck using this $currentuser string with extension attributes but no luck here.
#!/bin/bash
# Max idle time for the screensaver in seconds.
idleMax=600
idleTime=$(/usr/bin/defaults -currentHost read com.apple.screensaver idleTime 2> /dev/null)
currentuser=$(stat -f '%Su' /dev/console | /usr/bin/cut -d ' ' -f 4)
if [ $? -eq 1 ]; then
defaults -currentHost write com.apple.screensaver idleTime -int ${idleMax}
elif [[ ${idleTime} -gt ${idleMax} || ${idleTime} -eq 0 ]]; then
sudo -ui $currentuser defaults -currentHost write com.apple.screensaver idleTime -int ${idleMax}
echo "screensaver time outside of scope; changed to 10 minutes"
fi