スクスニップ

AppleScriptの断片をここに書く

InDesign 選択された段落に番号を振る(桁数指定) AppleScript

2-4 02 選択された段落に指定した桁数の番号を振る(P.121)

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

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

=== 書籍の JavaScriptAppleScript で書き直してみます ===

部品作製

f:id:mikomaya:20141112143323p:plain

-- 指定した桁数(0を左に追加)に整形した数(文字列)を返す
on zeroPadding(myNum, pSize)
    set pStr to {}
    repeat pSize times
        set end of pStr to "0"
    end repeat
    set pStr to pStr as string
    set myNum to myNum as string
    set mySize to length of myNum
    if mySize ≥ pSize then
        -- 数値の桁数が多い場合はそのまま返す
        return myNum
    end if
    set fixStr to pStr & myNum
    set fixSize to length of fixStr
    characters (fixSize - pSize + 1) thru (fixSize) of fixStr as string
end zeroPadding

-- オブジェクト内の段落[pFrom]から[pTo]までを選択
-- pToが0の場合は、最終段落まで
-- pToがマイナスの場合は、最終段落 - pTo段落まで
to selectParagraph(myObj, pFrom, pTo)
    tell application "Adobe InDesign CS6"
        tell myObj
            if pTo = 0 then set pTo to number of paragraph
            if pTo < 0 then set pTo to (number of paragraph) + pTo
            select (text from paragraph pFrom to paragraph pTo)
        end tell
    end tell
end selectParagraph

-- 選択された段落に行番号を追加
to addParagraphNumbers2(mySep, pSize)
    tell application "Adobe InDesign CS6"
        set loop to number of paragraph of selection
        repeat with i from 1 to loop
            set myNumber to zeroPadding(i, pSize) of me
            set contents of insertion point 1 of paragraph i of selection to ("" & myNumber & mySep)
        end repeat
    end tell
end addParagraphNumbers2

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

display dialog "桁数を入れてください" default answer "3"
set pSize to text returned of result as number
if pSize < 3 then set pSize to 3

set myPrefix to {}
repeat pSize times
    set end of myPrefix to "0"
end repeat
set myPrefix to myPrefix as string

tell application "Adobe InDesign CS6"
    tell active document
        set myObj to text frame 1
    end tell
end tell

selectParagraph(myObj, 4, -1)
addParagraphNumbers2(" : ", pSize)

実行結果

f:id:mikomaya:20141112160035p:plain

f:id:mikomaya:20141112160037p:plain

f:id:mikomaya:20141112160038p:plain

f:id:mikomaya:20141112160040p:plain

InDesign 選択された段落に番号を振る AppleScript

2-4 01 選択された段落に番号を振る(P.120)

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

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

=== 書籍の JavaScriptAppleScript で書き直してみます ===

部品作製

f:id:mikomaya:20141112143323p:plain

-- オブジェクト内の段落[pFrom]から[pTo]までを選択
-- pToが0の場合は、最終段落まで
-- pToがマイナスの場合は、最終段落 - pTo段落まで
to selectParagraph(myObj, pFrom, pTo)
    tell application "Adobe InDesign CS6"
        tell myObj
            if pTo = 0 then set pTo to number of paragraph
            if pTo < 0 then set pTo to (number of paragraph) + pTo
            select (text from paragraph pFrom to paragraph pTo)
        end tell
    end tell
end selectParagraph

-- 選択された段落に行番号を追加
to addParagraphNumbers(mySep)
    tell application "Adobe InDesign CS6"
        set loop to number of paragraph of selection
        repeat with i from 1 to loop
            set contents of insertion point 1 of paragraph i of selection to ("" & i & mySep)
        end repeat
    end tell
end addParagraphNumbers

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

tell application "Adobe InDesign CS6"
    tell active document
        set myObj to text frame 1
    end tell
end tell

selectParagraph(myObj, 4, -1)
addParagraphNumbers(" : ")

実行結果

f:id:mikomaya:20141112143337p:plain

InDesign 行内の()で囲まれた文字列を削除する AppleScript

2-3 04 選択されたテキストフレーム内の「(」と「)内の文字」を削除する(P.118)

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

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

=== 書籍の JavaScriptAppleScript で書き直してみます ===

部品作製

f:id:mikomaya:20141112133257p:plain

-- オブジェクト内の[pIdx]行目のテキストの[cFrom]文字〜[cTo]文字を削除する
on removeStr(myObj, pIdx, cFrom, cTo)
    tell application "Adobe InDesign CS6"
        tell myObj
            delete text from character cFrom to character cTo of paragraph pIdx
        end tell
    end tell
end removeStr

-- 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
    return 0
end textIndexOf

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

【注意】スクリプトの書き方を間違えると、 内側のリピート文が無限ループに落ちる可能性がある。

set startMark to "("
set endMark to ")"
tell application "Adobe InDesign CS6"
    set myTF to text frame 1 of document 1
    tell myTF
        set loop to number of paragraphs
        --行数分繰り返す
        repeat with i from 1 to loop
            set myStr to paragraph i
            set startPoint to textIndexOf(myStr, startMark, 1) of me
            -- 行内に"("がある間繰り返す
            repeat
                --log startPoint
                if startPoint > 0 then
                    set endPoint to textIndexOf(myStr, endMark, 1) of me
                    if endPoint is 0 then exit repeat
                    -- 文字の削除
                    --delete text from character startPoint to character endPoint of paragraph i
                    removeStr(myTF, i, startPoint, endPoint) of me
                    set myStr to paragraph i
                    set startPoint to textIndexOf(myStr, startMark, 1) of me
                    if startPoint < 1 then exit repeat
                else
                    -- 次の行へ
                    exit repeat
                end if
            end repeat
        end repeat
    end tell
end tell

実行結果

f:id:mikomaya:20141112133308p:plain

InDesign 複数の文字を置換(ページ指定) AppleScript

2-3 03 特定のページのテキストフレーム内複数の文字を置換する(P.116)

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

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

=== 書籍の JavaScriptAppleScript で書き直してみます ===

部品作製

  • 置換キーワードファイル[replace.txt]

f:id:mikomaya:20141112111058p:plain

f:id:mikomaya:20141112111117p:plain

on replaceWords(myObj, repWords) -- 複数置換処理
on replaceWord(myObj, kw, rw) -- 置換処理
on split(myStr, mySep) -- 文字列を[mySep]で分割 -> リスト
on join(myList, mySep) -- リストを[mySep]で連結 -> 文字列

上記ハンドラは、以下のエントリで実装済みです。

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

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 -- 各行に分割
set targetPage to {1, 3} -- 置換対象ページ

tell application "Adobe InDesign CS6"
    tell active document
        repeat with myPage in targetPage
            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

実行結果

f:id:mikomaya:20141112113711p:plain

InDesign 複数の文字を置換 AppleScript

2-3 02 全てのページのテキストフレーム内複数の文字を置換する(P.113)

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

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

=== 書籍の JavaScriptAppleScript で書き直してみます ===

部品作製

  • 置換キーワードファイル[replace.txt]

f:id:mikomaya:20141112111058p:plain

f:id:mikomaya:20141112111117p:plain

-- 複数置換処理
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

実行結果

f:id:mikomaya:20141112111129p:plain