6 Replies Latest reply on Apr 9, 2013 5:32 AM by Muthuraj Durairaj

# Change X, Y position of a text frame based on some conditions

Hi All,

I am trying to re-position the Y position alone of all text frames in a documennt if that text frame has certain Y position. Here is the code I tried.

It works, but, it place all the text frames in to the given myTextFrame.move ( ["216.425 pt","54 pt"] ); one on another. Whereas, I need to change Y position of only the text frames with the following conditions.

The conditions are:

If X="216.425 pt" AND Y="103.89 pt", change to X="216.425 pt" and Y="54 pt"   OR

If X="216.425 pt" AND Y="78.945 pt", change to X="216.425 pt" and Y="54 pt"    OR

If X="216.425 pt" AND Y="79.157 pt", change to X="216.425 pt" and Y="54 pt"    OR

If X="213.425 pt" AND Y="486.647 pt", change to X="216.425 pt" and Y="54 pt"    OR

If X="213.425 pt" AND Y="74.409 pt", change to X="216.425 pt" and Y="54 pt"

Please help me on this to add the above conditions into the following code and reposition only the text frames has the above said XY positions.  Is there anyother way other than "move" statement?

var myDocument=app.activeDocument;

var myPages=myDocument.pages;

for(p=0;p<myPages.length;p++) {

var allTextFrames=myPages[p].textFrames;

for (s=0; s<allTextFrames.length; s++) {

var myTextFrame = allTextFrames[s];

var myTextFrameBounds =  myTextFrame.geometricBounds;

var properly_aligned = "54 pt";

if ((myTextFrameBounds[0] = "103.89 pt") && (myTextFrameBounds[1] ="216.425 pt")) {

myTextFrame.move ( ["216.425 pt","54 pt"] );

}

}

}

• ###### 1. Re: Change X, Y position of a text frame based on some conditions

Hi,

change:

if ((myTextFrameBounds[0] = "103.89 pt") && (myTextFrameBounds[1] ="216.425 pt"))

to:

if ((myTextFrameBounds[0] == "103.89 pt") && (myTextFrameBounds[1] == "216.425 pt"))

rgds

• ###### 2. Re: Change X, Y position of a text frame based on some conditions

Hi Jumb_Over,

Thanks for your quick reply. I have modified the line as you suggested. But, now no text frame is moving and nothing is happening. Please suggest

• ###### 3. Re: Change X, Y position of a text frame based on some conditions

Hi,

Alert geometrictBounds of textFrame you want to be moved. Check these numbers. Notice there could be a 9-digits decimal numbers. Thats why comparing can fail. So use Math.round() or Math.floor() or Math.ceil().

rgds

• ###### 4. Re: Change X, Y position of a text frame based on some conditions

Hi

var myDocument=app.activeDocument;

var myPages=myDocument.pages;

for(p=0;p<myPages.length;p++) {

var allTextFrames=myPages[p].textFrames;

for (s=0; s<allTextFrames.length; s++) {

var myTextFrame = allTextFrames[s];

var myTextFrameBounds =  myTextFrame.geometricBounds;

var properly_aligned = "54 pt";

if (Math.round(myTextFrameBounds[0] == "103.89 pt") && Math.round(myTextFrameBounds[1] =="216.425 pt")) {

myTextFrame.move ( ["216.425 pt","54 pt"] );

}

}

}

• ###### 5. Re: Change X, Y position of a text frame based on some conditions

Muthuraj Durairaj wrote:

Review your code! The logic is severely flawed. Three errors that stand out (there may be more, but these need fixing right away):

if (Math.round(myTextFrameBounds[0] == "103.89 pt")

1. You are comparing the result of a Math.round operation to a string specifying a measurement unit. Measurement units are *not* a number, and you cannot use the "pt" suffix here. You also should be *very* careful in comparing numbers with strings, and best is to avoid it as long as you can't be sure what's happening.

2. Math.round is a function, and it appears you want it to apply to 'myTextFrameBounds[0]'. So it needs a closing parenthesis right after that. NOT around the entire expression "Math.round(myTextFrameBounds[0] == "103.89 pt")" -- this evaluates to "Math.round(false)", and that in turn always evaluates to "0".

3. With these problems fixed: the result of "Math.round" will *never* contain a decimal point. This comparison will always fail.

• ###### 6. Re: Change X, Y position of a text frame based on some conditions

Hi Jongware,

I have modified the code little bit after referring one of the post. Here is the updated code. It is working fine for the whole number (var oldposition1) as input value and does not work for decimal values (var oldposition2, var oldposition3, var oldposition4 and var oldposition5). However, it repositions the frames with decimal values (newposition1, newposition2 and newposition3). I am facing problem with the decimal input values for original position of text frames. Is there any other way to give decimal values as input as my document comes with these decimal values as X, Y position.

var oldposition1 = [216, 103] // original position, upper left corner

var oldposition2 = [216.425, 78.945] // original position, upper left corner

var oldposition3 = [216.425, 79.157] // original position, upper left corner

var oldposition4 = [213.425, 74.409] // original position, upper left corner

var oldposition5 = [213.425, 486.647] // original position, upper left corner

var newposition1 = [216.425, 54] // new position, upper left corner

var newposition2 = [36, 54] // new position, upper left corner

var newposition3 = [216.425, 437.4] // new position, upper left corner

var myObjects = app.activeDocument.pageItems.everyItem().getElements();

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

if (Math.round(myObjects[i].geometricBounds[1])==oldposition1[0]&&Math.round(myObjects[i].ge ometricBounds[0])==oldposition1[1]){

myObjects[i].move(newposition1);

}

else if (Math.round(myObjects[i].geometricBounds[1])==oldposition2[0]&&Math.round(myObjects[i].ge ometricBounds[0])==oldposition2[1]){

myObjects[i].move(newposition1);

}

else if (Math.round(myObjects[i].geometricBounds[1])==oldposition3[0]&&Math.round(myObjects[i].ge ometricBounds[0])==oldposition3[1]){

myObjects[i].move(newposition1);

}

else if (Math.round(myObjects[i].geometricBounds[1])==oldposition4[0]&&Math.round(myObjects[i].ge ometricBounds[0])==oldposition4[1]){

myObjects[i].move(newposition1);

}

else if (Math.round(myObjects[i].geometricBounds[1])==oldposition5[0]&&Math.round(myObjects[i].ge ometricBounds[0])==oldposition5[1]){

myObjects[i].move(newposition3);

}

}