#!/bin/bash
# Utility to convert ClarisWorks or AppleWorks (".cwk") files
# to Microsoft Word 2007 (".docx") format.
# Made necessary for many because Mac OS X Mavericks' version of Pages
# no longer reads Appleworks files.
# Requires that you have LibreOffice installed. See http://www.libreoffice.org/download/libreoffice-fresh/
# To convert a whole directory (and subdirectories) of Appleworks .cwk files to MS Word .docx ones,
# first do:
# bash cwk2docx.sh -list
# to see what files are there.
#
# Then to really run it, do:
# bash cwk2docx.sh -process
#
# This finds all .cwk files, and for each one:
# a) copies it to tmp.cwk
# b) converts it to tmp.docx
# c) move tmp.docx to .docx
# d) copies tmp.cwk to cwkBackup//.cwk
# e) deletes the original .cwk file
#
# When finished, all .cwk files have been replaced by corresponding .docx files,
# and all the original .cwk files are backed up to cwkBackups with their original paths inside there.
#
# If something goes poorly, you can do
# bash cwk2docx.sh -restore
# to copy all of the .cwk files back to where they originally were.
#
# Please back your directory up before running this, just in case.
#
# by Randy Wilson, 21 November 2014, wilsonr@familysearch.org
#
if [ "$1" = "" ] || [ "$2" = "" ] ; then
echo "Utility to convert one or more Appleworks (.cwk) files to MS Word (.docx) files."
echo "Backs up the .cwk file to cwkBackup//."
echo "Requires that Libre Office be installed in /Applications."
echo Usage:
echo " $0 See usage"
echo " $0 -list See list of .cwk files in "
echo " $0 -convert Convert one file from .cwk to .docx"
echo " $0 -process Batch convert all .cwk files in to .docx"
echo " (Also moves .cwk files to ./cwkBackup/)"
echo " $0 -restore Restore backed up .cwk files from ./cwkBackup/ to "
exit 0
fi
command="$1"
dir="$2"
file="$dir"
backupDir="cwkBackup"
# List all .cwk files
if [ $command = "-list" ] ; then
find "$dir" -name "*.cwk" -print
exit 0
fi
# Convert ALL .cwk files in the directory to .docx
if [ "$command" = "-process" ] ; then
if [ -e /Applications/LibreOffice.app/Contents/MacOS/soffice ] ; then
echo "Processing all .cwk files in $dir..."
find "$dir" -name "*.cwk" -exec sh "$0" -convert {} \;
exit 0
else
echo "You must download LibreOffice and put it in your Applications folder for this to work."
echo "Visit http://www.libreoffice.org"
exit -1
fi
fi
# Convert a single file from .cwk to .docx
if [ "$command" = "-convert" ] ; then
echo
docx="${file/%.cwk/.docx}"
echo "Converting $file to $docx..."
tmpCwk="tmp.cwk"
tmpDocx="tmp.docx"
#echo "copying $file to $tmpCwk"
cp -v "$file" "$tmpCwk"
#echo converting "$tmpCwk" to "$tmpDocx"
/Applications/LibreOffice.app/Contents/MacOS/soffice --headless --convert-to docx:"MS Word 2007 XML" "$tmpCwk"
if [ -e "$tmpCwk" ]; then
#echo "Moving $tmpDocx to $docx"
mv "$tmpDocx" "$docx"
backupFile="$backupDir/$file"
theDir="$backupDir/$(dirname "${file}")"
#echo "Created file $docx"
#echo " backupFile=$backupFile"
#echo " theDir=$theDir"
# Make sure the directory exists before moving file to it
#echo "Creating directories up to $theDir"
mkdir -p "$theDir"
#echo "Moving temp file $tmpCwk to $backupFile"
mv -v "$tmpCwk" "$backupFile"
if [ -e "$file" ]; then
echo "Removing original file $file"
rm "$file"
fi
else
echo "Warning: Didn't create $docx from $file"
fi
exit 0
fi
# Move files back from the backup directory to where they were before.
if [ "$command" = "-restore" ] ; then
# find all files in 'cwkBackup', strip 'cwkBackup/' off of the path
# add / to the beginning, and move the files back.
find "$backupDir" -name "*.cwk" -exec "$0" -restoreOne {} \;
exit 0
fi
# Restore one file from cwkBackup/path to path
if [ "$command" == "-restoreOne" ] ; then
origFile="${file#${backupDir}/}"
#echo "RestoreOne: file=<$file>, origFile=<$origFile>"
mv -v "$file" "$origFile"
exit 0
fi