Posted on 09-22-2015 12:27 AM
I ran into trouble setting up a script to toggle show/hide hidden folders because of a user(_xcsbuildd) that xcode server has running. The script ran fine on computers without xcode server, but failed on the computers with the server. The script wouldn't function properly because 'who' would return _xcsbuildd as $CurrentUser. I tried substituting 'id -un' instead of 'who' and the modified script ran flawlessly straight from the computer, but failed once I ran it through Self Service.
ORIGINAL SCRIPT
CurrentUser=who | grep "console" | awk '{print $1}'
STATUS=sudo -u $CurrentUser defaults read com.apple.finder AppleShowAllFiles
if [ $STATUS = TRUE ];
then
sudo -u $CurrentUser defaults write com.apple.finder AppleShowAllFiles FALSE
else**
sudo -u $CurrentUser defaults write com.apple.finder AppleShowAllFiles TRUE
fi
killall Finder
MODIFIED SCRIPT
CurrentUser=id -un | awk '{print $1}'
STATUS=sudo -u $CurrentUser defaults read com.apple.finder AppleShowAllFiles
if [ $STATUS = TRUE ];
then
sudo -u $CurrentUser defaults write com.apple.finder AppleShowAllFiles FALSE
else
sudo -u $CurrentUser defaults write com.apple.finder AppleShowAllFiles TRUE
fi
killall Finder
Can anyone tell me why It ran fine on the computer, but not when deployed via Self Service Policy ?
Posted on 04-09-2019 07:26 AM
Did you ever get anywhere with this?
Everything I have tried to modify AppleShowAllFiles from Self Service accomplishes nothing more than the Finder restart.
Posted on 04-09-2019 07:29 AM
This is a reliable way to determine the currently logged in user:
CurrentUser=$(/usr/bin/python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "
");')
Posted on 04-09-2019 07:33 AM
That's exactly what I always use to collect the current user.
This is what I was testing before I started adding if statements to make it toggle.
It works from BBEdit, but not from Self Service.
#!/bin/bash
CurrentUser=`python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "
");'`
defaults write /Users/$CurrentUser/Library/Preferences/com.apple.finder AppleShowAllFiles -bool true
/bin/sleep 2
killall Finder
exit 0
Posted on 04-09-2019 07:37 AM
Just to be sure, try it like this:
#!/bin/bash
CurrentUser=`python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "
");'`
/usr/bin/defaults write "/Users/$CurrentUser/Library/Preferences/com.apple.finder.plist" AppleShowAllFiles -bool true
/bin/sleep 2
killall Finder
exit 0
Posted on 04-09-2019 07:49 AM
Thank you for your help. That gave the same result.
The end result with all my attempts to switch AppleShowAllFiles is a com.apple.finder.plist file that is set back to factory default.
I normally use /usr/bin/defaults write. Removed it last night when trying to wrap my head around what's going on. :-)
Posted on 04-09-2019 08:02 AM
Interestingly, this one is working, but only if I keep the
sudo -u
If I remove it it's back to failing.
#!/bin/bash
CurrentUser=`who | grep "console" | awk '{print $1}'`
STATUS=`sudo -u $CurrentUser defaults read com.apple.finder AppleShowAllFiles`
if [ $STATUS == TRUE ];
then
sudo -u $CurrentUser defaults write com.apple.finder AppleShowAllFiles FALSE
else
sudo -u $CurrentUser defaults write com.apple.finder AppleShowAllFiles TRUE
fi
killall Finder
Posted on 04-09-2019 08:10 AM
Adding the su -u to
su -u /usr/bin/defaults write "/Users/$CurrentUser/Library/Preferences/com.apple.finder.plist" AppleShowAllFiles -bool true
does not work.
Posted on 04-09-2019 08:15 AM
Do it like this:
sudo -s -u "$CurrentUser" defaults write "/Users/$CurrentUser/Library/Preferences/com.apple.finder.plist" AppleShowAllFiles -bool true