Show hidden file toggle works with "who(ami)" but not with "id -un"

miguelscopely
New Contributor

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

!/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

MODIFIED SCRIPT

!/bin/bash

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 ?

8 REPLIES 8

gskibum
Contributor III

Did you ever get anywhere with this?

Everything I have tried to modify AppleShowAllFiles from Self Service accomplishes nothing more than the Finder restart.

ryan_ball
Valued Contributor

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 + "
");')

gskibum
Contributor III

@ryan.ball

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

ryan_ball
Valued Contributor

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

gskibum
Contributor III

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. :-)

gskibum
Contributor III

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

I found it here

gskibum
Contributor III

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.

ryan_ball
Valued Contributor

Do it like this:

sudo -s -u "$CurrentUser" defaults write "/Users/$CurrentUser/Library/Preferences/com.apple.finder.plist" AppleShowAllFiles -bool true