Quark to Indesign - hidden characters

Hello everyone,

I ran into a small problem at work with our switchover from QuarkXPress to Adobe Indesign. Some of our Quark files (including some that are supplied to us as Quark documents) have hidden characters in them and those characters don’t make the transition properly into InDesign. For instance, the text might read: “Juno (2007) Story of a …” in QuarkXPress, but: “Juno (2007) H^Ellen Page H\Michael Cera.H^ Story of a …” when the Quark document is brought into InDesign through our plugin.

I’ve come up with something short to get rid of anything that is supposed to be “hidden” (that’s what the H^ tags are supposed to indicate):

set endflag to 0
set deleteflag to 0
set curritem to 1
set charcount to 0
repeat until endflag is 1
	tell application "Adobe InDesign CS3"
		try
			tell selection
				-- grab current word from InDesign
				set currword to word curritem
			end tell
		on error
			-- If this errors out then we must be at the end of the text
			set endflag to 1
		end try
		
		--check that the word is longer than 3 characters to avoid an error
		if (count of characters in currword) is greater than 3 then
			if text 1 thru 3 of currword is "H\\^" then
				--flag the next words for deletion until another hidden flag is found
				set deleteflag to 1
			end if
			if text ((count of characters in currword) - 2) thru (count of characters in currword) of currword is "H\\^" then
				--we've found a hidden flag so delete this word but turn off hidden flag afterwards
				set deleteflag to 2
			end if
		end if
		if deleteflag is greater than 0 then
			--we need to delete this word AND the space after it
			tell selection
				delete word (curritem as number)
				delete character (charcount + 1)
			end tell
		end if
		if deleteflag is 0 then
			-- tally up the current position to delete the space after the word
			set charcount to (count of characters in currword) + charcount + 1
			set curritem to 1 + curritem
		end if
		if deleteflag is 2 then
			-- we've reached the end of some hidden text so reset the flag
			set deleteflag to 0
		end if
	end tell
end repeat

It seems to work fine, and isn’t slow, and it’s sorta neat to see it work in InDesign.

I had thought about passing everything into one string and using Applescript’s text item delimiters to neatly carve out the hidden text, but the problem is that I don’t want to lose the character formatting in InDesign, since (among other things) the film titles are in bold, the description is one font size smaller, and so forth.

Is there an easier way to do this?

And sorry if my code is a bit messy, I’m not a professional, this is just stuff left over from highschool and year 1 of university.