2013/03/17 miで文字置換v2
Macのテキストエディタ「mi」でオープン中のテキストファイルの文字置換を行うAppleScriptのサンプルです。
miの新しいバージョン3.0betaが出たので、AppleScript系の機能をチェックしていたところ……以前に掲載したmiの文字置換ルーチンの内容を見て、
「何を無駄な処理をしているんだろう?」
と気付き、書き換えたものです。
以前に掲載したものは、1行ごとに取り出して、行ごとに置換していました(←バカ)。AppleScriptのtext item delimiterの性質を考えると、大量のデータを一気に置換するように組まないとスピードが出ないわけで……データを小分けにして置換してもぜんぜん意味がありません。
たぶん、テキストの行数が増えれば増えるほど所要時間が変わってくるはずです。
mi 3.0betaについては、まだまだ作成途上という印象で……AppleScript系の機能についても、まだ各種オブジェクトからのプロパティを取得できないとか、大幅に刷新されたGUIに対するオブジェクトの定義があるんだかないんだか分らなかったり、というところです。
miのライバルは、フリー配布されていながら機能豊富なTextWraglerだと思います。
差分表示機能を強化しているあたり、このTextWranglerやらJeditやらを意識していると思いますが、最近Windowsから移行してきたユーザーが必要としていると思われるCP932のテキストのサポートあたりで差別化すべきではないかと。
スクリプト名:miで文字置換v2 |
script spd property inList : missing value property outList : missing value end script set targStr to “ぴよ” –置換対象文字列 set repStr to “ぷよ” –置換後文字列 set inList of spd to {} set outList of spd to {} –最前面のドキュメントから本文を取得 tell application “mi” tell document 1 set (inList of spd) to content end tell end tell –置換処理 set (outList of spd) to repChar((inList of spd), targStr, repStr) of me –最前面のドキュメント本文に置換結果を戻す tell application “mi” tell document 1 set content to (outList of spd) end tell end tell –文字置換ルーチン on repChar(origText, targStr, repStr) set {txdl, AppleScript’s text item delimiters} to {AppleScript’s text item delimiters, targStr} set temp to text items of origText set AppleScript’s text item delimiters to repStr set res to temp as text set AppleScript’s text item delimiters to txdl return res end repChar |