InDesign 空のテキストフレームを削除する AppleScript
2-2 03 空のテキストフレームを削除する
- 作者: (株)シータス,古籏一浩
- 出版社/メーカー: 技術評論社
- 発売日: 2008/11/25
- メディア: 大型本
- 購入: 5人 クリック: 91回
- この商品を含むブログ (8件) を見る
部品作製
-- テキストフレームを削除する set myPage to 2 set fIdx to 1 tell application "Adobe InDesign CS6" delete text frame fIdx of page myPage of active document end tell
-- テスト用ドキュメント
部品が揃ったので、まとめ
tell application "Adobe InDesign CS6" tell active document set pMax to number of pages repeat with pidx from 1 to pMax tell page pidx set fMax to number of every text frame repeat with fIdx from 1 to fMax if contents of text frame fIdx is "" then delete text frame fIdx end if end repeat end tell end repeat end tell end tell
実行結果[エラー]
テキストフレーム(以降フレーム)を1から数え上げているのがエラーの原因。 書籍のスクリプトはエラーにならないように、フレームの個数からはじめてカウンタを1つずつ減らすように書かれている(説明はされていない)。
例えばページにフレームが3個ある場合は、AppleScript的に記すとこうなる。
{text frame 1,text frame 2,text frame 3} -- ※ <== 左になるほど上に配置されている
この状態で、text frame 1 を削除すると、こうなる。
{text frame 1,text frame 2}
text frame 1 は確かに削除されるのだが、text frame 1 が削除された瞬間、それまで text frame 2 だったものが 1 になり、 text frame 3 だったものが 2 になる。常に1から始まるようになっている。
これは、フレームに限らず、他のオブジェクトでも同様。だから、オブジェクトを削除する場合、後ろの(下の)オブジェクトから処理するのが鉄則ですね(ときどき忘れてしまうけど)。
書籍のスクリプト同様、カウントダウンで書いたのが↓こちら
tell application "Adobe InDesign CS6" tell active document set pMax to number of pages repeat with pidx from 1 to pMax tell page pidx set fMax to number of every text frame repeat with fIdx from fMax to 1 by -1 if contents of text frame fIdx is "" then delete text frame fIdx end if end repeat end tell end repeat end tell end tell
実行結果
- OS X 10.8.5
- Adobe InDesign CS6, version: 8.0.2.413