Create a Menu Item to Hide and Show Hidden Files in OS X

Use Automator to create a contextual menu to hide or show hidden files

Contextual Menu to Hide or Show Hidden FIles

By default, the Mac hides many system files that you may at some point need to access. Apple hides these files because an accidental change to, or the outright removal of the files could cause problems for your Mac.

By combining the Terminal commands for showing and hiding files and folders with Automator to create a service that can be accessed from contextual menus, you can create a simple menu item to show or hide those files.

Creating the Shell Script to Toggle Hidden Files

We already know the two Terminal commands that are needed to either show or hide hidden files. What we need to do is create a shell script that will toggle between the two commands, depending on whether we want to show or hide the files in the Finder.

First, we need to determine whether the current state of the Finder is to show or hide hidden files; then we need to issue the appropriate command to change to the opposite state. To do this, we will use the following shell commands:

STATUS=`defaults read AppleShowAllFiles`if [ $STATUS == 1 ]then defaults write AppleShowAllFiles -boolean FALSEelse defaults write AppleShowAllFiles -boolean TRUEfikillall Finder 

That's a pretty basic shell script that will do the job for us. It starts by asking the Finder what the current state of the AppleShowAllFiles is set to and then storing the results in a variable called STATUS.

The variable STATUS is then checked to see if it is TRUE (the number one is equivalent to TRUE). If it is TRUE (set to hide files and folders), then we issue the command to set the value to FALSE. Likewise, if it's FALSE (set to show files and folders), we set the value to TRUE. In this way, we've created a script that will toggle the Finder's hiding of files and folders on or off.

While the script is somewhat useful by itself, its real value comes when we use Automator to wrap around the script and create a menu item that will let us turn hidden files and folders on or off with just a mouse click.

Using Automator to Create a Toggle Hidden Files Menu Item

  1. Launch Automator, located in the Applications folder.
  2. Select Service as the type of template to use for your new Automator task, and click the Choose button.
  3. In the Library pane, make sure Actions is selected, then underneath the Library item, click Utilities. This will filter the available workflow types to just those relating to utilities.
  4. In the filtered list of actions, click Run Shell Script and drag it to the workflow pane.
  5. At the top of the workflow pane are two drop-down menu items. Set the 'Service receives selected' to 'files or folders.' Set the 'in' to 'Finder,' respectively.
  6. Copy the entire shell script command that we created above (all six lines), and use it to replace any text that may already be present in the Run Shell Script box.
  7. From the Automator file menu, select Save, and then give the service a name. The name you select will appear as the menu item. We call ours Toggle Hidden Files.
  8. After saving the Automator service, you can quit Automator.

Using the Toggle Hidden Files Menu Item

  1. Open a Finder window.
  2. Right-click any file or folder.
  3. Select Services > Toggle Hidden Files, from the pop-up menu.
  4. The Finder will toggle the state of hiding files, causing hidden files and folders to display or be hidden depending on their current condition.