I have a structured Indesign document. I select a text frame with text, in which some symbols are marked by an XML tag 'XXX'. I want to mark all the rest text in this story with a tag 'YYY'.
I've tried this code:
var mySelection = app.selection[0]; // Get the selected frame
var tagToApply = "YYY"; // Tag to apply
var tagToSkip = "XXX"; // Tag to skip
var myTextFrame = mySelection;
var myTexts = myTextFrame.texts;
var myStory = myTextFrame.parentStory;
// Function to check if the text is not marked with tag to skip
function isUntagged(text) {
return text.associatedXMLElements.name !== tagToSkip;
}
// Check all the text in the selected frame
for (var i = 0; i < myTexts.length; i++) {
var thisText = myTexts[i];
if (isUntagged(thisText)) {
// Mark the found untagged text with the tag to apply
var myXMLElement = app.activeDocument.xmlElements.item(0).xmlElements.add(tagToApply, thisText);
}
}
The problem is that all the story text is marked with 'YYY' tag, including the fragments tagged as 'XXX'.
Probably you want something like this: