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.
I've already shown you how to use Terminal to show or hide files and folders. That method is pretty good if you only have an occasional need to work with the hidden files and folders on your Mac. But there's a better way if you tend to work frequently with your Mac's hidden goodies.
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 com.apple.finder AppleShowAllFiles` if [ $STATUS == TRUE ]; then defaults write com.apple.finder AppleShowAllFiles FALSE else defaults write com.apple.finder AppleShowAllFiles TRUE fi killall 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. 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
- Launch Automator, located in the /Applications folder.
- Select Service as the type of template to use for your new Automator task, and click the Choose button.
- 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.
- In the filtered list of actions, click Run Shell Script and drag it to the workflow pane.
- 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.'
- Copy the entire shell script command that we created above (all eight lines), and use it to replace any text that may already be present in the Run Shell Script box.
- From the Automator file menu, select "Save," and then give the service a name. The name you select will appear as the menu item. I call mine Toggle Hidden Files.
- After saving the Automator service, you can quit Automator.
Using the Toggle Hidden Files Menu Item
- Open a Finder window.
- Right-click any file or folder.
- Select Services, Toggle Hidden Files, from the pop-up menu.
- The Finder will toggle the state of hiding files, causing hidden files and folders to display or be hidden depending on their current condition.