14 Replies Latest reply on Oct 24, 2016 10:23 AM by charur94718007

    Create groups in Indesign

    charur94718007 Level 1

      Screen Shot 2016-10-20 at 2.18.45 PM.pngHello All, Is there any script to create n number of  groups in a document with a gap of 20 pts. First group must start with top corner of the page and second will align next to first with gap of 20 pts. And so on.. But when it touches the right corner of the documents then next group must be in another row. I have attached the sample image what is the requirement. I want to calculate number of rows and columns required to place n number of groups with a gap of 20 pts.

        • 1. Re: Create groups in Indesign
          tpk1982 Level 4

          We can do by script.. but did you tried with step and repeat method?

           

          Screen Shot 2016-10-20 at 5.02.35 PM.png

          • 2. Re: Create groups in Indesign
            Obi-wan Kenobi Adobe Community Professional

            Hi Kartik,

             

            Not with this feature because it can't calculate the width/height of the frame!

             

            see: Create rectangles in Indesign

             

            (^/)

            • 3. Re: Create groups in Indesign
              tpk1982 Level 4

              Thanks Obi.. I thought OP will create rectangle with his desired values..

              • 4. Re: Create groups in Indesign
                Obi-wan Kenobi Adobe Community Professional

                If, you're right! If not, not!  Aha! 

                 

                See another cool way on other topic!

                 

                (^/)

                • 5. Re: Create groups in Indesign
                  charur94718007 Level 1

                  No, I have not tried. I need a script. This is small part of my Indesign plugin. So definitely need to do by script

                  • 6. Re: Create groups in Indesign
                    tpk1982 Level 4

                    Please get the Obi mentioned script from the below path

                     

                    Novelty/Frame_Divider.jsx at master · GitBruno/Novelty · GitHub

                    • 7. Re: Create groups in Indesign
                      charur94718007 Level 1

                      Is this necessary to give number of rows and columns in adavance???

                      • 8. Re: Create groups in Indesign
                        S Hopkins Adobe Community Professional

                        Yep,

                        You either need to know the size of each element (width and height) or number of rows and columns.

                        Shirley Hopkins

                        • 9. Re: Create groups in Indesign
                          charur94718007 Level 1

                          Yes I know width and height of the element. But not know in advance number of rows and columns. I want a formula to calculate number of rows and columns required based on width and height of the element and considering each element should be 20pts. apart.

                          • 10. Re: Create groups in Indesign
                            Laubender Adobe Community Professional & MVP

                            See the DOM documentation for Page:

                            Adobe InDesign CS6 (8.0) Object Model JS: Page

                             

                            Property bounds

                            is giving you the size of the page

                             

                            If the page is not resized with the Page Tool maybe its easier to check for:

                            Adobe InDesign CS6 (8.0) Object Model JS: DocumentPreference

                             

                            Properties:

                            pageHeight

                            pageOrientation

                            pageWidth

                             

                            And  corresponding:

                            Adobe InDesign CS6 (8.0) Object Model JS: MarginPreference

                             

                            The properties of

                            page.marginPreferences
                            bottom

                            right

                            left

                            top

                             

                            That will give you the base for your calculations of the width and the height of the available area.

                             

                            Regards,
                            Uwe

                            • 11. Re: Create groups in Indesign
                              S Hopkins Adobe Community Professional

                              Hi,

                              Think of it this way: you will have one more element (page item) than you will have gutters (space between horizontally). This also applies for elements vertically (one more element than gaps (space between vertically). Thus you can subtract the width of 1 element from the width of the page and then divide that by the width of the element plus that of the space between).

                              Here is the calculation written in AppleScript and JavaScript (have your script calculate the values for the arbitrary values below and you have it):

                              AppleScript

                              set pwid to 400 --page width

                              set phgt to 800 --page height

                              set iwid to 30 -- item width

                              set ihgt to 40 -- item height

                              set gutter to 20 -- space between horizontally

                              set gap to 20 -- space between vertically

                              set numColumns to round ((pwid - iwid) / (iwid + gutter)) rounding down

                              set numRows to round ((phgt - ihgt) / (ihgt + gap)) rounding down

                              {numColumns, numRows}

                               

                              JavaScript

                              var pwid = 400; //page width

                              var phgt = 800; //page height

                              var iwid = 30; //item width

                              var ihgt = 40; //item height

                              var gutter = 20; //space between horizontally

                              var gap = 20; //space between vertically

                              var numColumns = Math.floor((pwid - iwid)/(iwid + gutter));

                              var numRows = Math.floor((phgt - ihgt)/(ihgt + gap));

                              [numColumns, numRows];

                              • 12. Re: Create groups in Indesign
                                charur94718007 Level 1

                                Thanks SHopkins.. This is right and thanks a lot for helping me to calculate rows and columns. One also written one script for the same. I am sharing with you. I need to handle the case if page is finished. Here is the script. You can see and let me know your suggestions as well about this. Before running the script please ensure that you one selected item.

                                 

                                var firstPage = app.documents[0].pages;

                                var margin = firstPage[0].marginPreferences.top;

                                var pgWidth = firstPage[0].bounds[3] - firstPage[0].bounds[1] - (margin * 2);

                                var pgheight = firstPage[0].bounds[2] - firstPage[0].bounds[0] - (margin * 2);

                                var pgX1 = firstPage[0].bounds[1] + margin;

                                var pgY1 = firstPage[0].bounds[0] + margin;

                                var objectWidth = app.selection[0].geometricBounds[3] - app.selection[0].geometricBounds[1];

                                var objectHeight = app.selection[0].geometricBounds[2] - app.selection[0].geometricBounds[0];

                                app.selection[0].move([pgX1, pgX1]);

                                var gutter = 20;

                                var gap = 20;

                                var row = 1;

                                var col = 1;

                                for (var i = 1; i < 40; i++) {

                                  var lastObject = app.selection[0];

                                  var mY1 = lastObject.geometricBounds[0];

                                  var mX1 = lastObject.geometricBounds[3] + gutter;

                                  var newObject = app.selection[0].duplicate();

                                  app.selection[0] = null;

                                  app.selection = newObject;

                                  newObject.move([mX1, mY1]);

                                  if (newObject.geometricBounds[3] > (firstPage[0].bounds[3] - margin)) {

                                    newObject.move([pgX1, (objectHeight * row) + (gap * row) + margin]);

                                    row++;

                                  }

                                  if (newObject.geometricBounds[0] + objectHeight > pgheight) {

                                    var newPage = app.documents[0].pages.add(LocationOptions.AT_END);

                                    var margin = newPage.marginPreferences.top;

                                    pgWidth = newPage.bounds[3] - newPage.bounds[1] - (margin * 2);

                                    pgheight = newPage.bounds[2] - newPage.bounds[0] - (margin * 2);

                                    pgX1 = newPage.bounds[1] + margin;

                                    pgY1 = newPage.bounds[0] + margin;

                                    app.selection[0] = null;

                                    app.selection = newObject;

                                    newObject.move(newPage);

                                    newObject.move([pgX1, pgY1]);

                                    row = 1;

                                  }

                                }

                                • 13. Re: Create groups in Indesign
                                  S Hopkins Adobe Community Professional

                                  I see a number of potential problems with the code above: you are assuming that margins are the same hich may not be the case. Also, necessity for lots of if/then tests. The code below uses the calculation for number rows and columns to handle the duplication. Also, it's a good example of nested for loops. It does not add pages, but you can do that.  Try it.

                                   

                                   

                                  var sel = app.selection;

                                  if (sel.length == 0) {

                                  alert ("Requires page item selection");

                                  }

                                  var gutter = 20;

                                  var gap = 20;

                                  app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS;

                                  var selObj = sel[0];

                                  var pageRef = selObj.parentPage;

                                  var pageBounds = pageRef.bounds;

                                  var mPref = pageRef.marginPreferences;

                                  var pMargins = [mPref.top, mPref.left, mPref.bottom, mPref.right];

                                  pWid = (pageBounds[3]-pageBounds[1]) - (pMargins[1] + pMargins[3]);

                                  pHgt = (pageBounds[2]-pageBounds[0]) - (pMargins[0] + pMargins[2]);

                                  var objBounds = selObj.geometricBounds;

                                  var objWid = objBounds[3] - objBounds[1];

                                  var objHgt = objBounds[2] - objBounds[0];

                                  var numColumns = Math.floor((pWid - objWid) / (objWid + gutter));

                                  var numRows = Math.floor((pHgt - objHgt) / (objHgt + gap));

                                  var moveX = (objWid + gutter);

                                  var moveY = (objHgt + gap);

                                  var y0 = pMargins[0];

                                  var x0 = pMargins[1];

                                  for (var j = 0; j <= numRows; j++) {

                                     for (var i = 0; i <= numColumns; i++) {

                                         var dupObj = selObj.duplicate();

                                         dupObj.move([x0, y0]);

                                         x0 = x0 + moveX;

                                     }

                                  x0 = pMargins[1];

                                  y0 = y0 + moveY;

                                  }

                                  selObj.remove();

                                  • 14. Re: Create groups in Indesign
                                    charur94718007 Level 1

                                    Thanks S Hopkins, for adding more check. I am glad that you correct my script. Thanks a alot.. :) :)