InDesign 複数の文字を置換 AppleScript
2-3 02 全てのページのテキストフレーム内複数の文字を置換する(P.113)
- 作者: (株)シータス,古籏一浩
- 出版社/メーカー: 技術評論社
- 発売日: 2008/11/25
- メディア: 大型本
- 購入: 5人 クリック: 91回
- この商品を含むブログ (8件) を見る
=== 書籍の JavaScript を AppleScript で書き直してみます ===
部品作製
- 置換キーワードファイル[replace.txt]
- InDesign ドキュメント
- AppleScript ハンドラ
-- 複数置換処理 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
実行結果
- OS X 10.8.5
- Adobe InDesign CS6, version: 8.0.2.413