在 InDesign 中选择后如何获得下一段?

How can I get the next paragraph after my selection in InDesign?

我正在使用 Adob​​e InDesign 和 ExtendScript 来查找使用 app.activeDocument.findGrep() 的关键字,并且这部分工作正常。我知道 findGrep() returns 一个文本对象数组。假设我想使用第一个结果:

var result = app.activeDocument.findGrep()[0];

如何获取 result 之后的下一段?

使用 InDesign DOM

var nextParagraph = result.paragraphs[-1].insertionPoints[-1].paragraphs[-1];

Indesign DOM 具有不同的文本对象,您可以使用这些对象来定位段落、单词、字符或插入点(闪烁光标所在的字符之间的 space)。一组 Text 对象称为集合。 Indesign 中的集合类似于数组,但一个显着区别是它们可以通过使用负索引 (paragraphs[-1]) 从后面寻址。

result 指的是 findGrep() 结果。它可以是任何文本对象,具体取决于您的搜索词。

paragraphs[-1] 表示结果的最后一段(A 段)。如果搜索结果只有一个词,那么this指的是这个词的封闭段落,这个段落集合只有一个元素。

insertionPoints[-1]指的是段落A的最后一个插入点。这是段落标记之后和第一个字符之前下一段(B 段)。此插入点属于本段下一段

paragraphs[-1] returns插入点的最后一段,即Paragraph B(下一段)

更简单的代码如下

result.paragraphs.nextItem(result.paragraphs[0]);

谢谢

毫克。

虽然 nextItem 看起来完全合适且高效,但它可能是性能泄漏的来源,尤其是当您在一个巨大的循环中多次调用它时。请记住, nextItem() 是一个创建内部范围和内容的函数...... 另一种方法是在故事中导航并到达下一段,这要归功于 indeces:

var main = function() {
 var doc, found, st, pCurr, pNext, ipNext, ps;
 if (!app.documents.length) return;
 doc = app.activeDocument;
 
 
 app.findGrepPreferences = app.changeGrepPreferences = null;
 
 app.findGrepPreferences.findWhat = "\A.";
 
 found = doc.findGrep();
 
 if ( !found.length) return;
 
 found  = found[0];
 st = found.parentStory;
 
 pCurr = found.paragraphs[0];
 
 ipNext = st.insertionPoints [ pCurr.insertionPoints[-1].index ];
 var pNext = ipNext.paragraphs[0];
 
 alert( pNext.contents );
};

main();

此处不主张绝对真理。只是建议 nextItem() 可能存在的问题。