Mobile app version of vmapp.org
Login or Join
Fox8063795

: Finding all TexFrame and TextPath objects in an InDesign document via scripting I'm looking for a fast way to extract all the TextFrame and TextPath objects in a document via scripting. I have

@Fox8063795

Posted in: #AdobeIndesign #IndesignScripting

I'm looking for a fast way to extract all the TextFrame and TextPath objects in a document via scripting.

I have a working implementation that works also for inline elements but it's really slow since it traverses all the objects recursively.

On a file with ~2000 elements (of them only 700 are TextFrames), this script takes 132 seconds to execute, which is a bit too much for a 2-year old i7 MBP.

This is the current implementation:
#include "undercorejs.js"

var allItems = [];

function recursiveSearchForTextObjects(elements) {
for (var j = 0; j < elements.length; j++) {
var child = elements[j];

if(child instanceof TextFrame || child instanceof TextPath){
if(_.indexOf(allItems, child) < 0){ // No dupes
allItems.push(child);
}
}

if(typeof child.allPageItems != "undefined"){
recursiveSearchForTextObjects(child.allPageItems);
}

if(typeof child.textPaths != "undefined"){
recursiveSearchForTextObjects(child.textPaths);
}

if(typeof child.groups != "undefined"){
recursiveSearchForTextObjects(child.groups);
}
}
}

var doc = app.activeDocument;
recursiveSearchForTextObjects(doc.allPageItems);


Is there a better way of extracting all the TextFrame/TextPath objects including the inline elements?

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Fox8063795

1 Comments

Sorted by latest first Latest Oldest Best

 

@Cofer715

Yes, you just need to loop over document's allPageItems property to access all page items, no matter if they are nested or not.

Then you can check if they are text frames or text paths and if so, collect them all in an Array.

// @target InDesign

var doc = app.activeDocument;
var allTextFrames = [];

for (var i = doc.allPageItems.length - 1; i >= 0; i--) {
var pi = doc.allPageItems[i];
if(pi instanceof TextFrame || pi instanceof TextPath) {
allTextFrames.push(pi);
};
}

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme