スクスニップ

AppleScriptの断片をここに書く

InDesign インライングラフィック AppleScript

2-2 05 テキストフレーム内の特定の文字列の前に画像を挿入する(P.106)

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

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

書籍では、ダイアログを開いて挿入する画像を指定しているが、 当ブログでは、画像ファイルのパスを設定しておく。

部品作製

f:id:mikomaya:20141111140930p:plain

-- 指定されたページのテキストフレームを返す
on getTextFrames(myPage)
    tell document 1 of application "Adobe InDesign CS6"
        every text frame of page myPage
    end tell
end getTextFrames

-- テキストフレームの[cIdx]番目の文字の前に画像を挿入する
on insertImage(tfObj, cIdx, myFile)
    tell application "Adobe InDesign CS6"
        place myFile on insertion point cIdx of tfObj
    end tell
end insertImage

-- JavaScript の indexOf メソッドを
-- AppleScript で実装する
-- 文字列内にある特定の文字列の開始位置を返す(出現回数の引数を追加)
on textIndexOf(myStr, myPat, myCount)
    set m to 0
    set myPat to every character of myPat
    set sLen to length of myStr
    set pLen to length of myPat
    
    if sLen < pLen then return m
    set loop to sLen - pLen + 1
    repeat with i from 1 to loop
        set iStr to characters i thru (i + pLen - 1) of myStr
        if iStr is myPat then
            set m to m + 1
            if m = myCount then
                return i
            end if
        end if
    end repeat
end textIndexOf

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

-- 検索パターン
set myPat to "AppleScript"

-- 画像ファイルのパス
set myFile to "Macintosh HD:Users:yourUserName: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 insPos to textIndexOf(myStr, myPat, 1) -- 最初に[AppleScript]が出現する位置
    if insPos > 0 then insertImage(myTF, insPos, myFile)
end repeat

実行結果

f:id:mikomaya:20141111141026p:plain

改造してみよう

下のテキストフレームには「AppleScript」が複数出現するので、それに対処してみる。

部品を追加

-- 文字列内にある特定の文字列の全ての開始位置を返す
on textIndexList(myStr, myPat)
    set res to {}
    set m to 0
    set myPat to every character of myPat
    set sLen to length of myStr
    set pLen to length of myPat
    
    if sLen < pLen then return res
    set loop to sLen - pLen + 1
    repeat with i from 1 to loop
        set iStr to characters i thru (i + pLen - 1) of myStr
        if iStr is myPat then
            set end of res to i
        end if
    end repeat
    res
end textIndexList

改造版

-- 検索パターン
set myPat to "AppleScript"

-- 画像ファイルのパス
set myFile to "Macintosh HD:Users:yourUserName: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 insPos to item p of pList
        if insPos > 0 then insertImage(myTF, insPos, myFile)
    end repeat
end repeat

改造版:実行結果

f:id:mikomaya:20141111141110p:plain