文字列の検索・置換

基礎知識

 text item delimiters(テキスト項目区切り)はAppleScriptそのものが持っている属性で、その値はOSを終了するまで保存されます。
 ですから、他のスクリプトに影響を与えないように、一時変数に退避して処理が終わったら戻す必要があります。
 また、テキストエディタなどのアプリケーションは独自にtext item delimitersを持っていることがありますので、それと明確に区別するためにはtext item delimiters of AppleScript(又はAppleScript's text item delimiters)と記述する必要があります。

text item delimiters of AppleScript
AppleScript's text item delimiters

 ちなみにdelimitersの最後のsを忘れがちなので、気をつけて下さい。
 今の所、一つしか判定されませんが、複数の区切りがリストで指定できるようになっています。
 ですから、正確には文字列("文字列")ではなく、文字列のリスト({"文字列1","文字列2"})を代入する必要があります。

一部を取り出す

 Macintoshのファイルのパスは、: (コロン)区切りなので、そこで分割できれば、何かと便利です。
 以下のスクリプトは、エイリアスやファイル参照からファイル名を取り出します。

ref2FileName(choose file) 

on ref2FileName(theFile)
	set tmp to AppleScript's text item delimiters
	set AppleScript's text item delimiters to ":"
	set fileName to last text item of (theFile as string)
	set AppleScript's text item delimiters to tmp
	return fileName
end ref2FileName

 以下のスクリプトは、逆にエイリアスやファイル参照からコンテナのパスを取り出します。
 コンテナは最後にコロンがついて無いといけないので、最後に足しています。

ref2Container(new file)

on ref2Container(theFile)
	set tmp to AppleScript's text item delimiters
	set AppleScript's text item delimiters to ":"
	set theContainer to text from text item 1 to text item -2 of (theFile as string)
	set AppleScript's text item delimiters to tmp
	return theContainer & ":"
end ref2Container

分割

 text itemというクラスは、text item delimitersを区切り文字とする文字列の単位です。
 ですから、text item delimitersに適当な区切り文字を代入すれば、柔軟な文字列の分割ができるという訳です。
 コンマ区切りやタブ区切りのデータ等は良く使うところでしょう。
 text item delimitersと複数形ですから、区切り文字は複数リストで指定することもできるのですが、実際に働くのはリストの一つ目だけです。ここでタブ(tab)と改行(return)を指定できると便利なのですが。

devText("1, 2, 3, 4, 5", ",") 

on devText(theText, aDelimiter)
	set tmp to AppleScript's text item delimiters
	set AppleScript's text item delimiters to aDelimiter
	set theList to every text item of theText
	set AppleScript's text item delimiters to tmp
	return theList
end devText

結合

 リストをas stringで文字列に変換すると、リストの値に間にはtext item delimitersが挟まれます。
 text item delimitersの初期値は空文字列("")なので、直接結合されているように見えますが、正確には""が挟まれている訳です。
 そこで、適当な区切り文字を設定してやれば、色々と便利に使えるという訳です。
 リストは文字列に変換できるものならば、文字列以外のものでも良く、別の種類のものの混在も可能であるところも魅力的です。

"<TR><TD>" & joinList({1, 2, 3, 4, 5}, "</TD><TD>") & "</TD></TR>"

on joinList(theList, aDelimiter)
	set tmp to AppleScript's text item delimiters
	set AppleScript's text item delimiters to aDelimiter
	set theText to theList as string
	set AppleScript's text item delimiters to tmp
	return theText
end joinList

置換

 こんどは、分割と結合を組み合わせて、置換を実行してみましょう。
 以下のサンプルでは、1characterの置換ですが、もっと長い文字列の置換も可能です。
 text item delimitersという名前から、短いものを想像しがちですが、長い文字列も区切りに使用することができるわけです。

replaceText("http://www.tonbi.jp/AppleScript/", "/", ":")

on replaceText(theText, serchStr, replaceStr)
	set tmp to AppleScript's text item delimiters
	set AppleScript's text item delimiters to serchStr
	set theList to every text item of theText
	set AppleScript's text item delimiters to replaceStr
	set theText to theList as string
	set AppleScript's text item delimiters to tmp
	return theText
end replaceText

検索

 さらに応用を続けまして、検索を行ってみることにしましょう。
 検索文字を発見した位置を全てリストに返します。発見できなかった時は空リストを返します。

findText("アップルの株がアップしている今日この頃", "アップ")

on findText(theText, serchStr)
	set tmp to AppleScript's text item delimiters
	set AppleScript's text item delimiters to serchStr
	set theText to every text item of theText
	set theList to {}
	set x to 0
	set delNum to number of serchStr
	repeat with curItem in theText
		set x to x + (number of curItem)
		set end of theList to x + 1
		set x to x + delNum
	end repeat
	set AppleScript's text item delimiters to tmp
	if (number of theList) = 1 then return {}
	return items 1 thru -2 of theList
end findText

 Tanaka's osaxの MT Replaceを使えば、文字列の置換ができます。MT Search Positionを使えば、文字列中の位置の検索もできます。


2000-02-29 2000-03-30 2001-05-28 2001-06-22