Calling handler generates: "Exit statement was not in a repeat loop"

Each time I run the following script, I get the following error; “Exit statement was not in a repeat loop.” when it calls the first handler. prior to this, I was getting the script to run through, but the event log simply showed addTheText() over and over and over - but it never actually executed.

Can anyone figure out why?

set folderLocation to (choose folder with prompt "Where is the folder you would like to work on?") as string
set allFolders to list folder folderLocation without invisibles
set folderCount to the number of items in allFolders

set n to 9
set pageNum to 1

set imageFolder to ""
set htmlTemplate to "TXT document with basic html format" as string
set inventoryDocument to (imageFolder & " inventory" & pageNum & ".html") as string
set anItem to ""
set anItem2 to ""
set anItem3 to ""
set anItem4 to ""
set anItem5 to ""
set x to ""
set n to ""

global pageNum, inventoryDocument, anItem, anItem2, anItem3, anItem4, anItem5, htmlTemplate, x, n

repeat with y from 1 to folderCount
	
	set imageFolderName to item y of allFolders
	set imageFolder to (folderLocation & imageFolderName) as string
	set allFiles to list folder imageFolder without invisibles
	set allFilesCount to the number of items in allFiles
	
	tell application "TextWrangler"
		open file htmlTemplate
	end tell
	
	repeat with x from 1 to allFilesCount by 5
		tell application "TextWrangler"
			if x = "" then
				exit repeat
			else
				tell me to addTheText()
				if (x + 4 = 100) then saveTheDocument()
			end if
		end tell
	end repeat
	tell application "TextWrangler"
		tell front document
			save front document to file inventoryDocument
			close front document
			set pageNum to 1
			set n to 9
		end tell
	end tell
end repeat

on addTheText()
	tell application "TextWrangler"
		tell front document
			try
				set anItem to (item x of allFiles)
			on error
				exit repeat
			end try
			try
				set anItem2 to (item (x + 1) of allFiles)
			on error
				exit repeat
			end try
			try
				set anItem3 to (item (x + 2) of allFiles)
			on error
				exit repeat
			end try
			try
				set anItem4 to (item (x + 3) of allFiles)
			on error
				exit repeat
			end try
			try
				set anItem5 to (item (x + 4) of allFiles)
			on error
				exit repeat
			end try
			tell line n
				set contents to ("<a href=" & anItem & "><img src=" & anItem & " width=" & (ASCII character 34) & "170" & (ASCII character 34) & "></a> <a href=" & anItem2 & "><img src=" & anItem2 & " width=" & (ASCII character 34) & "170" & (ASCII character 34) & "></a> <a href=" & anItem3 & "><img src=" & anItem3 & " width=" & (ASCII character 34) & "170" & (ASCII character 34) & "></a> <a href=" & anItem4 & "> <img src=" & anItem4 & " width=" & (ASCII character 34) & "170" & (ASCII character 34) & "></a> <a href=" & anItem5 & "><img src=" & anItem5 & " width=" & (ASCII character 34) & "170" & (ASCII character 34) & "></a> <br>" & return) as string
			end tell
			set n to (n + 1)
		end tell
	end tell
end addTheText

to saveTheDocument()
	tell application "TextWrangler"
		tell front document
			tell line n to set conents to ("<br><br><a href=" & imageFolder & " inventory" & (pageNum + 1) & ".html>Next Page</a>") as string
			save front document to file inventoryDocument
			close front document
			set pageNum to (pageNum + 1)
		end tell
	end tell
end saveTheDocument

Hi Scott,

AppleScript’s right.
In the addTheText() handler are a couple of exit repeats but no repeat loop

In the addTheText handler, you are using an exit statement when you are not inside a repeat. Offhand, it looks you could replace that handler with this.

on addTheText()
	tell application "TextWrangler"
		tell front document
			try
				set anItem to (item x of allFiles)
				set anItem2 to (item (x + 1) of allFiles)
				set anItem3 to (item (x + 2) of allFiles)
				set anItem4 to (item (x + 3) of allFiles)
				set anItem5 to (item (x + 4) of allFiles)
			on error
				return false
			end try
			
			set quoteChar to ASCII character 34
			tell line n
				set contents to ("<a href=" & anItem & "><img src=" & anItem & " width=" & quoteChar & "170" & quoteChar & "></a> <a href=" & anItem2 & "><img src=" & anItem2 & " width=" & quoteChar & "170" & quoteChar & "></a> <a href=" & anItem3 & "><img src=" & anItem3 & " width=" & quoteChar & "170" & quoteChar & "></a> <a href=" & anItem4 & "> <img src=" & anItem4 & " width=" & quoteChar & "170" & quoteChar & "></a> <a href=" & anItem5 & "><img src=" & anItem5 & " width=" & quoteChar & "170" & quoteChar & "></a> <br>" & return)
			end tell
			set n to (n + 1)
		end tell
	end tell
	return true
end addTheText

.and replace the call to the handler with this:

tell me to addTheText()
if result is false then exit repeat

Edit: Side note: If your script will only be running on Mac OS X v10.4 or later, then you could replace quoteChar with AppleScript’s predefined quote value:

-- this is only defined in AppleScript v1.10 or later
quote

Thanks, guys.

I just was not seeing it.

ooooooooohhhhh… missed that news!