我有一个 struct 化的InDesign文档.我 Select 了一个带有文本的文本框架,其中的一些符号用一个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.parentStory.characters;
// var characters = frame.parentStory.words; // <-- if you don't need to include the spaces

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;
    }

    // reset the start and shift the end further
    start = end;
    end += 2;
}

Javascript相关问答推荐

jest使用useLocate测试自定义挂钩

追踪执行顺序

为什么有些库公开了执行相同任务的方法,但每个方法都处于同步/同步上下文中?

在JavaScript中打开和关闭弹出窗口

在JavaScript中,如何将请求的回调函数的结果合并到运行的代码中?

具有相同参数的JS类

从mat—country—select获取整个Country数组

如何用显示网格平滑地将元素从一个地方移动到另一个地方?

将状态向下传递给映射的子元素

无法在nextjs应用程序中通过id从mongoDB删除'

当试图显示小部件时,使用者会出现JavaScript错误.

Javascript json定制

为什么123[';toString';].long返回1?

确保函数签名中的类型安全:匹配值

如何在.NET Core中将chtml文件链接到Java脚本文件?

使用NextJS+MongoDB+Prisma ORM获取无效请求正文,无法发布错误

<;img>;标记无法呈现图像

为什么在函数中添加粒子的速率大于删除粒子的速率?

将范围 Select 器添加到HighChart面积图

react 路由DOM有条件地呈现元素