InDesign get the associated XML element of selected text

I am trying to obtain the name of the markup tag (inline text tag) of a selected string of text. I am not sure why this is not yielding the result I am after. Can anybody help me out? I think I am relatively close?

tell application "Adobe InDesign CS4"
	activate
	set theDoc to document 1
	tell theDoc
		set mySel to selection
		set selRef to object reference of selection
		if (count mySel) is equal to 0 then error number -128
		
		set selText to object reference of (text from character 1 to character -1) of selection
		
		set myElement to associated XML element of selText
		set myXMLName to name of markup tag of myElement
		display dialog myXMLName as string
	end tell
end tell

Hi, Jeff. Associated XML element should be plural. Additionally, you’ve created a list in the object reference variablization of selText. Were you not to use an object reference, variabilization would implicitly dereference that text, which is equally unworkable. I (generally) avoid using variables to define things that are only used once. Your code will work if you eliminate the superfluous variables in it, which is all of them. :wink:

Hi Marc, I wanted to thank you for replying. I do understand what you mean. However I did eventually figure out a work around to derive at exactly what I was after. I will post what I have this weekend. I would do that now but my brain is not working since I was at a business meeting for all day. I wish I could script more. Although my name might be over this forum quite a bit. the time I get to work in AS is extremely limited and it sucks because it seems to be a lesson in relearning when I have to jump back into it.

This is a more detailed explanation of my issue and my method to resolve it.

Our operators occasionally select a string of text and apply a markup tag to that text. That is okay.

But it is not uncommon for the operator to later down the road select some of that same text or additional/more text with that string somewhere included within it and then apply a new markup tag. In our workflow this would create issues. So I was looking for a way to determine if the selected text had any inline markup tag associated to it prior to them applying the new markup tag.

The only way I found to handle this was to capture the selected text and then cycle through all of the associated markup tags within the parent story. If a markup tag contains any of that same text string then the script would automatically remove the existing markup tag before applying the new markup tag.

This snippet appears to do the trick well. But ideally I don’t even care about the text contents. All I really care about is knowing if any markup tag is associated with the selected text and simply untag it. But the only way I found to achieve my goal was to see if the selected text somewhere exists within the existing markup tag associated with the string that is currently selected by the operator.


tell application "Adobe InDesign CS4"
	activate
	tell document 1
		set selText to object reference of (text from character 1 to character -1) of selection as text
		set parentStoryID to parent story of selection
		set theXML to associated XML element of parentStoryID
		set myElements to name of markup tag of XML elements of theXML as list
		set theElements to every XML element of theXML
		repeat with x in theElements
			set theTagName2 to the name of markup tag of x
			set ChildElement to object reference of x
			set ChildElementMarkupTagContents to contents of contents of ChildElement as text
			if selText contains ChildElementMarkupTagContents then
				untag x
			end if
			if ChildElementMarkupTagContents contains selText then
				untag x
			end if
		end repeat
	end tell
end tell