Mac OS X のエディタ Jedit X 用の misima 旧字・旧仮名変換対応 AppleScript ができた。
call soap のリターン値において Unicode テキストがエラーになる前回問題への対応として,以下の工夫を行った。
- misima で変換結果を "0xFFFF"(UCS 十六進数) 形式で出力するオプションをサポートする。
- call soap リターンをパラグラフに分割する。
- パラグラフごとに変換結果 Unicode 十六進テキストを print pack("U*", 十六進テキスト); に埋め込んで perl を do shell script で呼び出し,Unicode 文字列に復元する。
つまり,call soap のリターン値をすべて ASCII コードで表現することにより "Unknown Escape" エラーを回避でき,一方,Unicode テキストへの復元は Perl がやってくれると言うわけである。
完成した misima SOAP 変換 Jedit X 対応 AppleScript コードを掲載しておく。"¬" は行の折返しを示しており,前後を 1 行に連結して入力しなければならない。Jedit X のスクリプトウィンドウから登録して実行すれば,選択テキストを私の misima SOAP サーバで変換することができる。いずれ misima パラメータ指定などを整理して misima パッケージにも同梱して公開するつもりでいる。
-- misima パラメータ: 旧字,旧仮名,単純,用字・用語変換,十六進形式 ¬
-h 新設: misima 変換結果を Unicode 「0x十六進数」形式で返却
property misimaargs : "-kithy -s c"
-- 選択テキストを取得
tell application "Jedit X"
tell front document
copy selected text to tString
end tell
if (count characters of tString) = 0 then
display dialog "変換したい文字列を選択してください。" buttons {"OK"}
return
end if
end tell
-- misima 引数をセット
set misimaParm to misimaargs
-- misima SOAP Server call
tell application "http://yasuda.homeip.net/axis/services/¬
misimaSoapConnector"
set rText to (call soap {method name:"misimaConvert", ¬
parameters:{Params1:misimaParm as string, ¬
Params2:tString as Unicode text}})
end tell
-- 0xFFFF 形式を Unicode 文字列に復元
set the sText to misimacnv(rText)
-- 選択テキストを結果で置換
tell application "Jedit X"
tell front document
set selected text to sText
end tell
end tell
-- 0xFFFF 形式を Perl で文字に変換
on misimacnv(str)
-- パラグラフ参照を定義
script list_paragraphs
property contents : paragraphs of str
end script
-- Unicode テキスト処理
set rt to ""
repeat with line_item in contents of list_paragraphs
-- Perl print 文に十六進形式の変換結果を埋め込む。
set p to "print pack(\"U*\", " & (get contents of line_item) & ");"
-- 改行 0x000a を "\n\n"(改行 2 個) に置換。¬
(do shell script が行末改行を削除するため)
set the p to replace(p, ", 0x000a);", ") . \"\\n\\n\";")
set rt to rt & (do shell script "perl -e " & quoted form of p)
end repeat
return rt
end misimacnv
-- テキスト置換ハンドラ
on replace(src, tg, rp)
-- delimiter をバックアップ
set delmback to AppleScript's text item delimiters
-- 変換対象テキストを区切りにして分割
set AppleScript's text item delimiters to tg
set tmpList to text items of src
-- 置換テキストを区切りにして復元
set AppleScript's text item delimiters to rp
set cText to tmpList as string
-- delimitter を復元
set AppleScript's text item delimiters to delmback
-- 置換結果を返却
return cText
end replace
以下に Jedit X での変換の様子を示す。

Jedit X 変換前

Jedit X 変換後
しかしながら do shell script が行末改行を削除する仕様であることに気づくのには時間を要してしまった。Apple のテクニカルノート 2065 が救ってくれた。また O'REILLY の Matt Neuburg 著 "AppleScript, The Definitive Guide, 2nd Edition"(2006) は,Unicode の取り扱いがヘボな AppleScript で,Perl を呼び出すことで <<Unknown Escape>> エラーを回避するというヒントを与えてくれた。まさに Definitive 決定版である。AppleScript における SOAP, XML-RPC サポートに関する技術解説は Introduction to XML-RPC and SOAP Programming Guide を参照。インターネットを探しても,こういう Tips を公開している個人サイト,メーリングアーカイブはまれで,やはり Apple の技術文書や権威あるリファレンスに当たるべきとの思いを強くした。ちょっと特殊な道にそれると日本人の書いた文書は役に立たず,英文の解説に頼らざるをえなくなることが多い。何故か。
Oreilly & Associates Inc.
