InDesign 複数のキーワードに対応し、個別の文字スタイルを適用する AppleScript
2-6 02 選択されたテキストフレーム内の複数の文字に、個別にスタイルを適用する (P.135)
- 作者: (株)シータス,古籏一浩
- 出版社/メーカー: 技術評論社
- 発売日: 2008/11/25
- メディア: 大型本
- 購入: 5人 クリック: 91回
- この商品を含むブログ (8件) を見る
=== 書籍の JavaScript を AppleScript で書き直します ===
概要(書籍の方法とは異なります)
- 文字スタイル設定ファイルを読み込む(各行に分割)
- テキストフィールド内の文字に対して
- 以下、設定ファイル行毎に繰り返す
- 設定ファイル1行をタブで分割 =>「キーワード/スタイル名」
- キーワード検索を実行し、キーワードの出現位置リストを取得する
- キーワードの出現位置リストの個数分繰り返す
- 文字スタイルを適用する
- 繰り返し終了
部品作製
- InDesign ドキュメント
- スタイル指定洋テキストファイル[style.txt]
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
実行結果
- OS X 10.8.5
- Adobe InDesign CS6, version: 8.0.2.413