A Simple TextEdit script needs help

I have a need to replace the formatting of some phone #s in a list of more than 30,000 entries.
I have the file in TextEdit, but I can’t get the last and most important event to work. What is the correct syntax for this?


tell application "TextEdit"
	
	set paraNumb to count of paragraphs of document 1
	repeat with i from 1 to paraNumb
		set thisPara to paragraph i of document 1
		if thisPara contains ")" then
			set offsetOne to (text offset of ")" in thisPara)
			set theParens to "(" as string
			set firstHalf to (characters 1 thru (offsetOne - 4) of thisPara) as string
			set lastText to (characters (offsetOne - 3) thru (count of characters of thisPara) of thisPara) as string
			set newText to firstHalf & theParens & lastText as string
			set text of paragraph i of document 1 to newText
	
			
		end if
		
		
	end repeat
end tell

I get an error message every time:

I am sure there’s a simple answer, but no examples of this basic (or is it?) command can I find.

Thanks for help!

I would be more inclined to read the whole document into a variable in my script and construct a new one line by line. That way, you do the whole thing in the script without hopping back and forth.

Try replacing set text of paragraph i of document 1 to newText
with set paragraph i of document 1 to newText

Thanks patron22,

That worked fine.

Thanks!

Adam,
What steps do I need to add and adapt for bringing in the whole file into a variable? I would only be guessing at this point.

Take care,