I got this to work on Sierra 10.12.3 in Automator.
First, I made this version of the script:
It functions the same way as freefly42's, just a different way of writing the same thing.
To translate for non-native bash speakers, what it does is:
If run with a commandline argument, it tries to see if the argument is a directory, and if that directory contains a file named ".disable_osx_metadata".
If the special file is not there, then nothing happens. That drive is ignored.
If the special file is there, the script deletes all the osx metadata and creates a few small new items which prevents anything else. For instance, creating a file named .Trashes prevents the OS from creating a directory named .Trashes and then writing files in there.
If run with no arguments, it runs itself once for each directory in /Volumes.
So, if you run it manually or click on it, it checks all attached drives.
If you create a workflow in Automator, then Automator runs it whenever a drive is attached, just for that drive.
#!/bin/bash
x=.disable_osx_metadata
[[ "$1" ]] || exec find /Volumes -type d -maxdepth 1 -mindepth 1 -exec $0 {} \;
[[ -e "$1/$x" ]] || exit 0
mdutil -i off "$1"
rm -rf "$1"/.{,_.}{fseventsd,Spotlight-V*,Trashes}
mkdir "$1/.fseventsd"
touch "$1/.fseventsd/no_log" "$1/.Trashes" "$1/$x"
Save as "disable_osx_metadata", chmod 755, copy to /usr/local/bin .
$ cd Documents/disable_osx_metadata/
$ chmod 755 disable_osx_metadata
$ sudo cp disable_osx_metadata /usr/local/bin
Then open Automator, New, Folder Action
Then I found that even in Sierra 10.12.3 you CAN still add the /Volumes directory in Automator, by a non-obvious way.
Go to Finder, File, "Go To Folder...", manually write in "/Volumes" and hit Enter.
Now this shows you a window with your HD and usb drives, but no obvious "/Volumes" folder to click on.
But the title bar says "/Volumes", and it appears at the bottom too.
You can drag the folder icon next to "/Volumes", either from the title bar or from the bottom, over to the Automator, and drop it on "Folder action receives files and folders added to:[______]"
Close Finder.
Back in Automator: on the left, scroll down and drag "Get Folder Contents" to the right and drop it.
Then on the left again, scroll down and drag "Run Shell Script" to the right and drop it below "Get Folder Contents".
Change "Pass input:" to "as arguments".
Shell:[ /bin/bash ] Pass input:[ as arguments ]
Then drag the script from /usr/local/bin onto the box under "Run Shell Script" so it says
"/usr/local/bin/disable_osx_metadata"
File, save, disable_osx_metadata.workflow
For reference, this gets saved in:
/Users/YOUR_NAME/Library/Workflows/Applications/Folder Actions
If you save something wrong, I think you have to manually navigate there in Finder to delete it.
Finally, for each new usb drive you want to protect, you have to create a file named ".disable_osx_metadata" in the root folder.
You can do it in Terminal:
$ touch /Volumes/NO\ NAME/.disable_osx_metadata
Or just keep a small text file around with a visible name (no leading dot) and copy it to the root of any new usb drive and rename it .disable_osx_metadata after copying.
That drive now gets cleaned each time you attach it from now on.
Not as clean as I'd like. You have to pollute the drive a little, in order to tell the OS not to pollute it more. There seems to be no way make the OS simply leave it alone and not add any files that you didn't ask for.
TODO: Add enable/disable functions to create/remove the dot-file.
TODO: This does not prevent the .DS_Store files in every directory.
TODO: Is it possible to package up the script and the foo.workflow file so a user can skip most of these manual directions? I see there is an "export" option in Automator that creates some sort of package file.
TODO: Possibly obsolete this whole post. This applescript + automator workflow might be better:
https://superuser.com/questions/319553/does-anyone-have-a-mac-terminal-script-to-remove-hidden-files/814104#814104