Quark 3 text find&rplace s l o w...

I put together a couple of scripts I found here and elsewhere to make a script that searches every text box of the open xpress document for various stings of text and replaces them with a different string. The script loads the list of changes from a text file. On a G5 iMac using Quark 3.32r5 in classic, the script below works, but runs incredibly slow. I had to put in a dialog showing each change as it processes, the slowness is not due to this dialog. While the “giving up after 1” adds the 1 second per change, it at least lets you know the script is working. Out list of changes is around 150, so the script takes a few minutes to march through the file. Any suggestions on if there is a better way to optimize this script would be appreciated.

set ListFile to (path to home folder) as string
set ListFile to ListFile & “Library:Preferences:changeslist.txt”

tell application “Finder”
set checkForList to (file ListFile) exists
end tell
– above checks to see if there is a file of changes in~/Library/Preferences/changeslist.txt
– changeslist.txt should contain a set of equal sign delimtes changes onec change per line
–Bilbo=Frodo
–Merry=Pippin
–etc

if checkForList is true then
try
set LoadChangesCmd to “cat ~/Library/Preferences/changeslist.txt”
set Changes to (do shell script LoadChangesCmd)
set ChangesList to paragraphs of Changes
set listcount to number of items in ChangesList
repeat with i from 1 to number of items in ChangesList
set ThisChange to item i of ChangesList
set {od, AppleScript’s text item delimiters} to {AppleScript’s text item delimiters, “=”}
set ThisChange to (text items of item i of ChangesList)
set AppleScript’s text item delimiters to od
set oldWord to item 1 of ThisChange
set newWord to item 2 of ThisChange
ChangeInQuark(oldWord, newWord, i, listcount)
end repeat
on error errmsg number errNum
display dialog errmsg & return & errNum
end try
else
display alert “There is no list of changes.” message “Place an equal sign delimited list in ~/Library/Preferences/changeslist.txt” as warning
end if

activate me
display dialog “All text has been replaced.” buttons {“Thanks”} default button 1

on ChangeInQuark(old_word, new_word, j, total)

set search_strings to {(old_word)}
set replace_strings to {(new_word)}
tell application "QuarkXPressâ„¢ 3.32r5"
	activate
	display dialog ((j as string) & " of " & total as string) & return & old_word & " -> " & new_word buttons {"OK"} giving up after 1
	try
		
		set pageList to {}
		set textboxList to {}
		tell document 1
			if pageList = {} then
				set pageList to index of every page
				if class of pageList ≠ list then set pageList to {pageList}
			end if
			set pageCount to count of pageList
			repeat with p from 1 to pageCount
				tell page (item p of pageList)
					if textboxList = {} then
						set textboxList to index of every text box
						if class of textboxList ≠ list then set textboxList to {textboxList}
					end if
					set textboxCount to count of textboxList
					repeat with t from 1 to textboxCount
						tell story 1 of text box (item t of textboxList)
							set myselection to (a reference to text of selection)
							repeat with i from 1 to (count of search_strings)
								try
									set mysearch to (a reference to (text of myselection whose contents of it = (old_word)))
									set contents of mysearch to (new_word)
								end try
							end repeat
						end tell
					end repeat
				end tell
			end repeat
		end tell
	on error errmsg number errNum
		display dialog errmsg & return & errNum
	end try
end tell

end ChangeInQuark

You might try addressing the stories rather than text boxes.

tell application "QuarkXPressâ„¢ 4.11"
	activate
	tell document 1
		set TheStories to count of every story
		repeat with i from 1 to TheStories
			set mysearch to (a reference to (text of story i whose contents of it = "YYYY"))
			set contents of mysearch to "XXXX"
		end repeat
	end tell
end tell

If I recall this will also search text overflow text. This should cut down significantly on your apple event calls to quark and speed up the script’s execution.

Thanks for the suggestion. I gave it a try, but (there is always a but) for some reason, it throws an error when the replacement text is in this pattern:

aaaa bbbbb ccccc=ccccc

here is an entry from the Event History
set every text of story 1 of document 1 whose contents = “New York Jets” to “Jets”
display dialog “QuarkXPressâ„¢ 3.32r5 got an error: Can’t set every text of story 1 of document 1 whose contents = "New York Jets" to "Jets".
-10006”
{button returned:“OK”}

What is strange is that even though I get the error, it still appears to replace the text. I could just ignore error -10006, but I would rather know what is the cause.

I’m not sure, I don’t have Quark 3 to test it on. It seems to work fine in Quark 4.11 in Classic. It might be an overflow error? Just a guess, but there may have been a change between Quark 3 ond 4’s scripting implamentation that is causing you an error in 3 when it runs fine in 4. Sorry I couldnt be of more help.

Out of interest, did it seam to speed things up at all?

Tough to say. I need to tweak it a bit further, and try to figure out why the error.