Preventing a memory error

Hello. I am trying to get the following script to work as a classic script with Quark 4.11. What I would like it to do is loop through a list of style names and then go through another loop that reapplies that style sheet within the document.

It almost works. On larger documents, it fails with a memory error and I was hoping someone could clarify what I could do to keep the memory error from occurring. I believe that the variable “theGraphs” gets overloaded with references to certain styles that appear many times and then the script fails. I have gotten the script to work with 2 or 3 styles on 200 page documents, but I would like it to work with about 8-10 styles on any document. Any ideas are greatly appreciated…and yes, it is a cruel hack of an older Sal Soghoian script…apologies to Sal.


tell application "QuarkXPress™ 4.11"
	activate
	set theGraphs to {}
	set megaList to {"style_name1","style_name2","style_name3","style_name4"}
	try
		repeat with x from 1 to count of megaList
			tell document 1
				set styleToSearchFor to item x of megaList as text
				set theGraphs to (the object reference of ¬
					every paragraph of story 1 of current box whose ¬
					name of style sheet is styleToSearchFor)
				
				if the class of theGraphs is not list then
					copy (coerce theGraphs to list) to theGraphs
				end if
				
				repeat with i from (number of theGraphs) to 1 by -1
					set style sheet of paragraph 1 of (item i of theGraphs) to null
					set style sheet of paragraph 1 of (item i of theGraphs) to styleToSearchFor
				end repeat
				
			end tell
			beep 2
		end repeat
	on error
		beep 4
	end try
end tell

Hmmm… I think in older OSs there was a size-limit for lists (about 4000 items) and, as you say, memory errors… I’m not scripting Quark anymore, so I can’t text, but usually you may need loop through all paragraphs (not very quick, but reliable). Eg (pseudo-code):

tell app foo
   tell document 1
      repeat with i from 1 to 99999999
         try
            set theParagraph to paragraph i
            --> do whatever
         on error --> mo more paragraphs
            exit repeat
         end try
      end repeat
   end tell
end tell

Hi jj,

I have taken some of your hints here before. Thanks for the reply.

Can I just ask…can I redo the script as OSX and avoid the memory errors or would I need Quark 5 or 6 for that. I am not sure I understand where the cutoff is with the memory errors–Finder 9, or Quark 4.11.

Thanks

I think the guilty is the app-which-runs-the-script (if an applet, the applet itself; if running from OSA Menu, OSA Menu; if from Script Editor, the Script Editor). But the final guilty is the AppleScript extension.
I don’t know, though, if you could write a OSX-native application and target a classic app… Anyway, for very long listings of paragraphs, it could make Quark crash or take a very long-long time… I don’t know :rolleyes: Just re-compile, save as OSX-native from OSX’s Script Editor and check against a dangerous Quark doc.