Wednesday, October 14, 2009

Convert Keynote / iWork 2009 to 2008

If you're like me, you tried Keynote 2009 and - while Keynote continues to be a superior presentation tool - decided that it wasn't worth the cost of upgrading. If you're even more like me, you created at least one presentation in Keynote 2009 during the trial (or converted a Keynote 2008 presentation to Keynote 2009 and tweaked it a bit), and were then quite dismayed to find that you could not open the Keynote 2009 file in Keynote 2008 at all. This is a big problem after the trial runs out - it leaves you with presentation material you can't recover!

The Keynote 2008 "file format" is really a directory structure. That is, each .key file is really a directory containing the information that Keynote uses to reconstruct the presentation. In Keynote 2009 this changed to a "flat file" format - they .key file really is a single file. Investigation reveals pretty quickly that the contents of that file are quite similar to the contents of the 2008 directory. A post by linux4research cleared this up - the 2009 format is actually a zipped archive! So half the battle is to simply rename the .key file to a .zip file, unzip it, then rename the unzipped folder with a .key extension. This still won't open in Keynote 2008, though. Luckily the other half of the battle is equally as simple. In they .key folder, open the file index.apxl, find the string key:version= followed by a quoted string that is probably something like 92008xxxxxx. Replace the number with 72007061400.

Anyways, here is a shell script that automates this process. I haven't tested it too extensively, but the basic idea should work. I haven't had experience with other iWork '09 programs (Pages, Numbers), but reportedly the differences are essentially the same, so it may work with them as well if you replace "key" with the proper extension.

#!/bin/bash
# Dan Swain dan.t.swain at gmail.com 10/14/09

# Bash script to convert Keynote 2009 format to Keynote 2008.
#
# Many thanks to linux4research's blog post
# http://linux4research.blogspot.com/ (continued next line)
# (continued url) 2009/09/mac-keynote-2009-format.html
# for the .key to .zip step.

# input argument handling: DISABLED - I don't trust it enough
# not to erase your keynote file on accident...
# if -k is given as first argument, keep the original file
# and the output has "2008" added before .key
# otherwise the original file is overwritten (may be desirable
# because keynote files tend to be LARGE)
# if [ $# -ge 2 ] # note we may throw away extra arguments
# then
# if [ "$1" = "-k" ]
# then
# INPUT=$2
# KEEP=1
# else
# echo "Unknown argument $1, use -k to keep original file."
# exit
# fi
# else
# INPUT=$1
# KEEP=0
# fi

# safe input handling - need at least one argument
if [ $# -ne 1 ]
then
echo "Only one input argument allowed."
exit
fi
INPUT=$1
KEEP=1

# basename doesn't work for keynote files...
BASE=${INPUT%.key}
ZIPFILE=$BASE".zip"

if [ $KEEP -eq 1 ]
then
# output filename
OUTPUT=$BASE
OUTPUT=$OUTPUT" 2008.key"
# copy/rename to zipfile
echo "Copying $INPUT to $ZIPFILE, this may take a moment.."
cp $INPUT $ZIPFILE
echo "Done."
else
# output filename
OUTPUT=$INPUT
# rename to zipfile
mv $INPUT $ZIPFILE
fi

# unzip
echo "Unzipping $ZIPFILE."
unzip $ZIPFILE -d "$OUTPUT"
# erase the zipfile
echo "Erasing $ZIPFILE."
rm $ZIPFILE
# change the version number in the index file
echo "Converting version string to 2008."
INDEXAPXL=$OUTPUT/index.apxl
sed -i '' 's/version=\"[0-9]*/version=\"72007061400/g' "$INDEXAPXL"