Toggle Finder Hidden File Status

Thu, May 1

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

--  Toggle Finder hidden file status
  -- Get current value
  set toggle to do shell script "defaults read AppleShowAllFiles"

  -- Toggle it
  if toggle = "ON" then
    do shell script "defaults write AppleShowAllFiles OFF"
    do shell script "defaults write 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.


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. #

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.


by bbiker on May 10, 2008. #

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:

by john on May 10, 2008. #

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. #

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

— Get current value
set toggle to do shell script “defaults read AppleShowAllFiles”

— Toggle it
if toggle = “YES” or toggle = “ON” then
do shell script “defaults write AppleShowAllFiles NO”
do shell script “defaults write 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 AppleShowAllFiles YES”

— Restart Finder
do shell script “killall Finder”

end try

by Dalmazio on May 12, 2009. #

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

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

by Richard on Jun 18, 2009. #