スクスニップ

AppleScriptの断片をここに書く

InDesign 複数の文字を置換 AppleScript

2-3 02 全てのページのテキストフレーム内複数の文字を置換する(P.113)

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

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

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

部品作製

  • 置換キーワードファイル[replace.txt]

f:id:mikomaya:20141112111058p:plain

f:id:mikomaya:20141112111117p:plain

-- 複数置換処理
on replaceWords(myObj, repWords)
    set loop to number of repWords
    repeat with i from 1 to loop
        set {kw, rw} to split(item i of repWords, tab) -- タブで分割
        if kw is not "" then
            replaceWord(myObj, kw, rw)
        end if
    end repeat
end replaceWords

-- 置換処理
on replaceWord(myObj, kw, rw)
    tell application "Adobe InDesign CS6"
        set find text preferences to nothing --[accept or nothing]
        set change text preferences to nothing --[accept or nothing]
        set find what of find text preferences to kw
        set change to of change text preferences to rw
        change text parent story of myObj
    end tell
end replaceWord

-- 文字列を[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

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

try
    set myFile to choose file with prompt "置換キーワードのファイルを指定してください"
on error
    display dialog "置換キーワードファイルがありません。"
    error
end try

set myText to read (myFile) -- ファイル読み込み
set repWords to every paragraph of myText -- 各行に分割

tell application "Adobe InDesign CS6"
    tell active document
        set pMax to number of page
        repeat with myPage from 1 to pMax
            set fMax to number of every text frame of page myPage
            repeat with i from 1 to fMax
                set tfObj to text frame i of page myPage
                my replaceWords(tfObj, repWords)
            end repeat
        end repeat
    end tell
end tell

実行結果

f:id:mikomaya:20141112111129p:plain