スクスニップ

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