Can this be sped up?

Hello all,

I’m fairly new to scripting, although I’ve written a few simple and useful ones.

This one is for my wife who got an index from the author in .rtf format for a book she’s laying out in inDesign. Upon importing the text, all the italics are lost, and there are 71K characters.

Word’s dictionary seems waaay to complex, and I learned that the italics characters are actually the italic font, so it’s strange that the italics are lost. So I figured I could just change the italic font to a very different font, which could then be easily replaced in ID once imported.

tell application "TextEdit"
	set myText to text of document 1
	set characterCount to count characters of myText
	tell document 1
		repeat with i from 1 to characterCount
			if font of character i is "GaramondPremrPro-It" then
				set font of character i to "EurostileRegular"
			end if
		end repeat
	end tell
end tell

It’s working, but creepingly slow at about 5 sec per character which will total about 100 hours. I’ve got the same script running on her machine, set to start from the end and go forward, but, still, 50 hours is too much.

Could I get some suggestions on how to speed this up?

Thanks in advance.

Model: PowerMac G5 2.0 GHz Dual
Browser: Safari 523.10.6
Operating System: Mac OS X (10.5)

Please forgive me as I am new to AppleScript, but not new to programing.

Since the file is relatively small, could you build a list (via the LIST class) then search the list instead of the text file. Searching a list is much faster than a text file.

Also, why not use the WORD class in TextEdit instead of each character?

Save the result in a new list… then push it to a new text file. newTextFilename.txt.

Model: iMac 24" 3Gigs Ram
AppleScript: 2.0
Browser: Safari 523.10.6
Operating System: Mac OS X (10.5)

Yup… you cannot use the text list feature as the Rich Text is stripped from the list in the array. So, any bold/italic etc gets lost as does the font name.

But I went with the WORD class and that seemed to work pretty well…

I created a 708K 15K worded junk file to test it out with. The process is still slow, but the replacement is by word length, not each character. So the net is faster.

tell application “TextEdit”
set myText to text of document 1
set characterCount to count word of myText
tell document 1
log characterCount keeping track of my numbers
repeat with i from 1 to characterCount
if font of word i is “Helvetica” then
set font of word i to “Gulim”

		end if
		--if i > 4 then return  just a quick way to, uh, quit
		--log i  keeping track of my numbers
	end repeat
end tell

end tell

Hi,

Word’s dictionary is hugh but also quite effective :wink:


tell application "Microsoft Word"
	set myFind to find object of text object of active document
	clear formatting myFind
	set name of font object of myFind to "GaramondPremrPro-It"
	set content of myFind to ""
	clear formatting replacement of myFind
	set name of font object of replacement of myFind to "EurostileRegular"
	set content of replacement of myFind to ""
	execute find myFind replace replace all
end tell

I imagine Stefan’s solution is what’s needed here. Just to note that in TextEdit, the relevant text element would be the ‘attribute run’.

tell application "TextEdit"
	set font of attribute runs of text of document 1 whose font is "GaramondPremrPro-It" to "EurostileRegular"
end tell

This could still take a while, though, in a long document.