14 Replies Latest reply on Jun 10, 2014 12:54 AM by Chinnadk

    Change Center position by Indesign script

    hasvi Level 1

      Hi,

      I want to move my objects to the center position of page width. Is this possible by script?

       

      by

      hasvi

        • 1. Re: Change Center position by Indesign script
          csm_phil Level 4

          Hi hasvi,

           

          try the below lines.

           

          
          app.activeDocument.align(app.selection[0], AlignOptions.TOP_EDGES, AlignDistributeBounds.MARGIN_BOUNDS, false);// this line move the object to top align
          app.activeDocument.align(app.selection[0], AlignOptions.HORIZONTAL_CENTERS, AlignDistributeBounds.MARGIN_BOUNDS, false);
          
          

           

          Please close the thread whenever you got the "Answer" or "Helpful"....

           

          thx,

          csm_phil

          • 2. Re: Change Center position by Indesign script
            hasvi Level 1

            Hi,

             

            Nice, but I want to select all the objects in last page(only) and move to the 'AlignOptions.HORIZONTAL_CENTERS' this position only, can you help?

             

            by

            hasvi

            • 3. Re: Re: Change Center position by Indesign script
              csm_phil Level 4

              hi hasvi,

               

              try the below lines.

               

              var myDoc = app.activeDocument;
              var myLpage = myDoc.pages[-1].pageItems.everyItem();
              myDoc.align(myLpage, AlignOptions.HORIZONTAL_CENTERS, AlignDistributeBounds.MARGIN_BOUNDS, false);  
              

               

               

              thx,

              csm_phil

              • 5. Re: Change Center position by Indesign script
                hasvi Level 1

                Hi,

                one more help pls, now I want to apply this for en number of Indesign files in particular folder. how is it possible?

                 

                by

                hasvi

                • 6. Re: Change Center position by Indesign script
                  Chinnadk Level 4

                  Hi Hasvi,

                   

                  Try this.

                   

                  var path = Folder.selectDialog ("Select folder");

                  var files = path.getFiles("*.indd");

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

                  {

                      var doc = app.open(files[i],false)

                      var myLpage = doc.pages[-1].pageItems.everyItem();

                      //doc.align(myLpage, AlignOptions.VERTICAL_CENTERS, AlignDistributeBounds.MARGIN_BOUNDS);

                      doc.align(myLpage, AlignOptions.HORIZONTAL_CENTERS, AlignDistributeBounds.MARGIN_BOUNDS);

                      doc.save();

                      doc.close();

                      }

                   

                  Regards,

                  Chinna

                  • 7. Re: Change Center position by Indesign script
                    hasvi Level 1

                    Hi chinna,

                     

                    thanks your valuable code, and all the text frames and images are moved to the center of page width as like below, now my 'page width', 'text frames' and 'image frames' everything must be equal width. is this possible?

                    Here Full green is the page width,

                     

                    1.png

                    by

                    hasvi

                    • 8. Re: Change Center position by Indesign script
                      Chinnadk Level 4

                      Hi Hasvi,

                       

                      Try this.

                       

                      var doc = app.activeDocument,

                          width = doc.documentPreferences.pageWidth,

                          txfms = doc.textFrames,

                          rects = doc.rectangles,

                          oldorigin = doc.viewPreferences.rulerOrigin;

                      doc.viewPreferences.rulerOrigin = RulerOrigin.pageOrigin;

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

                      {

                              var gb = txfms[i].geometricBounds;

                              txfms[i].geometricBounds = [gb[0],0,gb[2],width];

                          }

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

                      {

                              var gb = rects[i].geometricBounds;

                              rects[i].geometricBounds = [gb[0],0,gb[2],width];

                          }

                      doc.viewPreferences.rulerOrigin = oldorigin;

                       

                      Regards,

                      Chinna

                      • 9. Re: Change Center position by Indesign script
                        hasvi Level 1

                        Hi Chinna,

                         

                        My first action is 'doc.align(myLpage, AlignOptions.HORIZONTAL_CENTERS, AlignDistributeBounds.MARGIN_BOUNDS);' after that I want to change the width. How to combine below script to your code because I need this action only for last page of my Indesign file, and I have more indesign files in particular folder. pls help.

                         

                        previous code upto center position:

                         

                         

                        var path = Folder.selectDialog ("Select folder");

                        var files = path.getFiles("*.indd");

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

                        {

                            var doc = app.open(files[i],false)

                            var myLpage = doc.pages[-1].pageItems.everyItem();

                            //doc.align(myLpage, AlignOptions.VERTICAL_CENTERS, AlignDistributeBounds.MARGIN_BOUNDS);

                            doc.align(myLpage, AlignOptions.HORIZONTAL_CENTERS, AlignDistributeBounds.MARGIN_BOUNDS);

                            doc.save();

                            doc.close();

                            }

                         

                        First action: Select the folder

                        Second: change the all items to center position

                        Third: Match all items width to the page width

                         

                        Everything only for last page of my Indesign file.

                         

                        by

                        hasvi

                        • 10. Re: Change Center position by Indesign script
                          Chinnadk Level 4

                          If all the items on the last page needs to be in the width of a page, then why do you want to change those items to the center before changing the width? Anyway, here is the code.

                           

                          var path = Folder.selectDialog ("Select folder");

                          var files = path.getFiles("*.indd");

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

                          {

                              var doc = app.open(files[i])

                              var myLpageItems = doc.pages[-1].pageItems.everyItem();

                              //doc.align(myLpageItems, AlignOptions.VERTICAL_CENTERS, AlignDistributeBounds.MARGIN_BOUNDS);

                              doc.align(myLpageItems, AlignOptions.HORIZONTAL_CENTERS, AlignDistributeBounds.MARGIN_BOUNDS);

                              var width = doc.documentPreferences.pageWidth;

                              var txfms = doc.pages[-1].textFrames;

                              var rects = doc.pages[-1].rectangles;

                              var oldorigin = doc.viewPreferences.rulerOrigin;

                              doc.viewPreferences.rulerOrigin = RulerOrigin.pageOrigin;

                              for(var j =0;j<txfms.length;j++)

                              {

                                  var gb = txfms[j].geometricBounds;

                                  txfms[j].geometricBounds = [gb[0],0,gb[2],width];

                              }

                              for(var j =0;j<rects.length;j++)

                              {

                                  var gb = rects[j].geometricBounds;

                                  rects[j].geometricBounds = [gb[0],0,gb[2],width];

                              }

                              doc.viewPreferences.rulerOrigin = oldorigin;

                              doc.save();

                              doc.close();

                              }

                           

                          Regards,

                          Chinna

                          • 11. Re: Change Center position by Indesign script
                            hasvi Level 1

                            Hi chinna,

                             

                            FYI all items should be center to the page width at the same time my text frame width must be match with page width, because we need any corrections we will insert the text lines in text frame, this is our concept. This is only for last page in my Indesign.

                             

                            It shows the error as like below:

                            1.png

                            by

                            hasvi

                            • 12. Re: Change Center position by Indesign script
                              Chinnadk Level 4

                              Hi Hasvi,

                               

                              This is an error because of a dialog was opened in indesign. Please close it and check again.

                               

                              And the above code will adjust the width for both text Frames and Rectangles if you don't want to do it on the rectangles then delete the below lines from your code.

                               

                                  var rects = doc.pages[-1].rectangles;

                                  for(var j =0;j<rects.length;j++)

                                  {

                                      var gb = rects[j].geometricBounds;

                                      rects[j].geometricBounds = [gb[0],0,gb[2],width];

                                  }

                               

                              Regards,

                              Chinna

                              • 13. Re: Change Center position by Indesign script
                                hasvi Level 1

                                Hi chinna,

                                 

                                 

                                For some reason its shows the error again, even after closing the all dialogue box in indesign. Dont consider order(first, second and three), because error shows on near by center alignment line. We need the final output as like all text frames width must be match with page width and graphic frames should be centre on page width.

                                 

                                by

                                hasvi

                                • 14. Re: Change Center position by Indesign script
                                  Chinnadk Level 4

                                  Restart Indesign and check.