12 Replies Latest reply on Dec 27, 2016 5:58 AM by Trevorׅ

    Resize polygon  based on text frame height

    keyank99503373

      Hi All

       

      I need to resize the polycon box based on the text frame height. How to do this with indesign script

       

      Here I have copied the screen short

       

      Capture.GIF

        • 1. Re: Resize polygon  based on text frame height
          Loic.Aigon Adobe Community Professional

          Well you need to read at the visibleBounds property of the textframe (which you can access through the applied XML Element), then relocate the bottom points of the polygon accordingly. Question here would be to access this polygon easily (is this part of a group with the text frame ?)

          • 2. Re: Resize polygon  based on text frame height
            keyank99503373 Level 1

            I am using this method to find the polycon inside the group

            var myDoc = app.activeDocument;

            var myGroups= myDoc.groups

                          

                      var pItems = myGroups[0].pageItems;               

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

                            {

                                var curItem = pItems[j].getElements()[0]; 

                                var constrName = curItem.constructor.name;            

                                if (constrName == "Polygon")

                                    {      

                                            app.toolBoxTools.currentTool = UITools.DIRECT_SELECTION_TOOL;   // to select the Polygon

                                            app.select(curItem)

                                    

                                     }

             

                                }

            • 3. Re: Resize polygon  based on text frame height
              Loic.Aigon Adobe Community Professional

              if you can presume the polygon is a direct child of the group then no need for looping

               

              myGroups[0].polygons[0] //assuming it's the first polygon of the group you are interested in…

               

              etc.

              • 4. Re: Resize polygon  based on text frame height
                Laubender Adobe Community Professional & MVP

                Hi together,

                note, that there is one speciality with this situation.

                 

                Do not do this:

                 

                1. Change size by changing the geometricBounds

                2. Change size by scaling

                 

                Instead move some of the pathpoints of the polygon:

                 

                MovePathPoints-instead-scaling-or-changingGeometricBounds.png

                 

                It could also be that the polygon is constructed with more than 6 path points.

                 

                 

                 

                polygon-with-12-path-points.png

                Or even more…

                And then you are a bit into trouble. ;-)

                 

                Regards,
                Uwe

                 

                // EDITED

                • 5. Re: Resize polygon  based on text frame height
                  keyank99503373 Level 1

                  How can I do this please advice. I am very little bit knowledge in scripting

                  • 6. Re: Resize polygon  based on text frame height
                    Trevorׅ Adobe Community Professional

                    Given that you select the polygon and the rectangle you could use.

                    So you just need to adapt this to your preferred method of identifying the polygon and rectangle.

                    Using select is really bad practice.

                     

                    HTH

                     

                    Trevor

                     

                    // By Trevor www.creative-scripts.com
                    var padding, paddingFromCurve, paddingRefencePoint, shapes, path, polygonEntirePath, l, c, pointMap, p, rectangleLow, polygonAdjustment;
                    
                    if (!app.properties.activeDocument) exit();
                    
                    padding = toDocUnit('3mm'); // change as needed
                    paddingFromCurve = true; // change as needed
                    
                    function toDocUnit(n) {
                        return UnitValue(n).as(app.scriptPreferences.measurementUnit === AutoEnum.AUTO_VALUE ? app.activeDocument.viewPreferences.verticalMeasurementUnits : app.scriptPreferences.measurementUnit) || +n || 0;
                    }
                    
                    function getShapes() {
                        var thePolygon, theRectangle, selection;
                        // Check you've selected the 2 shapes
                        selection = app.selection;
                        if (!selection ||
                            selection.length !== 2 ||
                            !('paths' in selection[0]) ||
                            !('paths' in selection[1]) ||
                            selection[0].paths.length !== 1 ||
                            selection[1].paths.length !== 1
                        ) {
                            alert('You need to select the rectangle and the polygon.');
                            exit();
                        }
                    
                        ///////////////////////////////////////////////////////////////////////
                        // Identify which of the selected items is the frame                 //
                        // This test could fail in a bunch of cases but it most likely won't //
                        // So unless we hear differently                                     //
                        ///////////////////////////////////////////////////////////////////////
                    
                        if (selection[0].paths[0].pathPoints.length === 4) {
                            theRectangle = selection[0];
                        } else if (selection[1].paths[0].pathPoints.length === 4) {
                            theRectangle = selection[1];
                        }
                    
                        /////////////////////////////////////////////////////////
                        // Identify which of the selected items is the polygon //
                        /////////////////////////////////////////////////////////
                    
                        if (selection[0].paths[0].pathPoints.length > 4) {
                            thePolygon = selection[0];
                        } else if (selection[1].paths[0].pathPoints.length > 4) {
                            thePolygon = selection[1];
                        }
                    
                        ////////////////////////////////////////////////////////////////////////
                        // All going well we should have identified the frame and the polygon //
                        // But we'll check to be sure                                         //
                        ////////////////////////////////////////////////////////////////////////
                    
                        if (!theRectangle || !thePolygon) {
                            alert('You need to select the rectangle and the polygon.');
                            exit();
                        }
                    
                        return {
                            rectangle: theRectangle,
                            polygon: thePolygon
                        };
                    }
                    
                    shapes = getShapes();
                    
                    //////////////////////////////////////////////////////////
                    // find the bottom four points of the polygon           //
                    // Again we're use the simplest method                  //
                    // it will work in most cases let us know if it doesn't //
                    //////////////////////////////////////////////////////////
                    
                    path = shapes.polygon.paths[0];
                    polygonEntirePath = path.entirePath;
                    l = polygonEntirePath.length;
                    pointMap = [];
                    for (c = 0; c < l; c++) {
                        pointMap.push([c, polygonEntirePath[c][2] ? polygonEntirePath[c][1][1] : polygonEntirePath[c][1]]); // the index and the vertical anchor point
                    }
                    
                    /////////////////////
                    // sort the points //
                    /////////////////////
                    
                    pointMap.sort(function(a, b) {
                        return b[1] > a[1];
                    });
                    
                    // Get the lowest 4 points of the polygon
                    pointMap = pointMap.splice(0, 4);
                    
                    ////////////////////////////////
                    // get low point of rectangle //
                    ////////////////////////////////
                    
                    rectangleLow = shapes.rectangle.geometricBounds[2];
                    paddingRefencePoint =  paddingFromCurve ? 3 : 0;
                    polygonAdjustment = rectangleLow - (pointMap[paddingRefencePoint][2] ? pointMap[paddingRefencePoint][1][1] : pointMap[paddingRefencePoint][1]) + padding;
                    
                    l = pointMap.length;
                    while (l--) {
                        p = pointMap[l][0];
                        if (polygonEntirePath[p][2]) {
                            polygonEntirePath[p][0][1] += polygonAdjustment;
                            polygonEntirePath[p][1][1] += polygonAdjustment;
                            polygonEntirePath[p][2][1] += polygonAdjustment;
                        } else {
                            polygonEntirePath[p][1] += polygonAdjustment;
                        }
                    }
                    
                    shapes.polygon.paths[0].entirePath = polygonEntirePath;
                    
                    
                    • 7. Re: Resize polygon  based on text frame height
                      Trevorׅ Adobe Community Professional

                      Now given your screenshot and that you are working with a group.

                      Then you could try the beneath.

                       

                      This presumes your group[s] are of a fixed structure.

                      You need to change the indexes in line 14 to make sure you get correct item being adjusted according to the correct item.

                       

                      shapes = {rectangle: myGroup.pageItems[3], polygon: myGroup.pageItems[0]};

                       

                      If you were to apply this to a several groups you would want to cache the polygons low point indexes.

                       

                      Before:

                      2016-12-26_15-09-43.png

                       

                      After

                       

                      2016-12-26_15-09-55.png

                       

                       

                      // jshint undef: true, unused: true

                      var padding, paddingFromCurve, paddingRefencePoint, shapes, path, polygonEntirePath, l, c, pointMap, p, rectangleLow, polygonAdjustment;

                      var myGroup, doc;

                       

                      doc = app.properties.activeDocument;

                      if (!doc) exit();

                       

                      padding = toDocUnit('3mm');

                      paddingFromCurve = true;

                       

                       

                       

                      myGroup = doc.groups[0];

                      shapes = {rectangle: myGroup.pageItems[3], polygon: myGroup.pageItems[0]};

                       

                      function toDocUnit(n) {

                          return UnitValue(n).as(app.scriptPreferences.measurementUnit === AutoEnum.AUTO_VALUE ? doc.viewPreferences.verticalMeasurementUnits : app.scriptPreferences.measurementUnit) || +n || 0;

                      }

                       

                       

                      //////////////////////////////////////////////////////////

                      // find the bottom four points of the polygon          //

                      // Again we're use the simplest method                  //

                      // it will work in most cases let us know if it doesn't //

                      //////////////////////////////////////////////////////////

                       

                      path = shapes.polygon.paths[0];

                      polygonEntirePath = path.entirePath;

                      l = polygonEntirePath.length;

                      pointMap = [];

                      for (c = 0; c < l; c++) {

                          pointMap.push([c, polygonEntirePath[c][2] ? polygonEntirePath[c][1][1] : polygonEntirePath[c][1]]); // the index and the vertical anchor point

                      }

                       

                      /////////////////////

                      // sort the points //

                      /////////////////////

                       

                      pointMap.sort(function(a, b) {

                          return b[1] > a[1];

                      });

                       

                      // Get the lowest 4 points of the polygon

                      pointMap = pointMap.splice(0, 4);

                       

                      ////////////////////////////////

                      // get low point of rectangle //

                      ////////////////////////////////

                       

                      rectangleLow = shapes.rectangle.geometricBounds[2];

                      paddingRefencePoint =  paddingFromCurve ? 3 : 0;

                      polygonAdjustment = rectangleLow - (pointMap[paddingRefencePoint][2] ? pointMap[paddingRefencePoint][1][1] : pointMap[paddingRefencePoint][1]) + padding;

                       

                      l = pointMap.length;

                      while (l--) {

                          p = pointMap[l][0];

                          if (polygonEntirePath[p][2]) {

                              polygonEntirePath[p][0][1] += polygonAdjustment;

                              polygonEntirePath[p][1][1] += polygonAdjustment;

                              polygonEntirePath[p][2][1] += polygonAdjustment;

                          } else {

                              polygonEntirePath[p][1] += polygonAdjustment;

                          }

                      }

                       

                      shapes.polygon.paths[0].entirePath = polygonEntirePath;

                       

                      Edit: Jive's is playing up, hence the lack of formatting

                      • 8. Re: Resize polygon  based on text frame height
                        Laubender Adobe Community Professional & MVP

                        keyank99503373 wrote:

                         

                        How can I do this please advice. I am very little bit knowledge in scripting

                        Hi,

                        before doing anything you have to analyze the polygon you want to change.

                        Some important questions:

                         

                        1. Are rounded corners applied with InDesign's corner options?

                        2. How many path points are at the very bottom of the polygon?

                        ( Select the polygon with the Direct Selection Tool and count.)

                         

                        If 1 is true, there is a good chance, that only 2 path points should be moved.

                        If not, you have to adapt pointMap.splice() in Trevor's snippet.

                         

                        Some other questions:

                        How many groups like the one in your screenshot are in your document?
                        Is every polygon you want to change constructed the same way?

                        ( Number of path points, corner options applied or not. )

                         

                        What should be the distance from the baseline of the last line of text to the bottom edge of the polygon?

                        ( I guess this is what we are after and not the distance of the bottom edge of the text frame to the bottom edge of the polygon.)

                        Regards,
                        Uwe

                        • 9. Re: Resize polygon  based on text frame height
                          keyank99503373 Level 1

                          Thanks you so much trevor. Its working for me. I have only one polygon inside the group.

                           

                          But i am reading each and every lines in your script. If i have any doubts i will ask you.

                           

                          I very glad to part of this forum.

                          • 10. Re: Resize polygon  based on text frame height
                            Trevorׅ Adobe Community Professional

                            Hi Keyank

                             

                            With all due respect to Loic in #1 above who is an excellent professional scripter, I think my answer at #6 or #7 should be marked as correct.

                            So please with Loic's approval unmark his answer as correct and mark mine as correct.

                             

                            Thanks