Replace a text part from x to y

hey everyone,

i am trying to figure out how i could replace something from point a to point y in a text file. i know how to make applescript read the file and stuff, i just want to know how the applescript would set the replace_code to all the characters from to

eg.: it should replace everything from to , to NEWSTUFF

so:

OLDSTUFF

should be replace with

NEWSTUFF

i cannot use the general replace method because there are always different characters in between the tags.

i presume it has to do with offsets?

lastly, i want it to go with the general search/replace snippet:

on do_replace(find_code, replace_code, file_source)
	set as_td to AppleScript's text item delimiters
	set AppleScript's text item delimiters to find_code
	set file_source to text items of file_source
	set AppleScript's text item delimiters to replace_code
	set file_source to "" & file_source
	set AppleScript's text item delimiters to as_td
	return file_source as «class utf8»
end do_replace

so basically i need something that will work like this imaginary one:

set offset 1 to "<title>" in file_source
set offset 2 to "</title>" in file_source
set find_code to all characters from and including offset 1 to and including offset 2

thanks,

max

Hi Max,
If I understand you correctly this should do the trick!

set file_source to "<title>OLDSTUFF</title>"

set find_code to "<title>"
set replace_code to "<hello>"
do_replace(find_code, replace_code, file_source)
set file_source to the result

set offset1 to offset of ">" in file_source
set offset2 to offset of "</" in file_source
set find_code to characters (offset1 + 1) thru (offset2 - 1) in file_source as string
set replace_code to "NEWSTUFF"
do_replace(find_code, replace_code, file_source)
set file_source to the result

set find_code to "</title>"
set replace_code to "</hello>"
do_replace(find_code, replace_code, file_source)

on do_replace(find_code, replace_code, file_source)
	set as_td to AppleScript's text item delimiters
	set AppleScript's text item delimiters to find_code
	set file_source to text items of file_source
	set AppleScript's text item delimiters to replace_code
	set file_source to "" & file_source
	set AppleScript's text item delimiters to as_td
	return file_source as «class utf8»
end do_replace

Thanks,
Nik

Coercing a list to a string involves AppleScript’s text item delimiters; Try text instead.

text (offset1 + 1) thru (offset2 - 1) of file_source

Thanks for the tip Bruce,
I’ve used this procedure so many times and never given a thought to the needles coercing.:slight_smile:
Nik

nik - your method is good. but the problem is that the tag ends in /> and not in . the problem is then, that /> exists about a million times.

but this offset THRU method seems excellent. i will give it a shot :smiley:

thanks,

max

Hi Max,
if and are the specific values that you’re looking for then we just need to change the values for offset1 and offset2? This should get around the problem of it picking up anything that contains > or </. If you would like me to change the code so you can give it a try then please let me know.
Thanks,
Nik

Hi,

this is a different approach, it replaces only, if the tag and the value matches


set file_source to "<title>OLDSTUFF</title>
<title>OTHERSTUFF</title>"

set find_code1 to "<title>"
set find_code2 to "</title>"
set replace_code1 to "<hello>"
set replace_code2 to "</hello>"
set find_text to "OLDSTUFF"
set replace_text to "NEWSTUFF"

set {TID, text item delimiters} to {text item delimiters, find_code1 & find_text}
set file_source to text items of file_source
set text item delimiters to replace_code1 & replace_text
set file_source to file_source as text
set text item delimiters to replace_text & find_code2
set file_source to text items of file_source
set text item delimiters to replace_text & replace_code2
set file_source to file_source as text
set text item delimiters to TID
file_source --> "<hello>NEWSTUFF</hello>\r<title>OTHERSTUFF</title>"


hi everyone,

i solved it thanks to your help.

you all wrote lots - but i just needed one line of it:

set find_code to characters (offset1 + 1) thru (offset2 - 1) in file_source as string

one last thing, i copied the line above without the “as string.” it works without it as well - but would it be better to leave it in there? i actually have no clue for what that thing is… lol

thanks,

max

Hi Max,
I think, as Bruce suggested the best method is

set find_code to text (offset1 + 1) thru (offset2 - 1) in file_source

but there are always lots of ways to reach the same result it’s just some are more efficient code than others!
Nik

great :smiley: