Hide Hidden Files Script

ehorn99
New Contributor

Goal : Create a script to hide hidden files to be delivered by a policy

Attempt: Created a text files and entered the following

defaults write com.apple.finder AppleShowAllFiles FALSE
killall Finder

Renamed to .sh

Does not seem to take. What am I missing?

5 REPLIES 5

jarednichols
Honored Contributor

I'm guessing the hidden flag needs to be reapplied to the files. Man chflags and that should get you in the right direction.

rlandgraf
Contributor
#!/bin/sh

CurrentUser=`who | grep "console" | awk '{print $1}'`

sudo -u $CurrentUser defaults write com.apple.finder AppleShowAllFiles FALSE
killall Finder

This is the script I wrote for this I think you need to do it per user as the script is run as root. Ours is in self service for certain people, but if you set it as a log in policy it should work.

ehorn99
New Contributor

rlandgraf: Thanks for the script. Works great for Self-Service and through a policy. The drawback as you described is that it only work on the current user instead of the computer as a whole. Anybody have a thought on how to perform the latter?

mm2270
Legendary Contributor III

Just change it to point at the root /Library/Preferences/ folder:

#!/bin/sh
defaults write /Library/Preferences/com.apple.finder AppleShowAllFiles FALSE
killall Finder

Although the com.apple.finder.plist file doesn't exist normally in the /Library/Preferences/ path, the above command creates a new one with that single setting in it. As long as its running as root, it will be able to create the file.

If you don't specify a proper path, just using "com.apple.finder" (or whatever plist you're trying to write to) will only apply to the account actually running the command. In the case of a Casper Suite policy, that would be root. If you do it as yourself while logged in, it looks to the file located at ~/Library/Preferences/

I'm not 100% certain if the above command pointed at /Library/Preferences/ then applies to all accounts, but in my tests, restarting Finder will show me invisibles if you change the above to "TRUE"

Last thing to keep in mind is that, unless you convert the above into either a System Level Enforced MCX or put it into a profile, an individual user's com.apple.finder.plist can potentially contain an AppleShowAllFiles TRUE setting that would override the above "FALSE" one you've placed in the root /Library/Preferences/ folder. If there is no setting in their plist, it uses the one in the above location, otherwise, it will use whatever is in their local plist.

Does that make sense?

tangerinehuge
New Contributor III

If anyone is interested I used those commands to create a script to toggle hidden files from Self Service:

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