在InDesign中查找未标记某个标签的框架符号,并用另一个标签标记它们

问题描述 投票:0回答:1

我有一份结构化的 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”的片段。

javascript adobe-indesign extendscript
1个回答
0
投票

可能你想要这样的东西:

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;
}
© www.soinside.com 2019 - 2024. All rights reserved.