[英]How can I get the next paragraph after my selection in InDesign?
I'm using Adobe InDesign and ExtendScript to find a keyword using app.activeDocument.findGrep()
, and I've got this part working well. 我正在使用Adobe InDesign和ExtendScript来使用app.activeDocument.findGrep()
查找关键字,我的这部分工作得很好。 I know findGrep()
returns an array of Text objects. 我知道findGrep()
返回一个Text对象数组。 Let's say I want to work with the first result: 假设我想使用第一个结果:
var result = app.activeDocument.findGrep()[0];
How can I get the next paragraph following result
? 如何获得下一段result
?
var nextParagraph = result.paragraphs[-1].insertionPoints[-1].paragraphs[-1];
The Indesign DOM has different Text objects that you can use to address paragraphs, words, characters, or insertion points (the space between characters where your blinking cursor sits). Indesign DOM具有不同的Text对象,可用于处理段落,单词,字符或插入点(闪烁光标所在的字符之间的空间)。 A group of Text objects is called a collection. 一组Text对象称为集合。 Collections in Indesign are similar to arrays, but one significant difference is that they can be addressed from the back by using a negative index ( paragraphs[-1]
). Indesign中的集合类似于数组,但一个显着的区别是它们可以通过使用负索引( paragraphs[-1]
)从后面解决。
result
refers to the findGrep() result. result
是指findGrep()的结果。 It can be any Text object, depending on your search terms. 它可以是任何Text对象,具体取决于您的搜索条件。
paragraphs[-1]
means the last paragraph of your result (Paragraph A). paragraphs[-1]
表示结果的最后一段(段落A)。 If the search result is just one word, then this refers the word's enclosing paragraph, and this collection of paragraphs has just one element. 如果搜索结果只是一个单词,那么这将引用单词的封闭段落,而这个段落集合只有一个元素。
insertionPoints[-1]
refers to the last insertionPoint of Paragraph A. This comes after the paragraph mark and before the first character of the next paragraph (Paragraph B). insertionPoints[-1]
引用段落A的最后一个插入点。它出现在段落标记之后和下一段落的第一个字符之前 (段落B)。 This insertionPoint belongs to both this paragraph and the following paragraph. 此insertedPoint属于本段和以下段落。
paragraphs[-1]
returns the last paragraph of the insertionPoint, which is Paragraph B (the next paragraph). paragraphs[-1]
返回insertionPoint的最后一段,即段落B(下一段)。
Althouh nextItem seems totally appropriate and efficient, it may be a source of performance leaks especially if you call it several times in a huge loop. Althouh nextItem似乎完全合适且高效,它可能是性能泄漏的来源,特别是如果你在一个巨大的循环中多次调用它。 Keep in mind that nextItem() is a function creating an internal scope and stuff… An alternative is to navigate within the story and reach the next paragraph thanks to the insertionPoints indeces: 请记住,nextItem()是一个创建内部范围和内容的函数......另一种方法是在故事中导航并通过insertPoints 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();
Not claiming the absolute truth here. 没有在这里声称绝对真理。 Just advising about possible issues with nextItem(). 只是建议nextItem()可能出现的问题。
more simply code below 更简单的代码如下
result.paragraphs.nextItem(result.paragraphs[0]);
thank you 谢谢
mg. 毫克。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.