Adding contextual menu item to zero out files

automatorshortcut-menuuti

I want to be able to easily zero-out (make the filesize 0, not fill the existing filesize with 0s) files from the Finder by selecting files from within Finder and selecting an action from the right click menu. I looked at the available Automator actions, but I could only figure out how to delete and recreate files, which would lose any existing attributes. How can I create a contextual menu item to zero out files?

Best Answer

  • Select Run Shell Script
  • Use the default /bin/bash command
  • Select Pass input as arguments
  • Enter the following into the text input box:

    for file in "$@"; do
        cat /dev/null > "$file"
    done
    

Possible improvements:

  • Display admin prompt if the user account doesn't have permissions to modify the selected files.
  • If a directory is selected, perform the action on all files in the selected directory.