スクスニップ

AppleScriptの断片をここに書く

InDesign 段落先頭の全角スペース(複数)を削除 AppleScript

2-4 03 テキストフレーム内の段落の先頭の文字が全角空白(複数)なら削除する(P.122)

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

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

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

部品作製

f:id:mikomaya:20141112163836p:plain

-- 各段落の先頭が全角スペース(複数)なら削除
to removeBeginningSpaces(myObj, cutChar)
    tell application "Adobe InDesign CS6"
        tell myObj
            set loop to number of paragraph
            repeat with i from 1 to loop
                tell paragraph i
                    set endPos to getPrefixLength(contents, cutChar) of me
                    log "endPos: " & endPos
                    if endPos > 0 then
                        set startPos to 1
                        set contents of (text from character startPos to character endPos) to ""
                    end if
                end tell
            end repeat
        end tell
    end tell
end removeBeginningSpaces

-- 文字列の先頭の[myChar]の文字数を返す
on getPrefixLength(myStr, myChar)
    log myStr
    set loop to number of myStr
    repeat with i from 1 to loop
        if character i of myStr is not myChar then
            return i - 1
        end if
    end repeat
    return 0
end getPrefixLength

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

set cutChar to " "
tell application "Adobe InDesign CS6"
    tell active document
        set myObj to text frame 1
    end tell
end tell
removeBeginningSpaces(myObj, cutChar)

実行結果

f:id:mikomaya:20141112163849p:plain