Toggle Hidden Files In OS X Finder With Automator Workflows

Posted on: by

Occasionally, I need to send a file to someone that may be one of the dotfiles, a common one is a sites .htaccess file. By default, if a file or directory starts with a dot in OS X it is not displayed in the finder. So how do I drag it into an email?

Well, a simple way is to make a copy of the file without the dot. But that means I have to go into a terminal and make a copy of the file and then remove it when I am done. It works, but it isn’t very efficient.

I wanted an easier way to handle this than that.

It turns out that you can toggle a setting on and off in OS X that will display hidden files in Finder.

    defaults write com.apple.finder AppleShowAllFiles YES

After typing that command into a terminal window you need to restart Finder for it to take effect. But there is no way I am going to remember this for the odd time when I need to display hidden files. I needed a shorter way.

What I really want is a keyboard shortcut that would allow me to toggle hidden files on and off with a keyboard command. My plan is to toggle hidden files on and off using super+h (I have remapped my cap lock key to be a super key – see A useful Caps Lock key by Brett Terpstra).

To do this we need to complete the following steps:

  1. Write a shell script to toggle AppleShowAllFiles on and off.
  2. Create an Automator Service to run the shell script
  3. Map the keyboard shortcut to the newly created service.

Creating the shell script is pretty straight forward:

#!/usr/bin/env bash

SHOWDOTFILES=$(defaults read com.apple.finder AppleShowAllFiles)

if [ $SHOWDOTFILES == "NO" ]
    echo "Toggle dotfiles on"
    $(defaults write com.apple.finder AppleShowAllFiles YES; killall -HUP Finder)
    echo "Toggle dotfiles off"
    $(defaults write com.apple.finder AppleShowAllFiles NO; killall -HUP Finder)

Now we have to create an Automator action so that we can attach the script to the services menu, and hence a keyboard shortcut.

Open Automator and choose Service as the document type:

Adjust the service parameters:

Search for the Run Shell Script action:

Add your script name and path:

Save the Automator workflow. When you save the workflow it is placed in ~/Library/Services.

Now open System Preferences > Keyboard > Shortcuts, locate services in the first column and then the name of your Automator service. From here you add your keyboard shortcut and test it out.