problem w/script in Tex-Edit Plus

I’ve been working on a script that uses Text-Edit Plus. I’ve got some plain text files of some old books. They indicate originally italicized text by enclosing it within a pair of underscores like this. I want my script to find those underscore enclosed strings and italicize them.

I’m going thru the document paragraph by paragraph, finding any underscore enclosed strings and I’m putting them in a list. Then I use Text-Edit Plus’ “replace” command to find each string in my list, replace it in the current paragraph and set its style to italic. This works on most of the document I’ve been using to while writing the script, but it runs into some paragraphs and throws a -1700 error, “Can’t make some data into the expected type”. This is the portion of the script that fails:

if italStrings ≠ {} then --compare our italic strings to the text of the paragraph and set their style to italic if we get a match
				set g to 1
				repeat until g > (count of italStrings)
					try
						replace paragraph currentParagraph of document 1 looking for item g of italStrings ¬
							replacing with item g of italStrings ¬
							looking for styles {style:plain} replacing with styles {style:italic}
					on error errTxt number errnum
						display dialog errTxt & return & errnum
					end try
					
					--set myFlag to result
					set g to g + 1
				end repeat
			end if

I’ve looked at this for three or four days now, and can’t find anything “special” about the paragraphs that this fails on versus the paragraphs that work fine.

I’d appreciate any ideas anyone may have. I’d be happy to post the entire script and a sample of the document that the script fails on.

Hi,

I guess the problem could be the kind of iterating the list elements.
AppleScript provides great methods for repeat loops, so it’s not necessary to create own
index variables and increase them.
The first line (≠{}) isn’t necessary either, if the list is empty, the repeat loop will be skipped.


repeat with  g in  italStrings
	try
		replace paragraph currentParagraph of document 1 looking for contents of g ¬
			replacing with contents of g ¬
			looking for styles {style:plain} replacing with styles {style:italic}
	on error errTxt number errnum
		display dialog errTxt & return & errnum
	end try
end repeat

Thanks for the idea Stefan. Definitely less verbose than my version, but that doesn’t solve the problem. It still throws the -1700 error. :frowning:

sorry, I don’t use TextEdit-Plus, therefore I can’t prove it