Program does not work in loop I've set up.

The code below is supposed to access a directory of my choice, cycle through the files (txt and jpg) in the chosen directory and then use the file name of each element of the directory to replace a dummy variable in an HTML file with the name of the file element and then rewrite the new HTML text to a new file whose name is the same as the original file element with an HTM suffix instead of JPG. It also ignores any files in the directory that are not JPG files.

My problem is that when I did the processing of a file without the loop, everything worked fine, but with the loop, the HTM file is created in the directory, but nothing is being written into the new HTM file. What am I doing wrong:/?

Thanks to everybody who created the code I’ve used searching Macscripter in this effort and thanks for taking the time to read this post.

set this_folder to (choose folder with prompt "Pick the folder containing the files to process:") as string
tell application "System Events"
	set a_list to every file of folder this_folder
end tell
repeat with myfile in a_list
	set n to name of myfile
	if n is not equal to ".DS_Store" then
		set SearchDelim to {"xxxxxx.jpg"}
		set ReplaceDelim to {n}
		display dialog SearchDelim
		--set TheNewFile to " " as alias
		set OldDelim to AppleScript's text item delimiters
		
		--select the text, might work best to replace with a drag and drop handlet for this script.
		set TheFile to (path to desktop as Unicode text) & "template.htm" as alias
		set the file_extension to the last word of n
		if file_extension = "jpg" then
			set n to ((text 1 thru ((length of n) - 4) of n)) & ".htm"
			set TheNewFile to this_folder & n
			
			try --error handling to make sure that the file is closed if there is a problem.
				open for access TheFile with write permission
				set TheText to read TheFile
				repeat with i from 1 to count of SearchDelim
					set AppleScript's text item delimiters to item i of SearchDelim
					--search items used as text item delimiters, line below returns a list of text between instances of the list item but not the list item's text.
					set TextList to every text item of TheText
					set AppleScript's text item delimiters to item i of ReplaceDelim
					--replace items set up as text item delimiters which are inserted in between the list items returned above when it is coerced into a string below.
					set TheText to TextList as string
				end repeat
				open for access TheNewFile with write permission
				set eof TheNewFile to 0 --make sure we are writing to the beginning of the file
				write TheText to TheNewFile
				close access TheFile
				close access TheNewFile
			on error
				close access TheFile
			end try
		end if
	end if
end repeat

set AppleScript's text item delimiters to OldDelim

Model: iMac G-4
AppleScript: 2.2.1 (100.1)
Browser: Firefox 3.0.4
Operating System: Mac OS X (10.5)

Well, here’s the finished product with some efficiencies added. The problem was in the original naming of TheNewFile.

–Open Directory with which you want to work and from that directory choose files that meet certain parameters
set this_folder to (choose folder with prompt “Pick the folder containing the files to process:”) as string
tell application “System Events”
set a_list to every file of folder this_folder whose (name extension = “jpg”) and (name does not contain “small”)
end tell

–choose html template you want to use
set TheFile to (choose file with prompt “Which HTML template file do you want to use?”) as alias

–loop to cycle through entire set of files chosen earlier
repeat with myfile in a_list
set n to name of myfile

--what do you want to look for and what do you want to replace it with.
set SearchDelim to {"xxxxxx.jpg"}
set ReplaceDelim to {n}
set OldDelim to AppleScript's text item delimiters

--create new html file name from current jpg file name
set n to ((text 1 thru ((length of n) - 4) of n)) & ".htm"
set TheNewFile to (open for access file (this_folder & n) with write permission)


try --error handling to make sure that the file is closed if there is a problem.
	
	
	set TheText to read TheFile
	
	--search and replace loop
	repeat with i from 1 to count of SearchDelim
		set AppleScript's text item delimiters to item i of SearchDelim
		--search items used as text item delimiters, line below returns a list of text between instances of the list item but not the list item's text.
		set TextList to every text item of TheText
		set AppleScript's text item delimiters to item i of ReplaceDelim
		--replace items set up as text item delimiters which are inserted in between the list items returned above when it is coerced into a string below.
		set TheText to TextList as string
	end repeat
	
	
	--The finished product is now written to the new file and that file is closed
	write TheText to TheNewFile
	close access TheNewFile
on error
	close access TheFile
	close access TheNewFile
end try

end repeat

set AppleScript’s text item delimiters to OldDelim