3 Replies Latest reply on Jan 27, 2012 3:34 AM by John Hawkinson

# Working around the frame selection order issue in CS 4

Hi guys,

I'm trying to write a javascript in Indesign that will copy the contents of every selected text frame into the clipboard. The main problem is the selection order, since I'm using CS 4. I'm trying to work around the problem by writing the X and Y coordinates of every frame in arrays through a "for" loop, since the relevant order is always from top left to bottom right in my documents. However, I'm not sure what to do after sorting the arrays : how could I tell the program to use first the frame with the coordinates closest to 0, then the next, etc. ? So far I have this :

var LesX = new Array();

var LesY = new Array();

var Blocs = app.selection;

for (var i = 0; i < Blocs.length; i++) {

var myBounds = Blocs[i].geometricBounds;

var X = myBounds[1];

var Y = myBounds[0];

LesX.push(X);

LesY.push(Y);

}

LesX.sort(sortnum);

Thanks a lot for your help!

Ma

• ###### 1. Re: Working around the frame selection order issue in CS 4

Ths is not a good approach. your LesX array will be something like [90, 50, 30, 60, 20] before the sort. And after the sort it will be [20, 30, 50, 60, 90]. But that will not tell you how to find the object whose X value is 20.

I would write it like this:

```function byYX(a,b) {
var
aY = a.geometricBounds[0],
bY = b.geometricBounds[0],
aX = a.geometricBounds[1],
bX = b.geometricBounds[1],
dy = bY-aY,
dx = bX-aX;

return dy?dy:dx;
}

var blocs = app.selection;
blocs.sort(byYX);
```
• ###### 2. Re: Working around the frame selection order issue in CS 4

Thanks, John! That's exactly it. I'm quite amazed!

This seems to return my array of frames sorted from bottom to top, but it's simple enough to invert :

``````
dy = aY-bY,
dx = aX-bX;

``````