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
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
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
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>"
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