Top / AppleScript

Jedit Scripts

表1はHTMLとJeditで名前が付けられている色の対応表です。アプリケーションにより色の名前に対応するコードが全く異なることがありますが、JeditはHTMLに合わせてあるようです。
表1: 16色の名前とコード
(32768, 49152はアプリケーションによっては、それぞれ32767, 49151のことも)
HTMLでの名前RGBのコードJedit4での名前RGBのコード
black#000000{0, 0, 0}
gray#808080{32768, 32768, 32768}
silver#C0C0C0{49152, 49152, 49152}
white#FFFFFF{65535, 65535, 65535}
maroon#800000えび茶{32768, 0, 0}
red#FF0000{65535, 0, 0}
purple#800080{32768, 0, 32768}
fuchsia#FF00FFピンク{65535, 0, 65535}
green#008000{0, 32768, 0}
lime#00FF00黄緑{0, 65535, 0}
olive#808000うぐいす{32768, 32768, 0}
yellow#FFFF00{65535, 65535, 0}
navy#000080{0, 0, 32768}
blue#0000FF{0, 0, 65535}
teal#008080青緑{0, 32768, 32768}
aqua#00FFFF{0, 65535, 65535}

言うまでもないですけど、あるアプリケーションでの文字の色のコードなんかを調べるには、次のようなスクリプトを実行してみて、履歴や結果のウィンドウに表示されるのを確認すればいいですね。

tell application "Jedit4"
    get fore color of first word of selection of front document
    -- get style of first word of selection of front document
end tell

●TeXのコメントに色を付ける1

開いている前面の文書を検索して、\の直後でない%があったら行末までをコメントと見なして色を付けます。同様のことはmiを使えば、難なくできることですが。ちょっと変えれば、Perlとか別のプログラムのコメントも扱えるかな? (一見単純そうだけど、$#配列名とか文字列やフォーマットの中やマッチ演算子のデリミタなんかに#が使われ得るので厄介ですね)。TeXとは書いていますが、スクリプトからもわかるようにTeXの文法にしたがった文書ではなくても大丈夫です。一般的な文書をコンピュータで作成する場合にもコメントを使って、思いついた文章をとりあえず書いておいて、後で不要な場合は削除するなどといったやり方はいいかもしれません。(ところで、井桁(いげた)記号とも言われるPerlのコメントの記号である#は、キャンセルを表すものとして紙の書き間違ったところに付けたものに由来するのだと思います,なので%より#の方がコメントの印として相応しいのかもしれません,ですが#は集合論で要素数を表すことに用いたり、数の前にナンバー記号として置いたりといろいろな事柄に使われすぎていて、コンピューターにその違いを指示するのが大変だと思います)
tell front document of application "Jedit4"
    repeat with theParagraph in every paragraph
        find "^%.*$|[^\\\\]%.*$" in theParagraph with grep and zenkaku sensitive
        -- [^\\\\]?%.*$ではなぜか^%.*$のところが無視されるので
        -- つまり?があっても無視されて[^\\\\]%.*$と解釈されちゃう?
        -- でもこの正規表現も本当に適切なものなのか?たぶんいいと思うのですがご指摘下さい
        if result is true then
            tell selection
                if first character is not "%" then -- 余分な先頭文字を除くため
                    find "%.*$" in it with grep and zenkaku sensitive
                end if
                set fore color to {65535, 0, 65535}
                -- ピンクにしてみました,黒にすれば元に戻すスクリプトになりますね
                -- 以下のようにするとコメントの部分を削除することができます
                -- set contents to ""
                -- また、以下のようにすれば%を消して、アンコメント (コメントでなく) します
                -- set first character to ""
            end tell
        end if
    end repeat
end tell

●TeXのコメントに色を付ける2

上のスクリプトは文書全体を処理しますが、これは選択したパラグラフ (いわゆる行で、改行を区切りとして分けたもの) を対象にします。
tell front document of application "Jedit4"
    set first_paraid to paragraph number of first paragraph of selection
    set last_paraid to paragraph number of last paragraph of selection
    repeat with paraid from first_paraid to last_paraid
        find "^%.*$|[^\\\\]%.*$" in paragraph paraid with grep and zenkaku sensitive
        if result is true then
            if paragraph number of selection is paraid then
                -- このif文は不要に思えるんだけど、findでパラグラフを指定しているのに、
                -- 間違った(その上?の)パラグラフを処理する場合があるから
                tell selection
                    if first character is not "%" then
                        find "%.*$" in it with grep and zenkaku sensitive
                    end if
                    set fore color to {65535, 0, 65535}
                    -- 以下のようにすれば、選択行内にあるコメントの頭から%を取ってアンコメントします
                    -- 下のスクリプトの逆操作になりますね
                    -- set first character to ""
                end tell
            end if
        end if
    end repeat
    set selection to paragraphs first_paraid thru last_paraid
    -- 上の文はなくてもいいけど、処理したパラグラフを選択状態にする
end tell

●選択部分の行頭に%を付ける (TeXのコメントアウト)

mi, QuoEditなどに同様の機能がありますが、それを何とJeditでも実現します (大げさ)。単に検索・置換をしているだけですね。
tell front document of application "Jedit4"
    set first_paraid to paragraph number of first paragraph of selection
    set last_paraid to paragraph number of last paragraph of selection
    repeat with paraid from first_paraid to last_paraid
        replace "^" to "%" in paragraph paraid with grep, case sensitive and zenkaku sensitive
        -- set fore color of paragraph paraid to {65535, 0, 65535}
        -- 後でアンコメントする可能性があれば、無理に色を変えたりしない方がいいかも
    end repeat
    set selection to paragraphs first_paraid thru last_paraid
    -- 上の文はなくてもいいけど、処理したパラグラフを選択状態にする
end tell

●スタイル付きHTML文書作成支援

Tex-Edit Plus色付けされた文書をHTML化するスクリプトを真似たスクリプトです。Tex-Edit Plusの動作と比較すると、上のスクリプトなどもそうですが、かなり遅いです。EGWordなどでも同様のスクリプトが作れますが、ちょっとやってみた感じですが、もっと遅い気がします。行ごとや語ごとに処理するスクリプトなのが遅くなっている原因だと思いますが、他の書き方がわかりません。
set basicColors to {¬
    {name:"black", RGBcolor:{0, 0, 0}, hexRGB256:"000000"}, ¬
    {name:"gray", RGBcolor:{32768, 32768, 32768}, hexRGB256:"808080"}, ¬
    {name:"silver", RGBcolor:{49152, 49152, 49152}, hexRGB256:"C0C0C0"}, ¬
    {name:"white", RGBcolor:{65535, 65535, 65535}, hexRGB256:"FFFFFF"}, ¬
    {name:"maroon", RGBcolor:{32768, 0, 0}, hexRGB256:"800000"}, ¬
    {name:"red", RGBcolor:{65535, 0, 0}, hexRGB256:"FF0000"}, ¬
    {name:"purple", RGBcolor:{32768, 0, 32768}, hexRGB256:"800080"}, ¬
    {name:"fuchsia", RGBcolor:{65535, 0, 65535}, hexRGB256:"FF00FF"}, ¬
    {name:"green", RGBcolor:{0, 32768, 0}, hexRGB256:"008000"}, ¬
    {name:"lime", RGBcolor:{0, 65535, 0}, hexRGB256:"00FF00"}, ¬
    {name:"olive", RGBcolor:{32768, 32768, 0}, hexRGB256:"808000"}, ¬
    {name:"yellow", RGBcolor:{65535, 65535, 0}, hexRGB256:"FFFF00"}, ¬
    {name:"navy", RGBcolor:{0, 0, 32768}, hexRGB256:"000080"}, ¬
    {name:"blue", RGBcolor:{0, 0, 65535}, hexRGB256:"0000FF"}, ¬
    {name:"teal", RGBcolor:{0, 32768, 32768}, hexRGB256:"008080"}, ¬
    {name:"aqua", RGBcolor:{0, 65535, 65535}, hexRGB256:"00FFFF"}}
tell front document of application "Jedit4"
    replace tab to "    " in it -- タブを4つのスペースにしてみました
    replace "&" to "&" in it with case sensitive and zenkaku sensitive
    replace "\"" to """ in it with case sensitive and zenkaku sensitive
    replace "<" to "&lt;" in it with case sensitive and zenkaku sensitive
    replace ">" to "&gt;" in it with case sensitive and zenkaku sensitive
    replace "≤" to "&le;" in it with case sensitive and zenkaku sensitive
    replace "≥" to "&ge;" in it with case sensitive and zenkaku sensitive
    replace "«" to "&laquo;" in it with case sensitive and zenkaku sensitive
    replace "»" to "&raquo;" in it with case sensitive and zenkaku sensitive
    replace "≠" to "&ne;" in it with case sensitive and zenkaku sensitive
    replace "¬" to "&not;" in it with case sensitive and zenkaku sensitive
    -- replace "\\\\" to "&#92;" in it with case sensitive and zenkaku sensitive -- 円記号がバックスラッシュで表示される?
    -- replace "'" to "&#39;" in it with case sensitive and zenkaku sensitive -- シングルクォート、これもやるべきなのかしら?&apos;と書いてもいい?
    -- 必要ならば上のような文を、もっと追加するといいでしょう
    repeat with idx from (number of every style run) to 1 by -1
        -- 文書中の何番目であるかというのを利用している場合、後ろから扱うことでうまく処理できる
        -- この場合は前からでも大丈夫だと思うけど、前から処理すると番号 (順番) が変わってしまう場合があるので
        select style run idx
        tell selection
            set digitHex to "0123456789ABCDEF"
            set codeStyle to ""
            set theColor to fore color
            if theColor is not {0, 0, 0} then -- 黒のところはそのままにして、表1の残り15色だったら
                set theRGB256 to {(item 1 of theColor) div 256, (item 2 of theColor) div 256, (item 3 of theColor) div 256}
                repeat with idxRGB from 1 to 3
                    set decNum to item idxRGB of theRGB256
                    set hexNum to ((character (decNum div 16 + 1) of digitHex) & (character (decNum mod 16 + 1) of digitHex)) as string
                    set item idxRGB of theRGB256 to hexNum
                end repeat
                set theRGB256 to ((item 1 of theRGB256) & (item 2 of theRGB256) & (item 3 of theRGB256)) as string
                repeat with basicColor in basicColors
                    if (theColor is RGBcolor of basicColor) or (theRGB256 is hexRGB256 of basicColor) then
                        set codeStyle to "color:" & (name of basicColor) & ";"
                        exit repeat
                    end if
                end repeat
                if codeStyle is "" then set codeStyle to "color:#" & theRGB256 & ";"
            end if
            set theStyle to style
            if theStyle is not plain then -- プレインテクストでなかったら
                if theStyle contains bold then set codeStyle to codeStyle & space & "font-weight:bold;"
                if theStyle contains italic then set codeStyle to codeStyle & space & "font-style:italic;"
                if theStyle contains underline then set codeStyle to codeStyle & space & "text-decoration:underline;"
            end if
            -- 他にもスタイルの形式はありますが、一般にはこれで十分でしょうか
            if codeStyle is not "" then
                if first character of codeStyle is space then set codeStyle to text 2 thru -1 in codeStyle
                if last character of codeStyle is ";" then set codeStyle to text 1 thru -2 in codeStyle
                set contents to "<span style=\"" & codeStyle & "\">" & it & "</span>"
            end if
        end tell
    end repeat
end tell

Top / AppleScript