3 Replies Latest reply on May 17, 2016 12:39 AM by Loic.Aigon

# Can someone simplify this script

Hi All,

I just write this script move text frames with script labels of page 1 in my document. I felt that this is something simple but having lot of loop with in it. Is there any way to simplify it by catch the text frame directly in secon and third loops.

```var jt=app.activeDocument.pages[0].textFrames;
for (i=0;i<jt.length;i++)
{
if ((jt[i].label=="Label1"))
{
if(jt[i].geometricBounds[2]>153)
{
var jt1=app.activeDocument.pages[0].textFrames;
for (j=0;j<jt.length;j++)
{
if ((jt1[j].label=="Label2"))
{
jt1[j].geometricBounds=[(jt[i].geometricBounds[2]+25),jt1[j].geometricBounds[1],(jt[i].geometricBounds[2]+48),jt1[j].geometricBounds[3]];
var jt2=app.activeDocument.pages[0].textFrames;
for (k=0;k<jt.length;k++)
if ((jt2[k].label=="Label3"))
jt2[k].geometricBounds=[jt1[j].geometricBounds[0],jt1[j].geometricBounds[1],jt2[k].geometricBounds[2],jt2[k].geometricBounds[3]];

}
}

}
}
}
```

My layout as below:

• ###### 1. Re: Can someone simplify this script

Not too sure if this is what you want. With an attempt to figure out your logic:

First within a loop of all of your text frames, create 3 arrays:

(1)  x1Array = All with Label1 and geometricBounds[2] > 153

(2)  x2Array = All with Label2

(3) x3Array - All with Label3

Then within a loop of the x1Array items, adjust the geometric bounds for all items in the x2Array and x3Arrays

Not any simpler, but easier to follow and smaller arrays to loop through.

It will be interesting to see what others come up with.

var x1Array = [];

var x2Array= [];

var x3Array = [];

var iBounds, jBounds, newjBounds, kBounds, newkBounds

app.scriptPreferences.measurementUnit=MeasurementUnits.POINTS;

//create array of items by label

it=app.activeDocument.pages[0].textFrames;

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

if (it[i].label=="Label1") {

iBounds = it[i].geometricBounds;

if (iBounds[2] > 153) {

x1Array.push(it[i]);

}

} else if (it[i].label=="Label2") {

x2Array.push(it[i]);

} else if (it[i].label=="Label3") {

x3Array.push(it[i]);

}

}

//loop through x1Array and adjust bounds for other items

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

iBounds = x1Array[i].geometricBounds;

for (var j = 0; j < x2Array.length; j++) {

jBounds = x2Array[j].geometricBounds;

newjBounds = [(iBounds[2]+25), jBounds[1], (iBounds[2]+48), jBounds[3]];

for (var k = 0; k < x3Array.length; k++) {

kBounds = x3Array[k].geometricBounds;

newkBounds = [newjBounds[0], newjBounds[1], kBounds[2], kBounds[3]];

}

}

}

• ###### 2. Re: Can someone simplify this script

An alternative is as follows: frames are offset to the right, the offset is determined by the value of the label. You can then move each frame to the right using the offset:

```jt = app.activeDocument.pages[0].textFrames;
for (i = 0; i < jt.length; i++) {
offset = jt[i].name.match (/\d+\$/)[0];
jt[i].move (undefined, [offset*100, 0]);
}
```

Peter

• ###### 3. Re: Can someone simplify this script

I second Peter's approach. The best way to make scripts "simple" is actually not to code anything until you get a sufficient hindsight to consider your problem in a generic way. Once you can resume your issue to a generic approach, it's easier to write down generic and reusable code.