Copy link to clipboard
Copied
Hi! I am trying to use a script from a past thread (https://community.adobe.com/t5/indesign-discussions/indesign-script-to-change-the-x-and-y-coordinate...) and it seemed to work for the posters participating in that thread. This script is supposed to take text frames at a certain x, y coordinate and move them using to new x, y coordinates. However, I keep receiving an error that says Error string: undefined is not an object whenever I enter the coordinates.
I'm using Adobe Indesign 2024 (v19.0 latest version). I am by no means acquainted with JavaScript at all so I have no idea where it is going wrong. Thank you!
This is the script:
var myDoc = app.activeDocument;
var w = new Window ('dialog {alignChildren: "top"}', 'Move TF (x,y) To (newx, newy)!');
w.alignChildren = "right";
var group1 = w.add ('group');
group1.add ('statictext {text: "Type the old x:"}');
var UIx = group1.add ("edittext", undefined, 0);
UIx.characters = 5;
UIx.minimumSize.width = 60;
UIx.maximumSize.width = 60;
UIx.active = true;
var group2 = w.add ('group');
group2.add ('statictext {text: "… and the old y:"}');
var UIy = group2.add ("edittext", undefined, 0);
UIy.characters = 5;
UIy.minimumSize.width = 60;
UIy.maximumSize.width = 60;
var group3 = w.add ('group');
group3.add ('statictext {text: "Type the new x:"}');
var UInewx = group3.add ("edittext", undefined, 0);
UInewx.characters = 5;
UInewx.minimumSize.width = 60;
UInewx.maximumSize.width = 60;
var group4 = w.add ('group');
group4.add ('statictext {text: "… and the new y:"}');
var UInewy = group4.add ("edittext", undefined, 0);
UInewy.characters = 5;
UInewy.minimumSize.width = 60;
UInewy.maximumSize.width = 60;
var buttons = w.add ('group {alignment: "center"}');
buttons.add ('button {text: "OK"}');
buttons.add ('button {text: "Cancel"}');
if (w.show () == 1) {
var x = Number (UIx.text);
x = x.toFixed(2);
var y = Number (UIy.text);
y = y.toFixed(2);
var newx = UInewx.text;
var newy = UInewy.text;
}
else
exit ();
app.doScript("main()", ScriptLanguage.javascript, undefined, UndoModes.FAST_ENTIRE_SCRIPT, "Move TF (x,y) To (newx, newy)!");
function main()
{
var myTF = myDoc.textFrames.everyItem().getElements();
for ( i = 0; i < myTF.length; i++)
{
var gB = myTF.geometricBounds;
var y1 = gB[0].toFixed(2);
var x1 = gB[1].toFixed(2);
if ( y1 == y && x1 == x ) myTF.move([newx, newy]);
}
}
1 Correct answer
myTF is an array and you are not indexing it. Try myTF[i].geometricBounds.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Just run your script outside of the app.doScript method. That way you will have the exact line where error occurs. When the message "Object is undefined" appears, it's generally that you are trying to call a property that doesn't exists or the object is not valid.
As I said, once you execute your script directly, you will have a clearer picture for debugguing.
Copy link to clipboard
Copied
Thanks! The error shows that the line is here -
app.doScript("main()", ScriptLanguage.javascript, undefined, UndoModes.FAST_ENTIRE_SCRIPT, "Move TF (x,y) To (newx, newy)!");
Copy link to clipboard
Copied
Did you read what I wrote?
👉👉👉"Just run your script outside of the app.doScript method"👈👈👈
i.e NOT using app.doScript BUT main(); directly…
Then you would have a different error line
Copy link to clipboard
Copied
myTF is an array and you are not indexing it. Try myTF[i].geometricBounds.
Copy link to clipboard
Copied
Thanks for this! I tried it and no longer get the Object is undefined error, but when I run the script and input the old and new (x,y) coordinates, no text frames in the document actually change. Any ideas as to why that could be?
Copy link to clipboard
Copied
Are you using the coordinates for the top left of the frame?
Copy link to clipboard
Copied
Yep - using the top left of the frame as the reference point. Should I be using a different reference point?
Copy link to clipboard
Copied
Using the top left and your script, it is working for me. Could it be a mix in units? I am making sure that I match the info on the toolbar.
Copy link to clipboard
Copied
Thank you so much! I had forgotten to add in my units when typing in the x y coordinates, so all your solutions combined with the missing bracket as pointed out by @rob day fixed the issue and it is now working! Thank you very much again for your patience and for taking the time to help a newbie out.
Copy link to clipboard
Copied
You did correct myTF.move([newx, newy]) to myTF[i].move.....
Copy link to clipboard
Copied
but when I run the script and input the old and new (x,y) coordinates, no text frames in the document actually change. Any ideas as to why that could be?
Hi @j25m27c , VS Code is flagging a missing brace in the if else statement at the end of the dialog code. This works for me:
var myDoc = app.activeDocument;
var w = new Window ('dialog {alignChildren: "top"}', 'Move TF (x,y) To (newx, newy)!');
w.alignChildren = "right";
var group1 = w.add ('group');
group1.add ('statictext {text: "Type the old x:"}');
var UIx = group1.add ("edittext", undefined, 0);
UIx.characters = 5;
UIx.minimumSize.width = 60;
UIx.maximumSize.width = 60;
UIx.active = true;
var group2 = w.add ('group');
group2.add ('statictext {text: "… and the old y:"}');
var UIy = group2.add ("edittext", undefined, 0);
UIy.characters = 5;
UIy.minimumSize.width = 60;
UIy.maximumSize.width = 60;
var group3 = w.add ('group');
group3.add ('statictext {text: "Type the new x:"}');
var UInewx = group3.add ("edittext", undefined, 0);
UInewx.characters = 5;
UInewx.minimumSize.width = 60;
UInewx.maximumSize.width = 60;
var group4 = w.add ('group');
group4.add ('statictext {text: "… and the new y:"}');
var UInewy = group4.add ("edittext", undefined, 0);
UInewy.characters = 5;
UInewy.minimumSize.width = 60;
UInewy.maximumSize.width = 60;
var buttons = w.add ('group {alignment: "center"}');
buttons.add ('button {text: "OK"}');
buttons.add ('button {text: "Cancel"}');
if (w.show () == 1) {
var x = Number (UIx.text);
x = x.toFixed(2);
var y = Number (UIy.text);
y = y.toFixed(2);
var newx = UInewx.text;
var newy = UInewy.text;
} else {
exit ();
}
app.doScript("main()", ScriptLanguage.javascript, undefined, UndoModes.FAST_ENTIRE_SCRIPT, "Move TF (x,y) To (newx, newy)!");
function main(){
var myTF = myDoc.textFrames.everyItem().getElements();
for ( i = 0; i < myTF.length; i++){
var gB = myTF[i].geometricBounds;
var y1 = gB[0].toFixed(2);
var x1 = gB[1].toFixed(2);
if ( y1 == y && x1 == x ) myTF[i].move([newx, newy]);
}
}
Copy link to clipboard
Copied
This works perfectly. Thank you so much for your help @rob day!

