Toggle Finder Hidden File Status

May 1, 2008

For some reason I prefer to have system files (hidden by default) shown in Finder (must be the Unix in my system from grad school). With that said, there are times when it would be nice to have these same files out of sight when working with a cluttered folder. After having thought many times about writing a short script to toggle hidden files on/off, I finally took a few minutes to crank out the code below:

Script Editor Click to paste code into the Script Editor

----------------------------------------
--  MacDeveloperTips.com
--  Toggle Finder hidden file status
----------------------------------------
try
  -- Get current value
  set toggle to do shell script "defaults read com.apple.finder AppleShowAllFiles"

  -- Toggle it
  if toggle = "ON" then
    do shell script "defaults write com.apple.finder AppleShowAllFiles OFF"
  else
    do shell script "defaults write com.apple.finder AppleShowAllFiles ON
  end if

  -- Restart Finder
  tell application "Finder" to quit
  delay 0.5 -- If you have problems, you can tweak the delay
  tell application "Finder" to launch
on error

  display dialog "Unable to toggle bit status." buttons {"Better luck next time"} 
    with icon caution with title "Error"
end try

Notice the reference to delay – if you have problems with the scripting not running properly, you can experiment with a longer delay. There is minimal error handling, essentially just trapping errors and displaying the message below:

I prefer to save AppleScript code as an application, create an icon that serves as a reminder of what the script does, and drag/drop the application onto the Finder toolbar. You can see a screenshot below what my toolbar looks like (the icon for this script is the blue & white Finder image).

And speaking of icons, in an upcoming tip I’ll show you how easy it is to change an application icon on a Mac.

6 comments

Awesome thanks for the script. Just a little note this will not work unless you have previously set finder to show hidden files. Just FYI. Strange. I guess it wont work if the default ShowAllFiles was never set.

by grndlvl on May 1, 2008 at 5:48 pm. Reply #

Total newbie to Mac (although I bought the 1st fishbowl, way back then).

Where do I find instructions on how to create an icon for an application. Currently, my Mac shows the hidden files so your script comes in handy.

Thank

by bbiker on May 10, 2008 at 10:24 pm. Reply #

If you install the Mac developer tools (ie, Xcode), there is an application in the Applications/Utilities folder called Icon Composer. You can also use other programs such as photoshop, fireworks, etc. I imagine a search on google would turn up a long list of tutorials on how to create an icon. This is a good idea for a future post…

If you prefer, you can copy an icon from any other Mac app: http://macdevelopertips.com/system/changing-an-application-icon.html

by john on May 10, 2008 at 10:33 pm. Reply #

You can also use

do shell script “killall Finder”

instead of the delay method above as this automatically restarts finder for you. Much cleaner and no delay needed.

by Mark A. Donohoe on Jun 5, 2008 at 12:50 am. Reply #

Thanks for this. Taking the suggested modifications from above and including error handling so it works even when the defaults variable is not defined:

try
– Get current value
set toggle to do shell script “defaults read com.apple.finder AppleShowAllFiles”

– Toggle it
if toggle = “YES” or toggle = “ON” then
do shell script “defaults write com.apple.finder AppleShowAllFiles NO”
else
do shell script “defaults write com.apple.finder AppleShowAllFiles YES”
end if

– Restart Finder
do shell script “killall Finder”

on error

– Error usually because “AppleShowAllFiles” is not defined
do shell script “defaults write com.apple.finder AppleShowAllFiles YES”

– Restart Finder
do shell script “killall Finder”

end try

by Dalmazio on May 12, 2009 at 12:09 am. Reply #

I created an application for simple minded people like me based on this code and included error handling as suggeested by Dalmazio. Find it here http://shoebox.kobie.nl/HiddenFiles/SwitchHiddenFiles.zip

It has no interface, when you start it, it just makes the switch and exits.

by Richard on Jun 18, 2009 at 3:54 am. Reply #

Post a comment:

Required.

Required. Not published.