Applescript problem with large text flows

I have an odd issue occurring with QuarkXPress 6.5 and one of my AppleScripts.

My script scrubs through a large amount of text looking for bold and italic text. It then places tags around the bold and italic. Standard html tags. There is some standard text which I don’t want to have tags on, so I then have the script remove the tags around this specific text with a find and replace subroutine.

The problem is that when the number of characters in the story is beyond about 32000+, the script goes haywire and begins tagging the wrong text and deleting the wrong text elsewhere.

I’ve tested this with 10.4.2 thru 10.4.4. Same results.

is this a known bug? Any solutions/workarounds?

I don’t think the 32K limit applies any more to AppleScript, so it might be quark giving you grief. Are you reading the file into AppleScript as a variable, or operating on it in Quark?

I’m operating on it in Quark itself. It is definitely Quark causing this issue.

I’m importing text in from Word. Before applying any styles in Quark (which wipes out the bold and italic), the script tags the bold and italic words.

This is the guts of the script. On a smaller document, it works great. On a document with 32000+ characters it tags the wrong text and deletes the wrong characters. i think the problem is with the replaceText subroutine. The offsets show up as negatives when the document has a large text flow. But the negatives don’t count from the end correctly.

Any help would be greatly appreciated!


tell application "QuarkXPress"
				activate
				tell front document
					set tool mode to contents mode
					
					tell story 1

                                    -- BOLD TAGS
						try
							copy "<B>" to before (text style ranges whose style contains bold)
							copy "</b>" to after (text style ranges whose style contains bold)
						end try

                                          set theUINStory to 1

						set theText to "</b><B>"
						set theReplaceText to ""
						my replaceText(theText, theReplaceText, theUINStory)
						
						set theText to "<B>Title</b>"
						set theReplaceText to "Title"
						my replaceText(theText, theReplaceText, theUINStory)
						
															             repeat with theNum from 1 to 9
							
							set theText to "<B>" & (theNum as string) & "</b>"
							set theReplaceText to (theNum as string)
							my replaceText(theText, theReplaceText, theUINStory)
							
							set theText to ("<B>" & (theNum as string) & "</b>" & tab)
							set theReplaceText to ((theNum as string) & tab)
							my replaceText(theText, theReplaceText, theUINStory)
							
							set theText to ("<B>" & (theNum as string) & tab & "</b>")
							set theReplaceText to ((theNum as string) & tab)
							my replaceText(theText, theReplaceText, theUINStory)
						end repeat
					end tell
				end tell
			end tell
			

on replaceText(theText, theReplaceText, theUINStory)
	tell application "QuarkXPress"
		tell front document
			
			tell story theUINStory
				set h to 0
				
				try
					set h to count of (every text where it is theText)
				end try
				
				if h is greater than 0 then
					repeat with k from h to 1 by -1
						set theOffset to (offset of text k where it is theText)
						delete (characters (theOffset + 1) thru (theOffset + (count of (characters of theText))))
						make new text at before character (theOffset + 1) with properties {contents:theReplaceText}
					end repeat
				end if
			end tell
			
		end tell
	end tell
end replaceText


Moving to the OS X forum.

Jacques: That did the trick! Thanks!