スクスニップ

AppleScriptの断片をここに書く

InDesign 複数のキーワードに対応し、個別の文字スタイルを適用する AppleScript

2-6 02 選択されたテキストフレーム内の複数の文字に、個別にスタイルを適用する (P.135)

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

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

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

概要(書籍の方法とは異なります)

  • 文字スタイル設定ファイルを読み込む(各行に分割)
  • テキストフィールド内の文字に対して
  • 以下、設定ファイル行毎に繰り返す
    • 設定ファイル1行をタブで分割 =>「キーワード/スタイル名」
    • キーワード検索を実行し、キーワードの出現位置リストを取得する
    • キーワードの出現位置リストの個数分繰り返す
    • 文字スタイルを適用する
  • 繰り返し終了

部品作製

f:id:mikomaya:20141114173042p:plain

f:id:mikomaya:20141114173054p:plain

  • AppleScript ハンドラ(既出分)

    • changeStyle(myObj, myStyle, posList) -- 文字スタイル変更する
    • searchKeyword(myRows, myKwd) -- キーワード[myKed]検索
    • setCharStyle(myObj, myRow, cFrom, cTo, myStyle) -- 文字スタイル適用
    • charStyle(myName) -- 文字スタイルを返す
    • 上記のハンドラは下記のエントリで実装されています
  • AppleScript ハンドラ(新規)

-- 文字スタイル設定ファイルを読み込む
on readStyleFile()
    try
        set myFile to choose file with prompt "置換キーワードのファイルを指定してください"
    on error
        display dialog "置換キーワードファイルがありません。"
        return missing value
    end try
    set myText to read (myFile) -- ファイル読み込み
    every paragraph of myText -- 各行に分割して返す
end readStyleFile

-- 文字列を[mySep]で分割 -> リスト
on split(myStr, mySep)
    set oldDelim to AppleScript's text item delimiters
    set AppleScript's text item delimiters to mySep
    set myList to every text item of myStr
    set AppleScript's text item delimiters to oldDelim
    return myList
end split

-- リストを[mySep]で連結 -> 文字列
on join(myList, mySep)
    set oldDelim to AppleScript's text item delimiters
    set AppleScript's text item delimiters to mySep
    set myStr to myList as string
    set AppleScript's text item delimiters to oldDelim
    return myStr
end join

部品が揃ったので、実行

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

on setUp()
end setUp

on main()
    
    -- 文字スタイル設定ファイル読み込み
    set setList to readStyleFile()
    
    -- P.1 の全テキストフレームを取得する
    tell document 1 of application "Adobe InDesign CS6"
        set myList to every text frame of page 1
    end tell
    
    -- テキストフレーム数分繰り返す
    set loop to number of myList
    repeat with i from 1 to loop
        set myTF to item i of myList
        
        -- 設定ファイルの行数繰り返す
        repeat with mySet in setList
            
            -- 設定ファイルの行を「キーワード/文字スタイル」にタブで分割
            set {myKwd, myStyleName} to split(mySet, tab)
            set myStyle to charStyle(myStyleName) -- スタイルオブジェクト
            -- ここから下は前のエントリと同じ
            set matchList to searchKeyword(every paragraph of myTF, myKwd)
            changeStyle(myTF, myStyle, matchList)
            
        end repeat
    end repeat
    
end main

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

実行結果

f:id:mikomaya:20141114173109p:plain

InDesign 特定の文字に文字スタイルを適用する AppleScript

2-6 01 選択されたテキストフレーム内の文字が特定の文字なら、スタイルを適用する (P.133)

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

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

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

概要(書籍の方法とは異なります)

  • テキストフィールド内の文字に対して
  • キーワード検索を実行し、キーワードの出現位置リストを取得する
  • キーワードの出現位置リストの個数分繰り返す
    • 文字スタイルを適用する

部品作製

  • InDesign ドキュメント(文字スタイル「太字」を作成しておく)

f:id:mikomaya:20141114170701p:plain

-- 文字スタイルを適用する
-- [myObj]の[myRow]段落の[cFrom]文字〜[cTo]文字まで
-- 文字スタイル[myStyle]を適用する
on setCharStyle(myObj, myRow, cFrom, cTo, myStyle)
    tell application "Adobe InDesign CS6"
        -- 1行が長くなるので、強制改行
        apply character style ¬
            text from character cFrom ¬
                to character cTo ¬
                of paragraph myRow ¬
            of myObj ¬
            using myStyle
    end tell
end setCharStyle

-- 指定された文字スタイルを返す
-- なければ文字スタイル 1を返す
on charStyle(myName)
    tell document 1 of application "Adobe InDesign CS6"
        try
            character style myName
        on error
            character style 1
        end try
    end tell
end charStyle

-- オブジェクト内の文字をキーワード[myKed]検索する
on searchKeyword(myRows, myKwd)
    
    set myKwd to every character of myKwd
    -- キーワードをリスト形式にあらかじめ変換しておく
    -- "AppleScript" => {"A", "p", "p", "l", "e", "S", "c", "r", "i", "p", "t"}
    
    -- 下記[C]の myWord がリスト形式になるので、それに合わせておく
    
    -- myWord as string で文字列に変換し、キーワードと比較するのも可能だが、
    -- 処理回数が少ない方を優先した
    
    set kLen to number of myKwd -- キーワードの長さ
    set mList to {} -- キーワード検索結果
    
    -- [A]段落分繰り返す (rIdxは1ずつ増加)
    repeat with rIdx from 1 to number of myRows
        
        set myRow to item rIdx of myRows
        
        -- [B] rIdx 段落目の文字数文繰り返す
        set loop to ((length of myRow) - kLen + 1) -- キーワードの長さを差引く
        repeat with i from 1 to loop
            
            -- [C] キーワードと同じ文字数の文字を段落から取り出す
            set cFrom to i
            set cTo to (i + kLen - 1)
            set myWord to characters cFrom thru cTo of myRow
            
            -- [D] キーワードに合致したら
            if myWord is myKwd then
                set end of mList to {rIdx, cFrom, cTo} -- 検索結果に追加
            end if
        end repeat
    end repeat
    
    return mList -- 検索結果を返す
end searchKeyword

-- キーワード検索結果[posList]を受け取り、文字スタイルを変更する
on changeStyle(myObj, myStyle, posList)
    set loop to number of posList
    repeat with i from 1 to loop
        set {myRow, cFrom, cTo} to item i of posList
        setCharStyle(myObj, myRow, cFrom, cTo, myStyle)
    end repeat
end changeStyle

部品が揃ったので、実行

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

on setUp()
end setUp

on main()
    -- 検索ワード
    set myKwd to "AppleScript"
    
    -- 適用する文字スタイル
    set myStyleName to "太字" -- スタイル名
    set myStyle to charStyle(myStyleName) -- スタイルオブジェクト
    
    -- P.1 の全テキストフレームを取得する
    tell document 1 of application "Adobe InDesign CS6"
        set myList to every text frame of page 1
    end tell
    
    -- テキストフレーム数分繰り返す
    set loop to number of myList
    repeat with i from 1 to loop
        set myTF to item i of myList
        set matchList to searchKeyword(every paragraph of myTF, myKwd)
        changeStyle(myTF, myStyle, matchList)
    end repeat
    
end main

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

実行結果

f:id:mikomaya:20141114170755p:plain

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

InDesign 表のセル内容が負数の場合、マイナスを赤字の「▲」にする AppleScript

2-5 03 表のセル内容が負数の場合、マイナスを赤字の「▲」にする(P.129)

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

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

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

部品作製

f:id:mikomaya:20141114105328p: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)
            --minus2red(myCell, myColor)
            --minus2sankaku(myCell)
            minus2sankakuRed(myCell, myColor)
        end repeat
    end repeat
end tableWalk

-- セルの1文字目が「-」なら、「-」を「▲」に置換し、文字色を変更する
on minus2sankakuRed(myCell, myColor)
    tell application "Adobe InDesign CS6"
        if contents of first character of myCell is "-" then
            set character 1 of myCell to "▲"
            set fill color of every character of myCell to myColor
        end if
    end tell
end minus2sankakuRed

部品が揃ったので、実行

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:20141114111340p:plain

InDesign 表のセル内容が負数の場合、マイナスを削除し「▲」にする AppleScript

2-5 02 表のセル内容が負数の場合、マイナスを削除し「▲」にする(P.128)

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

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

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

部品作製

f:id:mikomaya:20141114105328p:plain

  • AppleScript ハンドラ
    • 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)
            --minus2red(myCell, myColor)
            minus2sankaku(myCell) -- <== ※
        end repeat
    end repeat
end tableWalk

-- セルの1文字目が「-」なら、「-」を「▲」に置換する
on minus2sankaku(myCell)
    tell application "Adobe InDesign CS6"
        if contents of first character of myCell is "-" then
            set character 1 of myCell to "▲" -- <== ※
        end if
    end tell
end minus2sankaku

部品が揃ったので、実行

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:20141114105358p:plain