Keynote ファイルをエクスポートするスクリプト
April 8, 2017
computer
Rmarkdown
データに基いて描く必要のない概念図などは Keynote で描いている。Rmarkdown や LaTeX などで読み込むには,JPEG や PDF にエクスポートして利用する。
Rmarkdown で使う場合に利用する knitr::include_graphics()
は出力がHTMLのときはJPEG画像を読み込んで,LaTeXのときはPDFを使うということをやってくれる。便利だけど手間は2倍。手作業を減らしたいので下のようなスクリプトを書いた。AppleScript も Bashスクリプトも初心者だけど一応動いている動いている。
使い方は Keynote ファイルを入れているフォルダを引数にしてBashスクリプトの方を実行する。例えば
bash keynote_export.sh folder -t pdf
bash keynote_export.sh folder -t jpeg
などとする。
ただし,本当に knitr::include_graphics()
に渡すための PDF を作るためにはできたPDFファイル filename.pdf
を filename/filename.001.pdf
のように分割する必要があるので,もう少し処理が必要。
普通に LaTeX で \includegraphics
を使う場合は pgfpages を使えばよい。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |