Finding Subscripts in Indesign CS2

I am writing a script that will convert Indesign CS2 files to HTML. We need something a little cleaner than the HTML export from Indesign and one that also writes a lot of proprietary CSS and dynamic code. Anyhoo…

All is going well. I can get the selected articles, (I should note down the road this will merge into an XCode App but I like to start in AS to work things out and then move on to building an application) find the various bold, italics and stuff. Two problems I am running into - subscripts - my code converts them from 2 (pretend that’s a subscript) to 2. Unfortunately it also tags any other number with that code.

The other problem is the content that is read out of Indesign comes with escapes around the quotes - like this:

"no man was allowed to enter the Garden save those whom he intended to be his Ashishin."

I have a search and replace subroutine (which I got off this site but can’t remember who wrote or I would properly credit it). Anyway, I can’t seem to figure out how to escape the escape to replace it…i tried something like this:

set thisStoryContents to my searchAndReplace(“\"”, “”", thisStoryContents)

didn’t get a compile error but it didn’t seem to work either.

Here’s the full code I am using. Any help and/or suggestions would be appreciated:


tell application "Adobe InDesign CS2"
	tell front document
		-->get the text
		set parentStory to parent story of selection
		set parentStoryID to id of parentStory
		set thisStoryContents to contents of story id parentStoryID
		
		-->search for ranges
		try
			set boldRange to (object reference of every text style range whose font style contains "Bold") of story id parentStoryID
			-->rewrite with html code
			repeat with thisBoldRange in boldRange
				set findThis to contents of properties of thisBoldRange
				set replaceWith to "<b>" & findThis & "</b>"
				set thisStoryContents to my searchAndReplace(findThis, replaceWith, thisStoryContents)
			end repeat
		on error
			display dialog "No bold range"
		end try
		
		try
			set italicRange to (object reference of every text style range whose font style contains "Italic") of story id parentStoryID
			repeat with thisItalicRange in italicRange
				set findThis to contents of properties of thisItalicRange
				set replaceWith to "<i>" & findThis & "</i>"
				set thisStoryContents to my searchAndReplace(findThis, replaceWith, thisStoryContents)
			end repeat
		on error
			display dialog "No italic range"
		end try
		
		try
			set supRange to (object reference of every text style range whose position contains superscript) of story id parentStoryID
			repeat with thisSupRange in supRange
				set findThis to contents of properties of thisSupRange
				set replaceWith to "<sup>" & findThis & "</sup>"
				set thisStoryContents to my searchAndReplace(findThis, replaceWith, thisStoryContents)
			end repeat
		on error
			display dialog "No superscripts"
		end try
		
		try
			set subRange to (object reference of every text style range whose position contains subscript) of story id parentStoryID
			repeat with thisSubRange in subRange
				set findThis to contents of properties of thisSubRange
				set replaceWith to "<sub>" & findThis & "</sub>"
				set thisStoryContents to my searchAndReplace(findThis, replaceWith, thisStoryContents)
			end repeat
		end try
		
		set thisStoryContents to my searchAndReplace(ASCII character 38, "&", thisStoryContents)
		set thisStoryContents to my searchAndReplace("Ô", """, thisStoryContents)
		set thisStoryContents to my searchAndReplace(ASCII character 213, ASCII character 39, thisStoryContents)
		set thisStoryContents to my searchAndReplace(ASCII character 210, ASCII character 34, thisStoryContents)
		set thisStoryContents to my searchAndReplace(ASCII character 211, ASCII character 34, thisStoryContents)
		set thisStoryContents to my searchAndReplace(ASCII character 208, "–", thisStoryContents)
		set thisStoryContents to my searchAndReplace(ASCII character 209, "—", thisStoryContents)
		--set thisStoryContents to my searchAndReplace(ASCII character 8226, "•", thisStoryContents)
		set thisStoryContents to my searchAndReplace("'", """, thisStoryContents)
		set thisStoryContents to my searchAndReplace("â„¢", "™", thisStoryContents)
		set thisStoryContents to my searchAndReplace("®", "®", thisStoryContents)
		set thisStoryContents to my searchAndReplace("©", "©", thisStoryContents)
		set thisStoryContents to my searchAndReplace("¢", "•", thisStoryContents)
		set thisStoryContents to my searchAndReplace(ASCII character 13, "<p>", thisStoryContents)
		set thisStoryContents to my searchAndReplace("<b></b>", "", thisStoryContents)
		set thisStoryContents to my searchAndReplace("<p></b>", "</b><p>", thisStoryContents)*)
		
	end tell
end tell

on searchAndReplace(findThis, replaceWith, inString)
	set AppleScript's text item delimiters to (findThis as string)
	set theFilePathItems to every text item of (inString as string)
	set AppleScript's text item delimiters to (replaceWith as string)
	set theNewFolderPath to theFilePathItems as string
	return theNewFolderPath
end searchAndReplace

Doh, I figured it out. As usual it was programmer error. It’s how I am replacing the range, I’m not actually grabbing the range, just the contents of it and then using a universal search and replace subroutine. So if the range for a subscript was:

text from character 3630 to character 3630 of story id 169 of document “indesigntest.indd” of application “Adobe InDesign CS2”

I need to just change that character, what I was doing was then getting that character and then changing ALL of the same characters…like I said, doh!