Must Have Hazel Rules

I know you can do it with Apple script and Pages, and I’m going to figure out that tool at some point. But for now, I personally do it with this little shell script which I shamelessly stole from somewhere else, so I left the author info in there:

#!/bin/bash
# Jacob Salmela
# 2016-03-12
# Convert annoying DOCX into PDFs with a right-click
# Run this as an Automator Service

###### SCRIPT #######
for f in "$@"
do
  # Get the full file PATH without the extension
  filepathWithoutExtension="${f%.*}"

  # Convert the DOCX to HTML, which cupsfilter knows how to turn into a PDF
  textutil -convert html -output "$filepathWithoutExtension.html" "$f"

  retVal=$?
  if [ $retVal -ne 0 ]; then
      exit $retVal
  fi

  # Convert the file into a PDF
  cupsfilter "$filepathWithoutExtension.html" > "$filepathWithoutExtension.pdf"
  
  retVal=$?
  if [ $retVal -ne 0 ]; then
      exit $retVal
  fi

  # Remove the original file and the temporary HTML file, leaving only the PDF
  rm "$f" "$filepathWithoutExtension.html" >/dev/null
done
4 Likes