Converting Old MacWrite Files to LibreOffice Using Automator

Found a cache of old MacWrite files (they appear in Finder as a black exec
icon). At least I think they're MacWrite files. They retain the creation dates which are around 1998.
They will open up quite well and retain most of the formatting with LibreOffice. But I was wanting to batch convert them usingĀ  Automator. Eventually I asked my new friend Chat who was able to help me out. After a few iterations we found something that works like a charm.

The code in the shell script is (can't vouch for the indentation):

for f in "$@"
do
    input_dir=$(dirname "$f")
    input_filename=$(basename "$f")
    output_filename="${input_filename%.*}.odt"
    /Applications/LibreOffice.app/Contents/MacOS/soffice --convert-to odt --outdir "$input_dir" "$f" --headless
    mv "$input_dir/$output_filename" "$input_dir/$input_filename.odt"
done

And if you'd care to see the explanation, here's the full convo with Chat. What was very cool was figuring out how to make the converted file save into the same directory the original was in.

This script will:

  • Get the input file directory and filename using the dirname and basename commands.
  • Set the output filename to be the input filename with the .odt extension using the ${input_filename%.*} syntax.
  • Use LibreOffice to convert the input file to ODT and save it in the input file directory using the --outdir option.
  • Rename the converted ODT file to have the same name as the input file with the .odt extension.Save the Automator workflow.

Now when you run the Automator app and drop MacWrite files onto it, the converted ODT files will be saved in the same directory as the original files with the same filename but with the .odt extension.