get text between to values

Hi all,
I have a text file that I would like to search for a value and then search for another value after the point where the first value has been found and return the text between these two points. I have written the script below that gives me what I want but I don’t think it’s the best way of doing it. I’ve been using offset but I’m sure it must be better to use AppleScript’s text item delimiters. Advise please!

set thefile to choose file

set the_text to read thefile

set search_string_1 to "FONTS"
set my_offest to offset of search_string_1 in the_text
set new_text to characters my_offest thru end of the_text as string

set search_string_2 to "COLORS"
set my_offset_2 to offset of search_string_2 in new_text
set my_new_offset to (my_offset_2 as real) - 1
set newer_text to characters 1 thru (my_new_offset as string) of new_text as string

Thanks,
Nik

Something like this should work:

set thefile to choose file

set the_text to read thefile

set {astid, text item delimiters} to {text item delimiters, "FONTS"}
set t to text item 2 of the_text
set text item delimiters to "COLORS"
set newer_text to text item 1 of t
set text item delimiters to astid

Hope that helps…

  • Dan

Hi Dan,
that’s exactly what I meant. glad you understood my post.
Thanks for the help,
Nik