Literally Replace a List of Strings with a List of Strings

Hey Folks,

I feel like I’ve asked this before, but I can’t find the post – nor can I find the answer in my library. If I have, and you know where – please point me to it.

The Satimage.osax can easily replace a literal list of strings with a list of strings.

set dataStr to "Now is the time for all good men to come to the aid of their country."
set newStr to change {"Now", "is", " the ", "men"} into {"NOW", "is a REALLY good", " ", "people"} in dataStr

--> "NOW is a REALLY good time for all good people to come to aid of their country."

I was thinking that AppleScriptObjC could also.

Or does it need to loop through using the stringByReplacingOccurrencesOfString method?

The Satimage.osax can also do this with case-sensitive on/off and can manage multiple regex patterns at once.

TIA.

-Chris

How about this?


– Created by: Takaaki Naganoya
– Created on: 2019/03/10

– Copyright © 2019 Piyomaru Software, All Rights Reserved

set origText to “Now is the time for all good men to come to the aid of their country.”
set repList to {{“Now”, “NOW”}, {“is”, “is a REALLY good”}, {" the ", " "}, {“men”, “people”}}

set bText to repCharList(origText, repList) of me
→ “NOW is a REALLY good time for all good people to come to aid of their country.”

on repCharList(origText as string, repList as list)
copy origText to tmpText

repeat with i in repList
copy i to {origStr, toStr}
set tmpText to repChar(tmpText, origStr, toStr) of me
end repeat

return tmpText
end repCharList

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

1 Like

You need to use a repeat loop. Or you can use my RegexAndStuffLib’s batch regex command – which does the repeat for you.

1 Like

Not what I wanted to know, but I like it.  :sunglasses:

-Chris