Skip to content

Instantly share code, notes, and snippets.

@kenjisato
Last active April 23, 2024 12:09
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kenjisato/de8435e64041d421605ea22af617038a to your computer and use it in GitHub Desktop.
Save kenjisato/de8435e64041d421605ea22af617038a to your computer and use it in GitHub Desktop.
Script to Export Keynote Presentation Files to PDF or JPEG
on sansExt(theFileName)
do shell script "file=" & theFileName & ";" & "echo ${file%.*}"
end sansExt
on getExt(theFileName)
do shell script "file=" & theFileName & ";" & "echo ${file##*.}"
end getExt
on run argv
set keynote_path to (item 1 of argv)
set out_path to (item 2 of argv)
set extension to getExt(out_path)
set basename to sansExt(out_path)
tell application "Keynote"
set keynote_file to open (keynote_path as POSIX file)
if extension is equal to "pdf" then
export keynote_file to (out_path as POSIX file) as PDF
else if extension is equal to "jpeg" then
export keynote_file to (basename as POSIX file) as slide images with properties { compression factor: 1.0, image format: JPEG }
else
do shell script "echo Output format " & extension & " not supported."
end
close keynote_file saving no
end tell
end run
#!/usr/bin/env bash
PACKAGE=$(basename $0)
VERSION="0.0.0.9001"
absolute_path () {
if [[ "$1" = /* ]]; then
echo ${1%/}
else
echo $(pwd)/${1%/}
fi
}
echo_help () {
echo "$PACKAGE - Export Keynote file to PDF/JPEG"
echo " "
echo "$PACKAGE [options] keynote_dir"
echo "options:"
echo "-h, --help show brief help"
echo "-o, --output-dir=DIR Output directory."
echo "-t, --type pdf or jpeg"
}
while test $# -gt 0; do
case "$1" in
-h|--help)
echo_help
exit 0;;
-o|--output-dir*)
shift
OUTPUTDIR=$(absolute_path $1)
shift;;
-t|--type*)
shift
TYPE=$1
shift;;
*)
if [[ ! -z "$1" ]] && [[ ! "$1" =~ ^-+ ]]; then
param+=( "$1" )
fi
shift;;
esac
done
INPUTDIR=$(absolute_path ${param[0]})
if [[ -z "$INPUTDIR" ]]; then
echo "Input directory is not specified."
exit 1
fi
[[ -z "$OUTPUTDIR" ]] && OUTPUTDIR="$INPUTDIR"
[[ -z "$TYPE" ]] && TYPE="pdf"
# The main loop
# Call an applescript to do the conversion.
for keyfile in $INPUTDIR/*.key; do
BASE=$(basename "$keyfile")
OUTFILE=$OUTPUTDIR/${BASE%.*}.$TYPE
osascript keynote_export.applescript "$keyfile" "$OUTFILE"
done
exit 0
@motform
Copy link

motform commented Sep 14, 2021

This is really great, thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment