Why this huge delay with a long TextEdit file?

Hello folks,

I’m working with a TextEdit file containing 57,000 paragraphs. TextEdit itself can find and replace more than 3000 lines in under two seconds. But when I use a simple Applescript, there is a delay of about 40 seconds to change one paragraph. Can someone explain why this is happening and how to make it go fast?

Here’s the script edited down to its simplest form:

tell application "TextEdit"
	set paragraph 5 of text of document 1 to "Some new text." & return
end tell

When I run it, The document quickly comes to the front, then I get the spinning beach ball of death for about 40 seconds before it changes the paragraph.

icta :frowning:

Model: iMac 3.06 GHz Intel Core 2 Duo
AppleScript: 2.0.1
Browser: Safari 531.21.10
Operating System: Mac OS X (10.6)

My guess is that the document has so many paragraphs that it is taking so long to get the data.

Perhaps consider copying the lot, then editing the string in AS???

TextEdit really isn’t scriptable except in a generic sense. Is your document in RTF or RTFD? If it’s plain text, then read it into your script, do what you want and write it back. If it’s formatted, however, then AppleScript’s probably not the way to go – it can’t really handle Rich Text properly.

Thanks Dylan, Richard and Adam,

I’m using a combination of your suggestions. I tried getting all the text and processing with AS but it is way too big a bite – still results in long delays. So I am bringing in 18 paragraphs at a time (one record), making my changes and then after processing 5 records (90 paragraphs) dumping the corrected records to a new text edit file. It’s not fast (about 5 records per second) but SO much better than one paragraph every 40 seconds.

It’s going to take awhile. I’ll just do something else while I wait.

Thanks again,
icta :slight_smile:

If the document is a text file, don’t call TextEdit to do the trick.

set theFullPathname to "the:complete:path:to:the:file"
set enListe to paragraphs of (read file theFullPathname)
--apply the required changes to enListe

set item 5 of enListe to "That's all folks !"

set eof of file theFullPathname to 0
write my recolle(enListe, return) to file theFullPathname


on recolle(l, d)
	local t
	set AppleScript's text item delimiters to d
	set t to l as text
	set AppleScript's text item delimiters to ""
	return t
end recolle


Yvan KOENIG (VALLAURIS, France) samedi 3 avril 2010 16:54:31


For other readers, it reads in the file as a LIST of paragraphs, and AS can really fly through a list!
It took about 4 seconds to read in a file with 57,474 paragraphs. I’ll have more to say when I have more time to work on it.

ifca :smiley: