Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
8

Need help with InDesign script error (Object undefined)

Explorer ,
Oct 12, 2023 Oct 12, 2023

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]);
     }
}

 

 

TOPICS
Scripting
1.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Engaged , Oct 13, 2023 Oct 13, 2023

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

Translate
Explorer ,
Oct 12, 2023 Oct 12, 2023

Screenshot 2023-10-12 at 3.35.24 PM.pngexpand image

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 12, 2023 Oct 12, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 12, 2023 Oct 12, 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)!");

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 12, 2023 Oct 12, 2023

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Oct 13, 2023 Oct 13, 2023

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 13, 2023 Oct 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Oct 13, 2023 Oct 13, 2023

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 13, 2023 Oct 13, 2023

Yep - using the top left of the frame as the reference point. Should I be using a different reference point? 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Oct 13, 2023 Oct 13, 2023

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 13, 2023 Oct 13, 2023

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Oct 13, 2023 Oct 13, 2023

You did correct myTF.move([newx, newy]) to myTF[i].move.....

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 13, 2023 Oct 13, 2023

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]);
    }
}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 13, 2023 Oct 13, 2023
LATEST

This works perfectly. Thank you so much for your help @rob day!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines