extracting a chunk of information from a file?


I’m pretty new at this, but am trying to read a text file and extract a specific piece of information from the file (say, in the example below, the information on the line prefixed by SaveRGBImagesPrefix and ending at the return).

I know how to open a file, access it, and close it, but haven’t done much with reading and using delimiters (I’m guessing that’s what’s called for here…). Is there a good generalized routine for reading a text file until a given phrase, and ending with a return?

Here’s an example of the contents of the text file:

Thanks in advance for your help.

– jpburns

If your text files aren’t huge, and you don’t need to change anything in the file when you find the targeted text, this might work. It reads the entire file and then looks for the line containing the targeted text. If your text files are large, you might need to read the file in chunks.

set foo to read file "path:to:file"
set foundIt to "Target text not found."

set oldDelims to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {ASCII character 13}
	set bar to text items of foo
	repeat with thisItem in bar
		if thisItem contains "SaveRGBImagesPrefix" then
			set foundIt to thisItem
			exit repeat
		end if
	end repeat
	set AppleScript's text item delimiters to oldDelims
on error
	set AppleScript's text item delimiters to oldDelims
end try

display dialog foundIt