スクスニップ

AppleScriptの断片をここに書く

InDesign セルのテキストオーバーフロー解消(表の再構築) AppleScript

2-5 03 [Column] 表組の再構築 (P.131)

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

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

=== 書籍の JavaScriptAppleScript で書き直します ===

概要

  • セル内のテキストがオーバーフローしていたら
  • 以下の処理を繰り返す
    • 文字の長体率を -1 減らす
    • 表の再構築(オーバーフローを正しく認識させるために必要な処理)
    • 最低長体率になったら繰り返しを脱出
  • オーバーフローが解消されるまで繰り返す

※動きが面白いので、今回は動画を用意してみました。

部品作製

f:id:mikomaya:20141114124901p:plain

  • AppleScript ハンドラ
    • addProcessColor(myName, myCMYK) -- プロセスカラーを追加する
    • colorNames() -- 全ての色名を返す
    • getColor(cName) -- 指定された名前のカラーオブジェクトを返す
    • tableList(myObj) -- オブジェクト内の全ての表を返す
    • tableSize(myTable) -- 表の大きさを返す(横,縦のセル数)
    • tCell(myTable, cIdx, rIdx) -- [cIdx]番目の項目の[rIdx]行目のセルオブジェクトを返す

上記ハンドラは、以下のエントリで実装済みです。

-- 表内の全てのセルを調査する
on tableWalk(myTable)
    set myColor to getColor("KINAKA")
    set {cMax, rMax} to tableSize(myTable)
    repeat with rIdx from 1 to rMax
        repeat with cIdx from 1 to cMax
            set myCell to tCell(myTable, cIdx, rIdx)
            fixHorizontasScale(myCell, 30) -- 最低長体率 30%
        end repeat
    end repeat
end tableWalk

-- セル内のテキストがオーバーフローしていたら
-- 長体率を変更してセル内におさめる
on fixHorizontasScale(myCell, myLimit)
    tell application "Adobe InDesign CS6"
        tell myCell
            if overflows is true then
                set myScale to horizontal scale of text 1
                log "HS: " & myScale
                repeat with myScale from (myScale - 1) to myLimit by -1
                    set horizontal scale of text 1 to myScale
                    
                    -- ★表組の再構築
                    tRecompose(myCell) of me
                    
                    if overflows is false then
                        exit repeat
                    end if
                end repeat
            else
                -- log "not overflows"
            end if
        end tell
    end tell
end fixHorizontasScale

-- myCellの親(表)の親(テキストフレーム)に★再構築を指示する
on tRecompose(myCell)
    tell application "Adobe InDesign CS6"
        recompose (parent of parent of myCell)
    end tell
end tRecompose

部品が揃ったので、実行

on run {}
    my setUp()
    my main()
    --my tearDown()
end run


on setUp()
    tell document 1 of application "Adobe InDesign CS6"
        select every text frame of page 1
    end tell
end setUp

on main()
    -- 色を追加
    --addProcessColor("KINAKA", {0, 100, 100, 0})
    
    -- 指定したテキストフレーム内の全てのテーブルオブジェクトを取得する
    tell document 1 of application "Adobe InDesign CS6"
        set myTF to text frame 1 of page 1
    end tell
    set tableList to tableList(myTF)
    
    -- テーブル数分繰り返す
    set loop to number of tableList
    tell application "Adobe InDesign CS6"
        repeat with i from 1 to loop
            tableWalk(item i of tableList) of me
        end repeat
    end tell
end main

on tearDown()
    activate
    display dialog "Script 終了" giving up after 3
end tearDown

実行結果

f:id:mikomaya:20141114124931p:plain