Banish Mac OS X Resource Forks from FAT32 volumes

I bought a digital photo frame for Mum’s birthday. The first thing I encountered while getting it set up for her was that Mac OS X Resource Forks seriously confuse it. Resulting in every other “picture” being unreadable and rather than skipping past them, it would display a grey screen with an error in the middle for the allotted time (well done genius!).

As usual I discovered this problem while demoing it to her! How embarrassing! “Give me five minutes.” I said. I knew exactly what needed to be done. I needed a way to automatically delete all the resource fork files from the FAT32 SD card. I figured someone else would have done it already, and they probably have, but I couldn’t find it. I guess my GoogleFu was weak that day. Many hours later, I cracked it. So much for five minutes!

Mac OS X has a nifty feature called “Folder Actions”. What this does is allow you to execute scripts when the contents of a folder change. When files are added to the SD card, I want to run the command:

find . -name "._*" -exec rm -rf {} \;

If you open a Terminal window and run that command, it will find all the files that begin with “._” and execute the “rm -rf” command on each one. Resource forks gone.

If you test that command in the Terminal then be sure that you are in your FAT32 volume. It won’t do anything on a HFS volume. If you remove the “-exec rm -rf {} \;” from the end of the command you can see the results of the find command, the files it’ll be deleting. Nothing is returned on HFS volumes, which is exactly what should happen.

I had hoped that I could just stick that command in a Bash shell script and attach that as a Folder Action, but I ended up having to do it through Automator. Which isn’t as straightforward as I’d hoped. In order to do that the command needs to be modified slightly so that it will still work when invoked by Automator. That’s the bit I found time consuming, given that this is the first time I’d even looked at Automator!

The final command we’ll be using is:

find "`dirname "$@"`" -name "._*" -exec rm -rf {} \;

From what I understand, the script will be run once for every file in the folder and Automator will pass the full path to each file in the folder to the command by means of the $@ variable. We use the dirname command to get the folder path from the file path. So /Volumes/SDCARD/DSC0001.JPG would become /Volumes/SDCARD. Then the command can run as normal.

This does mean that the command gets run more times than is nessessary. But to be honest, I don’t care. It’s only going to run when the contents of the card are being changed, which won’t be that often and it’s not as if anyone’ll notice!

Now, I find the integration between Automator and Folder Actions, well, it’s not great. I’m pretty clued up on most things computery, I’m a programmer! But even I find it confusing.

Open Automator and create a new workflow from the “Folder Action” template. At the top of the workflow, you need to select the volume that you want the script attached to. Next you want to add the “Run Shell Script” action. Make sure that “Pass Input” is set to “as arguments” and enter the final command above into the Automator action. Save it and you should be done. Check to be sure by control-clicking on the volume icon in Finder and select “Folder Action Setup”. A small window will appear and drop a sheet in front. Dismiss the sheet and verify that your volume name is in the left column, click it, and check that your folder action is present in the right column. If it isn’t, click the little plus button at the bottom of the right column to get that sheet back and select your folder action from the list. If your volume doesn’t appear in the left column, you can add it there using the plus button at the bottom of that column.

Once that’s all done, the resource forks will be deleted automatically, even after you eject the volume and remount it, it’ll still work.