我有一份结构化的 Indesign 文档。我选择一个带有文本的文本框架,其中一些符号由 XML 标签“XXX”标记。我想用标签“YYY”标记这个故事中的所有其余文本。
我已经尝试过这段代码:
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);
}
}
问题在于所有故事文本都标有“YYY”标签,包括标记为“XXX”的片段。
可能你想要这样的东西:
var frame = app.selection[0];
if (!(frame instanceof TextFrame)) exit();
var tag_to_apply = 'YYY';
var tag_to_skip = 'XXX';
var characters = frame.characters;
var start = 0, end = 0;
while (end < characters.length) {
// get the end of unttaged text
while (characters[end].associatedXMLElements[0].markupTag.name != tag_to_skip) {
end++;
if (end >= characters.length) break;
}
// apply the tag from the start to the end
app.activeDocument.xmlElements[0].xmlElements.add(tag_to_apply, characters.itemByRange(start, end-1));
// shift the end of untagged text
end += 2;
if (end >= characters.length) break;
// loop through the text tagged with the skip-tag
while (characters[end].associatedXMLElements[0].markupTag.name == tag_to_skip) {
end++;
if (end >= characters.length) break;
}
// get the new start and shift the end of untagged text
start = end;
end += 2;
}