InDesign インライングラフィック(確認しながら文字列を置換) AppleScript
2-2 07 テキストフレーム内の特定の文字列を、確認しながら画像に置換する(P.108)
- 作者: (株)シータス,古籏一浩
- 出版社/メーカー: 技術評論社
- 発売日: 2008/11/25
- メディア: 大型本
- 購入: 5人 クリック: 91回
- この商品を含むブログ (8件) を見る
部品作製
- InDesign ドキュメント
- AppleScript ハンドラ
-- テキストフレーム内の[cFrom]番目の文字から[cTo]番目の文字を選択する on selectString(tfObj, cFrom, cTo) tell application "Adobe InDesign CS6" tell tfObj select (text from character cFrom to character cTo) end tell end tell end selectString -- テキストフレームの[cFrom]番目の文字から[cTo]番目の文字数部分に前に画像を挿入する on replaceStr2Image(tfObj, cFrom, cTo, myFile) tell application "Adobe InDesign CS6" tell tfObj place myFile on text from character cFrom to character cTo end tell end tell end replaceStr2Image
他のハンドラは以下のURLで実装済み http://scsnip.hatenablog.com/entry/2014/11/11/141330
-- 指定されたページのテキストフレームを返す on getTextFrames(myPage) -- 文字列内にある特定の文字列の開始位置を返す JavaScriptのindexOf(出現回数の引数を追加) on textIndexOf(myStr, myPat, myCount) -- 文字列内にある特定の文字列の全ての開始位置を返す on textIndexList(myStr, myPat)
部品が揃ったので、まとめ
-- 検索パターンを指定 display dialog "置換対象のタグ(文字)を入れてください" default answer "<<icon_AScript>>" set myPat to text returned of result -- 画像ファイルを指定 set myFile to "Macintosh HD:Users:logox:Desktop:asDocIcon.png" as alias set myList to getTextFrames(1) set loop to number of myList repeat with i from 1 to loop set myTF to item i of myList tell document 1 of application "Adobe InDesign CS6" set myStr to (text of myTF as string) end tell set pList to textIndexList(myStr, myPat) -- [AppleScript]が出現する全ての位置 set pList to reverse of pList -- 後ろの文字から処理 set loop to number of pList repeat with p from 1 to loop set insFrom to item p of pList set insTo to insFrom + (length of myPat) - 1 selectString(myTF, insFrom, insTo) display dialog "置換しますか?" buttons {"キャンセル", "NG", "OK"} if button returned of result is "OK" then if insFrom > 0 then replaceStr2Image(myTF, insFrom, insTo, myFile) end if end repeat end repeat
実行結果
- OS X 10.8.5
- Adobe InDesign CS6, version: 8.0.2.413