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