9 Replies Latest reply on Oct 4, 2016 8:55 AM by Awitmer

    Indesign script to change the x and y coordinates of page items

    austinw

      Hi all!

       

       

       

      Would there be a way to find text frames with the same x and y coordinates

      in a document and make a change to the x and y coordinates that would change

      all the text frames by a script of some sort?

       

       

       

      Thanks a bunch for your help!

       

       

       

      Austin Witmer_2

       

       

       

       

        • 1. Re: Indesign script to change the x and y coordinates of page items
          Skemicle Level 3

          Yes there is a way to do this. =)

          • 2. Re: Indesign script to change the x and y coordinates of page items
            Obi-wan Kenobi Adobe Community Professional

            … Something like this:

             

            var myTF = app.activeDocument.textFrames.everyItem().getElements();
            var x = 10;
            var y = 15;  
            var movex = 8;
            var movey = 22;
            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].geometricBounds = [gB[0]+movey, gB[1]+movex, gB[2]+movey, gB[3]+movex];
                 } 
            

             

            [ need to be confirmed by the experts! ]

             

            (^/) 

            • 3. Re: Indesign script to change the x and y coordinates of page items
              Obi-wan Kenobi Adobe Community Professional

              Still not a great master of UI! 

               

              Capture d’écran 2016-09-27 à 14.14.10.png

              Capture d’écran 2016-09-27 à 14.14.23.png

               

              Done! 

              [ To be validated by the [JS] Masters! ]

               

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

               

               

              (^/)

              1 person found this helpful
              • 4. Re: Indesign script to change the x and y coordinates of page items
                Awitmer Level 1

                This was extremely helpful!

                 

                Thank you very much! I can’t figure out  a way to mark your answer as correct, otherwise I would.

                • 5. Re: Indesign script to change the x and y coordinates of page items
                  Obi-wan Kenobi Adobe Community Professional

                  You're welcome!  It was funny (and interesting) to write and I would be truly interested by [JS] Masters' comments!

                   

                  (^/) 

                  • 6. Re: Indesign script to change the x and y coordinates of page items
                    Awitmer Level 1

                    Now I've got another question. Would there be a way to modify this script so that it would work for graphic frames and not just text frames?

                     

                    Thanks in advance!

                    • 8. Re: Indesign script to change the x and y coordinates of page items
                      [Jongware] Most Valuable Participant

                      See the line where Obi-Wan uses "myDoc.textFrames.everyItem()"? That is to specifically target just these (which is what you asked for).

                       

                      Documents have a general 'pageItems' property, which enumerates all items in the document, so not just text frames and graphics, but lines and rectangles and polygons and ovals and groups as well. ("Groups" make life interesting here. If I recall correctly, it and its contents are reported twice.)

                       

                      To target just graphic containers, you'd typically use 'myDoc.allGraphics' (without the 'everyItem().getElements() part', it's not a set of objects but already a plain array). Then, for each graphic – the image inside any kind of frame – you use its parent's geometricBounds – the frame in which the graphic is placed.

                      • 9. Re: Indesign script to change the x and y coordinates of page items
                        Awitmer Level 1

                        Thanks all for your help!

                        A friend of mine suggested that I change textFrames to pageItems instead and that has worked perfectly for me!

                        Cheers!