スクスニップ

AppleScriptの断片をここに書く

InDesign テキストフレームの長体処理 AppleScript

2-2 04 選択されたテキストフレームがオーバーフローしているとき長体をかける(P105)

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

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

書籍では、テキストフレームを選択した状態でスクリプトを実行しているが、当ブログではスクリプトで対象オブジェクトを取得する方式に書き替えることにする(オブジェクトを選択する必要のない場合)。

また、書籍の解説部分(当ブログでは部品作製)はスクリプトが1行で書かれているが、なるべくハンドラ化するよう試みる。

部品作製

f:id:mikomaya:20141111111825p:plain

-- 指定されたページのテキストフレームを返す
on getTextFrames(myPage)
    tell document 1 of application "Adobe InDesign CS6"
        every text frame of page myPage
    end tell
end getTextFrames

-- テキストフレームがオーバーフローしているか?
on isOverFlow(tfObj)
    tell application "Adobe InDesign CS6"
        overflows of tfObj
    end tell
end isOverFlow

-- テキストフレームの[tIdx]番目のテキストの長体率を取得する
on horizontalScale(tfObj, tIdx)
    tell application "Adobe InDesign CS6"
        horizontal scale of text tIdx of tfObj
    end tell
end horizontalScale

-- テキストフレームの[tIdx]番目のテキストの長体率を変更する
on setHorizontalScale(tfObj, tIdx, myScale)
    tell application "Adobe InDesign CS6"
        set horizontal scale of text tIdx of tfObj to myScale
    end tell
end setHorizontalScale

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

set limitScale to 65 -- 最小長体率
set myList to getTextFrames(1)
set loop to number of myList
repeat with i from 1 to loop
    set myTF to item i of myList
    if isOverFlow(myTF) is true then
        set myScale to horizontalScale(myTF, 1)
        repeat
            set myScale to myScale - 1
            if myScale < limitScale then exit repeat
            setHorizontalScale(myTF, 1, myScale)
            if isOverFlow(myTF) is false then exit repeat
        end repeat
    end if
end repeat

実行結果

f:id:mikomaya:20141111111842p:plain

  • テキストフレーム 1(以下 TF1)と TF2 の長体率が変更された。
  • TF2 は長体率が最小長体率(上記スクリプトでは65%)に達したので処理が中断している。
  • TF1 のオーバーフローは解消されたが、行頭と行末の長体率が異なっている。

処理中の画面を観察すると、どうやらTF外にある文字の長体率が変更されないようだ。

長体率を変更する対象を text から paragraph に変更すると、上手く動くようだ。

部品を追加

-- テキストフレームの指定された段落(paragraph)の長体率を変更する
-- pidx が 0 なら全ての段落が対象
on setParagraphScaleH(tfObj, pidx, myScale)
    tell application "Adobe InDesign CS6"
        if pidx > 0 then
            set horizontal scale of paragraph pidx of tfObj to myScale
        else
            set horizontal scale of every paragraph of tfObj to myScale
        end if
    end tell
end setParagraphScaleH

改良版

set limitScale to 65 -- 最小長体率
set myList to getTextFrames(1)
set loop to number of myList
repeat with i from 1 to loop
    set myTF to item i of myList
    if isOverFlow(myTF) is true then
        set myScale to horizontalScale(myTF, 1)
        repeat
            set myScale to myScale - 1
            if myScale < limitScale then exit repeat
            setParagraphScaleH(myTF, 0, myScale)
            if isOverFlow(myTF) is false then exit repeat
        end repeat
    end if
end repeat

改良版:実行結果

f:id:mikomaya:20141111111855p:plain