InDesign CS4 markup tag issue

I am not sure why this code used to work in CS2 and not in CS4.
I used to be able to tag selected text with a markup tag using the below script. It was simple and worked, however with CS4 I receive the error. “Adobe InDesign CS4 got an error: Cannot change the element to be associated with this object. Try creating a new element.”
Can anybody PLEASE help me out?

tell application "Adobe InDesign CS4"
	activate
	tell document 1
		tell XML element 1
			set t to make XML element with properties {markup tag:"expirationdate"}
		end tell
		tell selection to markup using t
	end tell
end tell

Hi Jeffkr

Seems to be working okay on my CS4

Hi Pidge,
I finally figured this out. Well. I should be honest, I did receive some help from some folks on Adobe Forums. However, they love javascript over there :frowning: But the good news is, this was translatable into AppleScript.

Below is the working script. This will apply a markup tag to selected/highlighted text in InDesign CS4. The name of the markup tag can be assigned to whatever one wishes. In my script I am using the word “expirationdate”.

tell application "Adobe InDesign CS4"
	tell document 1
		set selText to object reference of (text from character 1 to character -1) of selection
		set parentStoryID to parent story of selection
		tell XML element 1
			set theStoryElement to make XML element with properties {markup tag:"Story"}
			tell theStoryElement
				set myXMLElement to make XML element with properties {markup tag:"expirationdate", XML content:selText}
				tell parentStoryID to markup using theStoryElement
			end tell
		end tell
	end tell
end tell

Hi, Jeff.

Does this code produce valid markup? On the initial run, it appears that you end up with two disassociated structure elements, one for the selection and another for the frame. Each subsequent run then produces a separate story element for each selection, even though it may reside in the same story. I’m not certain, but shouldn’t all tagged text within a single text flow live inside one story element?

Hi Marc,
Interesting? when I use this code multiple times on various selected characters within the same text frame, I am receiving multiple “expirationdate” xml tagged text instances all within one parent Story element.

My structure shows:

Root
Story
expirationdate “selected text 1”
expirationdate “selected text 2”

I tested further, and, while the code gets executed in all versions, its resulting markup is malformed in CS1”my home testing environment”and CS2; there must have been fundamental changes to later versions that prevent this from happening.