InDesign: Deleting Image From a Rectangle Deletes the Box's XML Tag???

So I wrote a fairly complex script to process some complicated files, but now one of the simplest pieces of the whole integrated script is failing, because when the script deletes a graphic from a rectangle, the XML tag for that box gets deleted too!

When you select the graphic inside the rectangle in the GUI and hit delete, it deletes the graphic and retains the XML tag. When AppleScript deletes the graphic, it strips off the XML tag too.

I can see that both the graphic object and the container for that graphic object both show an “associated XML element” that is the same. I am assuming this is why when the graphic is deleted that the associated XML tag gets deleted from the frame too, but why doesn’t it delete the XML tag when you do it through the GUI?

I’ve been trying different angles to make this work all afternoon and nothing helps. Has anyone else noticed this behavior? It’s maddening. How hard can it be to delete a graphic from an XML tagged box and leave the XML tag intact? Apparently, the answer is “very”!

Does anyone have a good process for deleting images from frames in a way that doesn’t delete the box’s XML tag along with it?

I guess there is not.

You have to delete the current tag and make a new one.
Something like this:

-- assuming the main XML element of the document is named "Root"
-- the tag "Image" is already present in the document and applied to the current image
-- the selection is the container of the image

tell application id "InDn"
	set tagName to "Image"
	
	set theFrame to item 1 of selection
	set oldTag to associated XML element of theFrame
	if oldTag ≠ nothing then untag oldTag
	delete graphic 1 of theFrame
	(place "some image file.ext" as «class furl» on theFrame)
	make XML element at XML element "Root" of active document with properties {markup tag:tagName, XML content:theFrame}
end tell


I had considered that possibility yesterday and I fiddled a bit with trying to make that work (though I was afraid it would retag the boxes out of order in the structure panel).

I was still hoping that maybe there was some other way to address the rectangle objects in a way that didn’t delete the XML tag from the rectangle along with the graphic.

I ended up using a little bit different approach than you show here, as the XML tag that I want to use already exists and already has a place in the structure panel that I wanted to retain. I’m afraid that deleting the tag and recreating it would knock things out of order and then I would probably have to resort the tags, and considering that we’re using this on dozens and dozens of different files with different XML tags, there would be no simple solution to that other than reading in the hierarchy and rearranging them.

It turns out that just deleting the graphic out and reassociating the tag to the rectangle leaves the structure intact!

Here’s the short snippet of code that I used for the retagging:

repeat with rr from 1 to count of graphicRectangles
set retagTag to associated XML element of item rr of graphicRectangles
delete graphics of item rr of graphicRectangles
markup item rr of graphicRectangles using retagTag
end repeat

Thanks for the reinforcement of my idea from yesterday. Looking at the problem with fresh eyes today allowed me to put the pieces together correctly and make it work. Yay!

Good job!