Skip to main content
Inspiring
March 29, 2012
Answered

Sorting text frames by height

  • March 29, 2012
  • 2 replies
  • 794 views

Having some trouble sorting text frames by height with the javascript sort function... is this possible?

var doc = app.activeDocument;

function tallestFrame(page) {

          var frames = page.textFrames.everyItem().getElements();

          frames.sort(function(a,b) {

                    var aHeight = (frames.geometricBounds[2] - frames.geometricBounds[0]);

                    var bHeight = (frames.geometricBounds[2] - frames.geometricBounds[0]);

                    return aHeight - bHeight;

          });

}

alert(tallestFrame(doc.pages[0]));

This topic has been closed for replies.
Correct answer John Hawkinson

In addition to Jongware's observation, this is wrong:

var frames = page.textFrames.everyItem().getElements();
frames.sort(function(a,b) {
          var aHeight = (frames.geometricBounds[2] - frames.geometricBounds[0]);

a and b are going to be individual objects in the frames, not their indices, so you should use

var aHeight = a.geometricBounds[2]-a.geometricBounds[0];

and soforth.

2 replies

John Hawkinson
John HawkinsonCorrect answer
Inspiring
March 30, 2012

In addition to Jongware's observation, this is wrong:

var frames = page.textFrames.everyItem().getElements();
frames.sort(function(a,b) {
          var aHeight = (frames.geometricBounds[2] - frames.geometricBounds[0]);

a and b are going to be individual objects in the frames, not their indices, so you should use

var aHeight = a.geometricBounds[2]-a.geometricBounds[0];

and soforth.

Inspiring
March 30, 2012

Thanks, I guess that's where I was getting stuck

Jongware
Community Expert
Community Expert
March 29, 2012

You don't return a value from your function.