How to toggle invisible files in OSX

OS X has numerous invisible files and folders. They are hidden to protect users from mistakenly moving or deleting critical files that might break the system or the applications that depend on them. Sometimes however you may need to find and use these files but OS X unlike its Windows counterpart has no options to show/hide hidden files anywhere. There are options, third party software is one and here a few others.

The Finder

If you need to find a hidden file for a one off situation you can use the search criteria in Finder. Launch the Finder (Command+F ) and select File → Find… to reveal the filter options. Select Kind → Other… to open a dialog sheet listing search attributes and select Visibility (if you are running Snow Leopard the attribute has been renamed to File invisible) and click OK. You should now be able to select either visible or invisible as a search criterion.

Terminal

For those comfortable using the command line we can set Finder to display all the hidden files, then restart it to apply those settings.

defaults write com.apple.finder AppleShowAllFiles TRUE
killall Finder

To hide the hidden files again simply replace TRUE with FALSE in the first line and run the commands again. This way is fine however it requires you to remember (or lookup) the commands and type them in each time. There must be an easier way? Well there is and this is to create your own application. Don’t worry its not as hard as it sounds.

AppleScript

All you need to do is copy this little script and save it as an application then you can run the application to toggle the hidden/visible states.

The below script is a combination of trial and error from various other scripts from various forums on the internet. Copy the AppleScript below and in the Finder menu, select Finder ? Services ? Script Editor ? Make a New AppleScript. Paste in the AppleScript code and save. Choose a name you’ll remember e.g. “Toggle Hidden Files” and a location e.g. “Applications Folder” and set the File Format to application.

set onOff to do shell script "defaults read com.apple.finder AppleShowAllFiles"
if onOff = "NO" or onOff = "OFF" then
  set newState to "show"
  set OnOffCommand to "defaults write com.apple.finder AppleShowAllFiles ON"
else
  set newState to "hide"
  set OnOffCommand to "defaults write com.apple.finder AppleShowAllFiles OFF"
end if

display dialog "Are you sure you want to " & newState & " hidden files? (This will restart the Finder)" buttons {"Cancel", "OK"} default button 2
copy result as list to {buttonPressed}
if buttonPressed is "OK" then
  try
    tell application "Finder" to quit
    do shell script OnOffCommand
    delay 1
    tell application "Finder" to launch
  end try
end if

Running the script will prompt you to confirm then restart the Finder to show the invisible files. Run the script again to toggle back to hidden.

If you need to show/hide hidden items regularly then you can assign a keyboard shortcut to the application using then keyboard shortcut menu in System Preferences.

Update for Snow Leopard 10.6 Users

Snow Leopard 10.6 users will notice Apple has changed things around a bit so here are revised instructions. Open Applications ? Utilities ? AppleScript Editor.app. Copy the AppleScript below then paste in the AppleScript Editor window and save. Choose a name you’ll remember e.g. “Toggle Hidden Files” and a location e.g. “Applications Folder” and set the File Format to application.

try
  do shell script "defaults read com.apple.Finder AppleShowAllFiles"
  get result as integer
on error
  get 0
end try

set i to (result + 1) mod 2—Get opposite of current value

tell application "Finder"
  activate
  get item (i + 1) of {"Hide", "Show"}
  display dialog ("Are you sure you want to " & result & " hidden files? (This will restart the Finder)") buttons {"Cancel", "OK"} default button 2 with icon note
  quit
end tell

try
  do shell script "defaults write com.apple.Finder AppleShowAllFiles " & i
  launch application "Finder"
on error errorMsg number errorNum
  display dialog "Error (" & errorNum & "):" & return & return & errorMsg buttons "Cancel" default button 1 with icon caution
end try

The original script is a repost from an article I wrote a few years ago. One common problem people have experienced was where “read com.apple.Finder AppleShowAllFiles” returns the error “The domain/default pair of (com.apple.Finder, AppleShowAllFiles) does not exist”.  This error is returned since the plist file com.apple.Finder (at ~/Library/Preferences) doesn’t normally contain a sibling named AppleShowAllFiles, but issuing the command manually will insert the AppleShowAllFiles sibling into com.apple.finder.plist, thus resolving the problem.

The revised Snow Leopard script handles the condition where “read com.apple.Finder AppleShowAllFiles” returns the “does not exist” error, by using first trying to get a result and if the error occurs then assumes it is in it’s default state. If the script does not seem to work at first try running it a second time and it will then normally work from then on.

As a final thought an alternative is to use Automator.app to create a workflow to execute the terminal commands. You will need to create a show workflow and a hide workflow instead of toggling between the two states and therefore it is not as good an option as a simple AppleScript application.

This article was posted on 15/01/10 in Apple, Code

Save a massive 36% on a subscription to .net magazine!

We love .net magazine (known as "Practical Web Design" outside the UK) and have teamed up with Future Publishing to get our readers up to a massive 36% off a years subscription.

comments

add comment

What you have had to say about all this...

Thanks.  This is awesome.  There is one thing for the 10.6 version which is not that big of a deal.  It doesn’t seem to relaunch finder by itself.  I have to manually relauch finder.

- Jay
x
Step 1. : Login details

about you.

Step 2. : Password

your say.

Step 3. : Email details

preferences.


That's the end of this article. I hope you found it useful. If you're enjoyed this article why don't you have a look around the archives, where you can find some more tutorials, tips and general ramblings.