スクスニップ

AppleScriptの断片をここに書く

InDesign 文末の句点「。」を削除 AppleScript

2-4 04 選択されたテキストフレーム内の文章の最後が「。」なら削除する(P.123)

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

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

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

部品作製

f:id:mikomaya:20141112174217p:plain

-- 各段落の先頭が全角スペース(複数)なら削除
to removeLastMark(myObj, cutChar)
    tell application "Adobe InDesign CS6"
        tell myObj
            -- 文末の文字を取得
            set lastChar to last character of last paragraph
            if lastChar is cutChar then
                -- 文末の文字を空に
                set contents of last character of last paragraph to ""
                set myMsg to "文末の「。」を削除しました。" & return
            else
                set myMsg to ""
            end if
            select myObj
            if overflows is true then
                display dialog myMsg & "このテキストフレームはオーバーフローしています"
            else
                display dialog myMsg & "このテキストフレームはオーバーフローしていません"
            end if
        end tell
    end tell
end removeLastMark

-- 文字列の先頭の[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

部品が揃ったので、実行

on run {}
    my setUp()
    my main()
    my tearDown()
end run

on setUp()
    tell document 1 of application "Adobe InDesign CS6"
        select every text frame of page 1
    end tell
end setUp

on main()
    set cutChar to "。"
    
    tell document 1 of application "Adobe InDesign CS6"
        set myList to selection -- 選択されたオブジェクトを変数に保存
        set loop to number of every item of myList
        repeat with i from 1 to loop
            set myTF to item i of myList
            removeLastMark(myTF, cutChar) of me
        end repeat
    end tell
end main

on tearDown()
    activate
    display dialog "Script 終了" giving up after 3
end tearDown

実行結果

f:id:mikomaya:20141112174230p:plain

f:id:mikomaya:20141112174232p:plain

f:id:mikomaya:20141112174233p:plain