Problem with scripting Quark to print PostScript files

This script takes every open quark document and prints postscript files of those documents to my desktop. It repeats, saving and closing documents before moving on to the next open document. It works fine, except on the next to last document open! It always skips over the next to last document, simply closing it without generating a postscript file. Does anyone have any ideas about what is causing this? Any input would be greatly appreciated, as this is extremely frustating. I have posted my script below.

tell application "QuarkXPress"
	activate
	repeat
		try
			if (count documents) is not equal to 0 then
				set theName to name of front document
				set thePSfile to ((path to desktop) & theName & ".ps") as string
				print document 1 PostScript file thePSfile without print dialog
				save document 1
				close document 1
				tell front document
					activate
				end tell
			else if (count documents) = 0 then
				exit repeat
			end if
		end try
	end repeat
end tell

Model: Power Mac G5
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Hi jon

Got some funny reactions from quark on this one, i think the trouble your gonna have with this is the size of the documents.
you may get a few connection invalid errors, i think its just quark trying to play catch up with the script.
this works for me but only tested on quark docs with a little amount of text in them you may want to up your delay.

tell application "QuarkXPress"
	set t to count documents
	repeat with i from 1 to t
		tell document 1
			set theName to name --of document i
			set thePSfile to ((path to desktop) & theName & ".ps") as text
			print PostScript file thePSfile
		end tell
		delay 1
		close document 1 saving no
		activate application "QuarkXPress"
	end repeat
end tell

Have a play around with this see where it gets you.
As for your question i’m not sure why it’s leaving your last document probably due to the way you had your repeat loop set up i’d guess!!