InDesign script to combine duplicate listings needs help.

I am trying to get a script to go through a listing and combine identical listings and put the page numbers together. It works fine when the identical lists are only two, but when there are three or more, the whole thing gets screwed up. I get no errors, but the end results are jumbled.

I am guessing it has much to do with the fact I am deleting paragraphs in the repeat loop, but I am not sure how to get around this. Any help would be greatly appreciated.

tell application "Adobe InDesign CS4"
	set enable redraw of script preferences to false
	set find text preferences to nothing
	set case sensitive of find change text options to false
	set include footnotes of find change text options to false
	set include hidden layers of find change text options to false
	set include locked layers for find of find change text options to true
	set include locked stories for find of find change text options to true
	set include master pages of find change text options to false
	set whole word of find change text options to false
	
	set myTextObjects to {text style range, paragraph, text column, text, story}
	if (count documents) > 0 then
		set mySelection to selection
		
		if (count mySelection) > 0 then
			if class of item 1 of mySelection is in myTextObjects then
				tell document 1
					
					set myText to item 1 of mySelection
					--first repeat loop is to compare paragraphs to see what is equal
					repeat with i from (count paragraphs of myText) to 1 by -1
						
						try
							set ParaA to (paragraph (i - 1)) of myText as string
							set ParaA_trim to (characters 1 thru ((text offset of (tab) in ParaA) - 1) of ParaA) as string --avoid the tab by going -1
							--return ParaA_trim
							set ParaB to (paragraph i of myText) as string
							set ParaB_trim to (characters 1 thru ((text offset of (tab) in ParaB) - 1) of ParaB) as string --again, avoid the tab by going -1
							--compare these strings to see if they are identical
							
							if ParaA_trim = ParaB_trim then
								-- then we have to combine them and take care of the page numbers
								--retrieve the page numbers
								set ParaA_page to (characters ((text offset of (tab) in ParaA) + 1) thru ((count of characters of ParaA) - 1) of ParaA) as string
								set ParaB_page to (characters ((text offset of (tab) in ParaB) + 1) thru ((count of characters of ParaB) - 1) of ParaB) as string
								--return ParaB_page
								(*Next, we need to compare these, and if they are different, combine them in ascending order¬
								and list them with commas after the tab*)
								if ParaA_page = ParaB_page or ParaB_page contains ParaA_page then
									set ParaC_page to ParaB_page
								else
									--here's where we sort then combine the page numbers
									--the index generation already sorts the page numbers in ascending order
									set ParaC_page to ParaA_page & ", " & ParaB_page as string
								end if
								set ParaC to ParaB_trim & (tab) & ParaC_page & return as string
								--now we need to delete one paragraph and change the content of the other one
								
								tell application "Adobe InDesign CS4"
									
									set find what of find text preferences to ParaB
									
									tell parent story of myText
										set {ParaB_textObjchar} to (find text ParaB)'s item 1's {object reference}
										set contents of ParaB_textObjchar to ParaC
										
									end tell
									set find what of find text preferences to ParaA
									tell parent story of myText
										set {ParaA_textObjchar} to (find text ParaA)'s item 1's {object reference}
										set contents of ParaA_textObjchar to ""
										
									end tell
								end tell
							else
								
								
							end if
							
						end try
						
					end repeat
					
				end tell
			else
				display dialog "Text is not selected, or too little text is selected. Please select several paragraphs and try again."
			end if
		else
			display dialog "Nothing is selected. Please select some text and try again."
		end if
	else
		display dialog "No documents are open. Please open a document and try again."
	end if
	
	
	
	
	
end tell



Modifications made on this script do accomplish what I need to do, to some extent.

Here’s the problem: after running this script on a list (like an index) to combine duplicate entries that may have different page numbers, it works fine as desired with a short list (10 to about 300 items). But when it gets larger (like 400+) then funny things happen to the listings that get merged.

Is there some kind of memory leak or penalty that could be mucking up things? How would I solve something like that?

tell application "Adobe InDesign CS4"
	set enable redraw of script preferences to false
	set find text preferences to nothing
	set case sensitive of find change text options to true
	set include footnotes of find change text options to false
	set include hidden layers of find change text options to false
	set include locked layers for find of find change text options to true
	set include locked stories for find of find change text options to true
	set include master pages of find change text options to false
	set whole word of find change text options to false
	
	set myTextObjects to {text style range, paragraph, text column, text, story}
	if (count documents) > 0 then
		set mySelection to selection
		
		if (count mySelection) > 0 then
			if class of item 1 of mySelection is in myTextObjects then
				tell document 1
					
					set myText to item 1 of mySelection
					set placePara to "123456789abcdefghijklmnopqrstuvwxyz" & return as string
					--first repeat loop is to compare paragraphs to see what is equal
					repeat with i from (count paragraphs of myText) to 1 by -1
						--return count paragraphs of myText
						try
							set ParaA to (paragraph (i - 1)) of myText as string
							set ParaA_trim to (characters 1 thru ((text offset of (tab) in ParaA) - 1) of ParaA) as string --avoid the tab by going -1
							--return ParaA_trim
							set ParaB to (paragraph i of myText) as string
							set ParaB_trim to (characters 1 thru ((text offset of (tab) in ParaB) - 1) of ParaB) as string --again, avoid the tab by going -1
							--return ParaB_trim
							
							if ParaB_trim = ParaA_trim then
								--return result
								-- then we have to combine them and take care of the page numbers
								--retrieve the page numbers
								set ParaA_page to (characters ((text offset of (tab) in ParaA) + 1) thru ((count of characters of ParaA) - 1) of ParaA) as string
								set ParaB_page to (characters ((text offset of (tab) in ParaB) + 1) thru ((count of characters of ParaB) - 1) of ParaB) as string
								--return ParaB_page
								
								(*Next, we need to compare these, and if they are different, combine them in ascending order¬
								and list them with commas after the tab*)
								set PageSet to {ParaA_page, ParaB_page}
								--return PageSet
								
								if (item 1 of PageSet) = (item 2 of PageSet) then
									set ParaE_page to (item 1 of PageSet) as string
									
								else if (item 1 of PageSet) ≠ (item 2 of PageSet) then
									set ParaE_page to (item 1 of PageSet) & ", " & (item 2 of PageSet) as string
									
								end if
								--return ParaE_page
								--here's where we sort then combine the page numbers
								--the index generation already sorts the page numbers in ascending order
								--set ParaE_page to ParaA_page & ", " & ParaB_page as string
								set ParaE to ParaB_trim & (tab) & ParaE_page & return as string
								--now we need to replace content of one or two paragraphs and put the combined content of the other one
								--return ParaE
								
								tell application "Adobe InDesign CS4"
									
									set find what of find text preferences to ParaB
									
									tell parent story of myText
										set {ParaB_textObjchar} to (find text ParaB)'s item 1's {object reference}
										set contents of ParaB_textObjchar to placePara
										
									end tell
									set find what of find text preferences to ParaA
									tell parent story of myText
										set {ParaA_textObjchar} to (find text ParaA)'s item 1's {object reference}
										set contents of ParaA_textObjchar to ParaE
									end tell
									
									
									
								end tell
								
								
								
							else
								
								
							end if
						end try
						
					end repeat
					
					repeat with i from (count paragraphs of myText) to 1 by -1
						--return count of paragraphs of myText
						
						
						--set placePara to "123456789abcdefghijklmnopqrstuvwxyz" & return as string
						set ParaD to (paragraph i of myText) as string
						set nullPara to "" as string
						--return nullPara
						tell application "Adobe InDesign CS4"
							set find what of find text preferences to placePara
							if ParaD = placePara then
								tell document 1
									tell parent story of myText
										
										set {ParaD_textObjchar} to (find text ParaD)'s item 1's {object reference}
										set contents of ParaD_textObjchar to nullPara
									end tell
								end tell
							end if
						end tell
						
					end repeat
					
				end tell
			else
				display dialog "Text is not selected, or too little text is selected. Please select several paragraphs and try again."
			end if
		else
			display dialog "Nothing is selected. Please select some text and try again."
		end if
	else
		display dialog "No documents are open. Please open a document and try again."
	end if
	
	
	
	
	
end tell



Thanks for any help!