1

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?

2

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);
  };
}
  • That worked. Same result but down to 17 seconds. Thanks! – paul.ago May 17 '17 at 11:43
  • Try using a negative while loop such as while ( n-- ) … and also allTextFrames[n] = pi; You may save some extra time maybe. – Loic May 17 '17 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.