スクスニップ

AppleScriptの断片をここに書く

InDesign 表のセル内容が負数の場合、マイナスを削除し「▲」にする AppleScript

2-5 02 表のセル内容が負数の場合、マイナスを削除し「▲」にする(P.128)

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

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

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

部品作製

f:id:mikomaya:20141114105328p:plain

  • AppleScript ハンドラ
    • tableList(myObj) -- オブジェクト内の全ての表を返す
    • tableSize(myTable) -- 表の大きさを返す(横,縦のセル数)
    • tCell(myTable, cIdx, rIdx) -- [cIdx]番目の項目の[rIdx]行目のセルオブジェクトを返す

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

以下のハンドラも前回のものを少し修正するだけで対応できます。

-- 表内の全てのセルを調査する
on tableWalk(myTable)
    set myColor to getColor("KINAKA")
    set {cMax, rMax} to tableSize(myTable)
    repeat with rIdx from 1 to rMax
        repeat with cIdx from 1 to cMax
            set myCell to tCell(myTable, cIdx, rIdx)
            --minus2red(myCell, myColor)
            minus2sankaku(myCell) -- <== ※
        end repeat
    end repeat
end tableWalk

-- セルの1文字目が「-」なら、「-」を「▲」に置換する
on minus2sankaku(myCell)
    tell application "Adobe InDesign CS6"
        if contents of first character of myCell is "-" then
            set character 1 of myCell to "▲" -- <== ※
        end if
    end tell
end minus2sankaku

部品が揃ったので、実行

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()
    -- 色を追加
    --addProcessColor("KINAKA", {0, 100, 100, 0})
    
    -- 指定したテキストフレーム内の全てのテーブルオブジェクトを取得する
    tell document 1 of application "Adobe InDesign CS6"
        set myTF to text frame 1 of page 1
    end tell
    set tableList to tableList(myTF)
    
    -- テーブル数分繰り返す
    set loop to number of tableList
    tell application "Adobe InDesign CS6"
        repeat with i from 1 to loop
            tableWalk(item i of tableList) of me
        end repeat
    end tell
end main

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

実行結果

f:id:mikomaya:20141114105358p:plain

InDesign 表のセル内容が負数の場合は赤文字にする AppleScript

2-5 01 表のセル内容が負数の場合は赤文字にする(P.127)

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

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

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

部品作製

f:id:mikomaya:20141113192605p:plain

-- プロセスカラーを追加する
on addProcessColor(myName, myCMYK)
    set cNames to colorNames()
    if myName is not in cNames then
        tell document 1 of application "Adobe InDesign CS6"
            make new color with properties {name:myName, model:process, space:CMYK, color value:myCMYK}
        end tell
    else
        activate
        display dialog "Color:[" & myName & "]は既に存在します。" & return & "(追加できません)"
    end if
end addProcessColor

-- 全ての色名を返す
on colorNames()
    tell document 1 of application "Adobe InDesign CS6"
        name of every color
    end tell
end colorNames

-- 指定された名前のカラーオブジェクトを返す
-- 見つからなければ color 1 を返す
on getColor(cName)
    tell document 1 of application "Adobe InDesign CS6"
        try
            color cName
        on error
            color 1
        end try
    end tell
end getColor
  • AppleScript ハンドラ等(表に関するもの)

表を扱う専用のAppleScriptライブラリを用意するのが望ましいが、今回はベタに書く。

-- オブジェクト内の全ての表を返す
on tableList(myObj)
    tell document 1 of application "Adobe InDesign CS6"
        every table of myObj
    end tell
end tableList

-- 表内の全てのセルを調査する
on tableWalk(myTable)
    set myColor to getColor("KINAKA")
    set {cMax, rMax} to tableSize(myTable)
    repeat with rIdx from 1 to rMax
        repeat with cIdx from 1 to cMax
            set myCell to tCell(myTable, cIdx, rIdx)
            minus2red(myCell, myColor)
        end repeat
    end repeat
end tableWalk

-- 表の大きさを返す(横,縦のセル数)
on tableSize(myTable)
    tell application "Adobe InDesign CS6"
        {number of columns, number of rows} of myTable
    end tell
end tableSize

-- [cIdx]番目の項目の[rIdx]行目のセルオブジェクトを返す
on tCell(myTable, cIdx, rIdx)
    tell application "Adobe InDesign CS6"
        cell cIdx of row rIdx of myTable
    end tell
end tCell

-- セルの1文字目が「-」なら、文字色を変更する
on minus2red(myCell, myColor)
    tell application "Adobe InDesign CS6"
        if contents of first character of myCell is "-" then
            set fill color of every character of myCell to myColor
        end if
    end tell
end minus2red

  • テーブル・セルオブジェクトの叩き方あれこれ
-- 指定したテキストフレーム内の全てのテーブルオブジェクトを取得する
tell document 1 of application "Adobe InDesign CS6"
    set myTF to text frame 1 of page 1
end tell
set tableList to tableList(myTF)


tell application "Adobe InDesign CS6"
    -- table 1
    tell item 1 of tableList
        set t1cNum to number of column -- 表の項目数
        set t1rNum to number of rows -- 表の行数
        cell 2 of row 2 -- 2行目の2項目目のセルオブジェクト
        set t1cVal to contents of cell 2 of row 2 -- 2行目の2項目目のセルオブジェクトの内容
    end tell
    
    -- table 2 のセル(2項目目の3行目)
    set myCell to tCell(item 2 of tableList, 2, 3) of me
    tell myCell
        set cVal to contents of first character --セルの最初の文字
        set cLen to number of every character of contents --セル内の文字数
        fill color of character 1 --セルの最初の文字色
        set cColor to name of fill color of character 1 --セルの最初の文字色の名前
    end tell
end tell

log "--------------- TABLE 1 ---------------"
log "T1-表の項目数: " & t1cNum
log "T1-表の 行数: " & t1rNum
log "T1-C2R2: " & t1cVal
log "--------------- TABLE 2 CELL(2,3) ---------------"
log "T2-C2R3-char 1: " & cVal
log "T2-C2R3-length: " & cLen
log "T2-C2R3-color: " & cColor

f:id:mikomaya:20141113192445p:plain

部品が揃ったので、実行

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()
    -- 色を追加
    addProcessColor("KINAKA", {0, 100, 100, 0})
    
    -- 指定したテキストフレーム内の全てのテーブルオブジェクトを取得する
    tell document 1 of application "Adobe InDesign CS6"
        set myTF to text frame 1 of page 1
    end tell
    set tableList to tableList(myTF)
    
    -- テーブル数分繰り返す
    set loop to number of tableList
    tell application "Adobe InDesign CS6"
        repeat with i from 1 to loop
            tableWalk(item i of tableList) of me
        end repeat
    end tell
end main

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

実行結果

f:id:mikomaya:20141113192801p:plain

InDesign 段落の先頭の文字が「■」ならスタイルを適用 AppleScript

2-4 04 選択された段落の先頭の文字が「■」ならスタイルを適用する(P.124)

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

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

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

部品作製

f:id:mikomaya:20141113154618p:plain

f:id:mikomaya:20141113154629p:plain

-- 各段落の先頭が「■」なら段落スタイルを適用
to applyParagraphStyle(myObj, checkMark)
    tell application "Adobe InDesign CS6"
        
        -- 段落スタイルを変数に保存
        set pStyle to paragraph style "段落見出し" of document 1
        
        tell myObj
            -- 行数分繰り返す
            set loop to number of every paragraph
            repeat with i from 1 to loop
                
                -- 行頭文字 = ■?
                set myChar to first character of paragraph i
                if myChar is checkMark then
                    -- << javascript >> .applyParagraphStyle(pStyle)
                    apply paragraph style paragraph i using pStyle
                end if
                
            end repeat
        end tell
    end tell
end applyParagraphStyle

段落スタイル、文字スタイル、swatchなどの情報はそれぞれのファイルで保持しているので、 … of document 1 という記述になります。(of document 1 を削除して実行するとエラーに落ちます)

部品が揃ったので、実行

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 checkMark 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
            applyParagraphStyle(myTF, checkMark) of me
        end repeat
    end tell
end main

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

実行結果

f:id:mikomaya:20141113154639p:plain

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

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