AppleScriptで文字置換を行う場合には、いくつかの方法があります。
①text item delimitersを利用する
AppleScriptの登場以来、利用されてきた方法です。AppleScriptの処理系では最速といってよい方法ですが、複数の箇所を一気に置換するのでやや過剰なのと処理方法が独特なので敬遠する人もいるようです。
また、処理対象の文字列のサイズが数Mバイト以上になると、処理が終了しなかったりします。巨大な文字列の置換には⑤が推奨されます。CotEditorのようなScriptableなテキストエディタの機能を利用するのもアリでしょう。
AppleScript名:文字置換(最短).scpt |
set origText to "abcdefg" set targStr to "de" set repStr to "xx" set a to repChar(origText, targStr, repStr) of me –> "abcxxfg" –Written By Philip Aker –文字置換ルーチン on repChar(origText as string, targStr as string, repStr as string) 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 |
②shellコマンドを利用する
do shell scriptコマンド経由で置換コマンドを利用する方法です。Mac OS X以降後に利用できるようになったものですが、日本語に対して正確に置換できるのか不安が残るので、個人的にはあまり積極的には利用していません。
③OSAXを利用する
macOS 10.15でサードパーティのOSAX(Scripting Additions)が廃止されたため、現在では利用できません。
④AppleScript Librariesを利用する
Shane StanleyのRegexAndStuffLibなどのライブラリを用いて正規表現を用いた文字置換を利用するものです。
⑤Cocoaの機能を利用する
Cocoaの機能を呼び出して文字置換を行うものです。いくつも利用できる機能があります。正規表現を使うものなど、お好きなものを利用してください。
AppleScript名:ASOCで文字置換5.scptd |
— Created 2015-06-30 by Takaaki Naganoya use AppleScript version "2.4" use scripting additions use framework "Foundation" set a to "あいうえお++かきくけこ" set b to cleanUpText(a, "+", "●") –> "あいうえお●●かきくけこ" on cleanUpText(someText, targStr, repStr) set theString to current application’s NSString’s stringWithString:someText set targString to current application’s NSString’s stringWithString:targStr set repString to current application’s NSString’s stringWithString:repStr set theString to theString’s stringByReplacingOccurrencesOfString:targString withString:repString options:(current application’s NSRegularExpressionSearch) range:{location:0, |length|:length of someText} return theString as text end cleanUpText |
これらに加えて、もっと簡単な方法がないかと探してみたら、
⑥offset ofで文字列検索して置換を行う
offset ofで置換対象の文字列を検索して置換を行います。オリジナル文字列に対して置換対象の文字列の位置が「冒頭ではない」「末尾ではない」という条件がそろっていて、かつ置換対象が0ないし1回しか対象文字列中に存在しないことが事前に分かりきっている場合にのみ使えます。
set urlData to "https://www.amazon.co.jp/gp/css/summary/print.html/ref=oh_aui_ajax_invoice"
set anOffset to offset of "/gp/" in urlData
set newURL to (text 1 thru (anOffset + (length of "/gp/") – 1) of urlData) & "legacy" & (text (anOffset + (length of "/gp/") – 1) thru -1 of urlData)
–> "https://www.amazon.co.jp/gp/legacy/css/summary/print.html/ref=oh_aui_ajax_invoice"
★Click Here to Open This Script
このぐらいで使えますが、本当にすべてのパターンを利用するには、場合分けしつつ書く必要があります。シンプルなはずなのに、シンプルになっていないという。
AppleScript名:単純文字置換.scpt |
set urlData1 to "https://www.amazon.co.jp/gp/css/summary/print.html/ref=oh_aui_ajax_invoice" set newURL1 to replaceOne(urlData1, "/gp/", "/gp/legacy/") of me –> "https://www.amazon.co.jp/gp/legacy/css/summary/print.html/ref=oh_aui_ajax_invoice" set urlData2 to "https://www.amazon.co.jp/css/summary/print.html/ref=oh_aui_ajax_invoice/gp/" set newURL2 to replaceOne(urlData2, "/gp/", "/gp/legacy/") of me –> "https://www.amazon.co.jp/css/summary/print.html/ref=oh_aui_ajax_invoice/gp/legacy/" set urlData3 to "https://www.amazon.co.jp/css/summary/print.html/ref=oh_aui_ajax_invoice/gp/" set newURL3 to replaceOne(urlData3, "https://", "http://") of me –> "http://www.amazon.co.jp/css/summary/print.html/ref=oh_aui_ajax_invoice/gp/" on replaceOne(origStr as string, targStr as string, repStr as string) set anOffset to offset of targStr in origStr if anOffset = 0 then return origStr set {aLen, bLen} to {length of origStr, length of targStr} if anOffset + bLen > aLen then –置換対象文字が末尾にある場合 set newStr1 to (text 1 thru (anOffset – 1) of origStr) set newStr2 to "" else if anOffset = 1 then –置換対象文字が先頭にある場合 set newStr2 to (text (anOffset + ((length of targStr))) thru -1 of origStr) set newStr1 to "" else –通常パターン set newStr1 to (text 1 thru (anOffset – 1) of origStr) set newStr2 to (text (anOffset + ((length of targStr))) thru -1 of origStr) end if return newStr1 & repStr & newStr2 end replaceOne |