InDesign基礎編1
InDesign CSをAppleScriptで制御する
基本的な作業を紹介します。
各アイコンのついたものは対応するバージョンを表します。
これ以前のバージョンのものは割愛させて頂く。
| 対応バージョン | CS | CS2 | CS3 |
| アイコン |
新規ドキュメント
![]()
![]()
![]()
tell application "Adobe InDesign CS2_J"
activate
set MyDocument to make document
end tell
make documentが新規ドキュメントを作成するコマンドだ。
これを実行すると、取りあえず新規ドキュメントが作成される。
しかし、これだけでは思い通りのものは作れず、どうしようもない。
ドキュメントサイズやページ数、スプレッド数などのオプションを指定する事で、思い通りのドキュメントを作成してみよう。

上図はCS2での新規ドキュメント作成のダイアログだ。
このダイアログの設定を変更するには以下のようにする。
●ドキュメントの設定を変更するにはdocument preferencesで変更する
tell application "Adobe InDesign CS2_J"
activate
set MyDocument to make document
tell MyDocument
set myDocumentPreferences to document preferences
tell myDocumentPreferences
set pages per document to 8 --ページ数
set facing pages to true --見開きページ(見開き)
set page width to "210 mm" --ページ幅
set page height to "297 mm" --ページ高さ
set page binding to right to left --綴じ方(右綴じ)
end tell
end tell
end tell
上のスクリプトを実行すると、A4サイズで8ページ右綴じの見開きドキュメントが作成される。
pages per document (integer) :ページ数を指定する
facing pages (boolean) :trueで見開き、falseで単ページ
page width (number or string) :ページの幅 数値もしくは文字を入れる
文字列とは、数値に単位を含めたものです。
単位がミリならば"mm"。ポイントなら"pt"という具合です。
page height (number or string) :ページの高さ page widthと同じ指定です。
page binding (default/right to left/left to right) :綴じ方向 defaultなら初期設定のまま。
right to leftで右綴じ、left to rightで左綴じとなります。
このほかにも塗り足しの設定など幾つか設定がありますが、スクリプト用語説明で確認してみて下さい。

テキストフレームを描画
![]()
![]()
![]()
●テキストフレームの作成はmake text frameで作成
tell application "Adobe InDesign CS2_J"
set MyDocument to active document
tell MyDocument
tell page 1
set myTextFrame to make text frame
tell myTextFrame
set geometric bounds to {"20 mm", "20 mm", "100 mm", "190 mm"}
set stroke color to swatch "None" of MyDocument
set fill color to swatch "None" of MyDocument
end tell
end tell
end tell
end tell
すでにドキュメントが開いている状態での描画です。
make text frameがテキストフレームの作成となり、サイズなどのプロパティを指定しています。
geometric bounds (list of number or string) :テキストフレームのサイズ。listはtop, left, bottom, rightの順で記述します。
stroke color (swatch) :フレームの境界線の色 ドキュメントに登録されているスウォッチ名で指定する。
fill color (swatch) :フレーム塗色 ドキュメントに登録されているスウォッチ名で指定する。
こちらも多くのプロパティが存在しますので、スクリプト用語説明を参照する事を勧めます。
設定は、text fameのプロパティですので、作成時に一括して指定する事も可能です。
set myTextFrame to make text frame with properties ¬
{geometric bounds:{"20 mm", "20 mm", "100 mm", "190 mm"}, ¬
stroke color:swatch "None" of MyDocument, ¬
fill color:swatch "None" of MyDocument}
”¬”はスクリプトをつないで記述していることを意味する記号です。実際には一行で書きます。
テキストの入力
![]()
![]()
![]()
それではプログラミングの定番。"Hello World"をテキストフレームに入力する方法を記します。
tell application "Adobe InDesign CS2_J"
set MyDocument to active document
tell MyDocument
tell page 1
set myTextFrame to make text frame
tell myTextFrame
set geometric bounds to {"20 mm", "20 mm", "100 mm", "190 mm"}
set stroke color to swatch "None" of MyDocument
set fill color to swatch "None" of MyDocument
-- * ここから追加
set myStory to parent story
tell myStory
set contents to "Hello World"
end tell
-- * ここまで
end tell
end tell
end tell
end tell
さきほどのテキストフレームを作成するスクリプトにテキストを入力する部分を追加した。
テキストフレームにはサイズや色などのプロパティはあるが、あくまでもオブジェクトのプロパティだ。
内容に関しては、storyで指定する。parentは指定のテキストフレームを意味するもので、これが無いとエラーになる。
実際に、テキストを指定しているのは、set contents to "Hello World"となる。
storyにはcontent以外に文字のサイズや文字色、フォントの指定などを行う。
set point size to "16 pt"--サイズ
set applied font to "小塚明朝 Pro R" --フォント
set fill color to "Black" --塗色
set stroke color to "Paper" --文字のふち色(Paperは白ふち)
set stroke weight to "0.2 pt" --ふち幅
set rotate single byte characters to true --縦組中の欧文回転
長方形フレームの作成
![]()
![]()
![]()
作成はテキストフレームと良く似ている。
tell application "Adobe InDesign CS2_J"
set MyDocument to active document
tell MyDocument
tell page 1
set myRectangle to make rectangle
tell myRectangle
set geometric bounds to {"20 mm", "20 mm", "100 mm", "190 mm"}
set stroke color to swatch "None" of MyDocument
set fill color to swatch "Black" of MyDocument
end tell
end tell
end tell
end tell
ご覧の通り変わったのは、text frameがrectangleに変わっただけだ。
もちろん一行での書き方も同様。
画像の貼り込み
![]()
![]()
![]()
引き続いて、画像の貼り込みを行ってみよう。
画像ファイルをフレームのセンターに貼り込む。
set placeFile to choose file --貼り込むファイルを選択する
tell application "Adobe InDesign CS2_J"
set MyDocument to active document
tell MyDocument
tell page 1
set myRectangle to make rectangle
tell myRectangle
set geometric bounds to {"20 mm", "20 mm", "100 mm", "190 mm"}
set stroke color to swatch "None" of MyDocument
set fill color to swatch "None" of MyDocument
-- * 貼り込み
place placeFile --ファイルを貼り込む
fit given center content --センターに貼り込む
-- * ここまで
end tell
end tell
end tell
end tell
●フレームにファイルを貼り込む場合は、placeを使う
placeで、Indesignでサポートされているファイルすべてが貼り込める。
PDFなどページのあるものなどは、それぞれの貼り込み設定のpreferenceが用意されており、貼り込みを行う前に、設定を行っておく。
PDFの場合、PDF place preferencesで貼り込み設定を行う。
set placeFile to choose file --貼り込むファイルを選択する
tell application "Adobe InDesign CS2_J"
-- * PDFの貼り込み設定
tell PDF place preferences
set page number to 1 -- PDFのページ
set transparent background to false --背景を透明にのチェック
set PDF crop to crop PDF --トリミング
end tell
-- * ここまで
set MyDocument to active document
tell MyDocument
tell page 1
set myRectangle to make rectangle
tell myRectangle
set geometric bounds to {"20 mm", "20 mm", "100 mm", "190 mm"}
set stroke color to swatch "None" of MyDocument
set fill color to swatch "None" of MyDocument
-- * 貼り込み
place placeFile --ファイルを貼り込む
fit given center content --センターに貼り込む
-- * ここまで
end tell
end tell
end tell
end tell
上のスクリプトで、PDFファイルの貼り込み設定を行ってから貼り込みを行うようにする。
ラインを引く
![]()
![]()
![]()
まずは直線を引く
tell application "Adobe InDesign CS2_J"
set MyDocument to active document
tell MyDocument
tell page 1
set myLine to make graphic line
tell myLine
set geometric bounds to {"20 mm", "20 mm", "100 mm", "190 mm"}
set stroke weight to "0.2 pt"
set stroke color to swatch "Black" of MyDocument
end tell
end tell
end tell
end tell
こちらもテキストフレームやグラフィックフレームと同様で、線を描く場合にはgraphic lineを使うだけだ。
次に連続する直線を描く方法を記します。
tell application "Adobe InDesign CS2_J"
set MyDocument to active document
tell MyDocument
tell page 1
set myLine to make graphic line
tell myLine
set entire path of path 1 to ¬
{{"20 mm", "20 mm"}, {"100 mm", "190 mm"}, ¬
{"80 mm", "90 mm"}, {"150 mm", "60 mm"}}
set stroke weight to "0.2 pt"
set stroke color to swatch "Black" of MyDocument
end tell
end tell
end tell
end tell
●連続する線を描く時はpathを使う
graphic lineのpathに対してプロパティを設定する。
そのため、set entire pathの後ろにpath 1を付けて指定しなければならない。
テキストフレームのstoryのようなものだ。
指定方法は、X点Y点の2点をリストにし、必要な点の集合をまたリストにして指定を行う。