スクスニップ

AppleScriptの断片をここに書く

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