Skip to main content
Participating Frequently
October 12, 2023
Answered

Need help with InDesign script error (Object undefined)

  • October 12, 2023
  • 2 replies
  • 2536 views

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-coordinates-of-page-items/m-p/8583897) 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]);
     }
}

 

 

This topic has been closed for replies.
Correct answer John D Herzog

myTF is an array and you are not indexing it. Try myTF[i].geometricBounds.

2 replies

John D Herzog
John D HerzogCorrect answer
Inspiring
October 13, 2023

myTF is an array and you are not indexing it. Try myTF[i].geometricBounds.

j25m27cAuthor
Participating Frequently
October 13, 2023

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?

John D Herzog
Inspiring
October 13, 2023

Are you using the coordinates for the top left of the frame?

j25m27cAuthor
Participating Frequently
October 12, 2023

Participating Frequently
October 13, 2023

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.

j25m27cAuthor
Participating Frequently
October 13, 2023

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)!");