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

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

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

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

var doc = app.activeDocument;

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

1 Answer 1


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) {
  • That worked. Same result but down to 17 seconds. Thanks!
    – paul.ago
    Commented May 17, 2017 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
    Commented May 17, 2017 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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