Search for text in file and append name of enclosing folder

I’m trying to wrap my head around a script for a specific purpose. I need to 1) search for a specific file type in a range of folders, 2) search the file for a string of text after a certain point and before a certain point (before “hell” and after “kitty”, 3) then take the output and rename the enclosing folder fo the current file. Then repeat the process for any other files (of the same file type) in the range of folders the user gave at first.

So, I’ve tried to wrap my head around text item delimiters. But I can’t seem to get any output. Here is what I have so far (this excludes the renaming part and the search for specific file type):

set myStartPoint to "'calibre_id'>"
set myEndPoint to "</dc:identifier>"
set myEndResult to ""

set myFile to choose file
set myText to read myFile
set myNonShortenedList to my TextItemDelimiterSplit(myText, myStartPoint, 2, -1)

set the clipboard to myEndResult

on TextItemDelimiterSplit(theText, theTID, theFrom, theTo)
	try
		set my text item delimiters to theTID
		set myReturn to text items theFrom thru theTo of theText
		set my text item delimiters to {""}
		return myReturn
	on error
		return {""}
	end try
end TextItemDelimiterSplit

This copies an empty field to the clipboard. How can I at least get the correct text back on the clipboard? I don’t want to spoon fed, but I need some help if anyone is willing.

Operating System: Mac OS X (10.8)

In the example code you’ve posted myEndResult is never set except to “” on line 3. I think you’re missing an line:

set myStartPoint to "'calibre_id'>"
set myEndPoint to "</dc:identifier>"
set myEndResult to ""

set myFile to choose file
set myText to read myFile
set myNonShortenedList to my TextItemDelimiterSplit(myText, myStartPoint, 2, -1)
set myEndResult to my TextItemDelimiterSplit(myNonShortenedList, myEndPoint, 1, -2)

set the clipboard to myEndResult

on TextItemDelimiterSplit(theText, theTID, theFrom, theTo)
   try
       set my text item delimiters to theTID
       set myReturn to text items theFrom thru theTo of theText as string
       set my text item delimiters to {""}
       return myReturn
   on error
       return {""}
   end try
end TextItemDelimiterSplit

The code is greedy

Ah, thanks for that. It helps but I still get the same output, nothing. And since I’m a n00b I don’t know if the code being greedy is good or bad.

Thanks DJ Bazzie Wazzie