スクスニップ

AppleScriptの断片をここに書く

InDesign 空のテキストフレームを削除する AppleScript

2-2 03 空のテキストフレームを削除する

組版時間を半減する! InDesign自動処理実例集

組版時間を半減する! InDesign自動処理実例集

部品作製

-- テキストフレームを削除する
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

-- テスト用ドキュメント

f:id:mikomaya:20141110190053p:plain

部品が揃ったので、まとめ

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

実行結果[エラー]

f:id:mikomaya:20141110190104p:plain

テキストフレーム(以降フレーム)を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

実行結果

f:id:mikomaya:20141110190255p:plain