5 Replies Latest reply on Jul 20, 2015 10:25 AM by elDudereno

    Resize top level groups

    elDudereno Level 3

      Running a for loop on all groups in a document. If group parent is a layer and layer is free then clipping mask and resize. Resize function fails as "Error 21 undefined is not an object" on the THIRD group in this example.

      Test_001.eps - Google Drive

       

      #target Illustrator

       

      var idoc = app.activeDocument;

      var gi = idoc.groupItems;

       

      function meetTheParents(PageItem){

           if (PageItem.parent.typename = "Layer"){

                return "Layer";

           }

      }

       

      function isLayerFree(layer){

           if (layer.locked == false && layer.visible == true){

                var gpa = layer.parent;

                isLayerFree(gpa);

                return true

                } else {

           return false

           }

      }

       

       

      for (j = gi.length -1; j >= 0; j--) {

           var iGroup = gi[j];

           if (iGroup.hidden==false && iGroup.locked==false) {

                if (meetTheParents(iGroup) == "Layer"){

                     if (isLayerFree(iGroup.parent)){

                          iGroup.selected = true;

                          app.executeMenuCommand ('makeMask');

                          app.executeMenuCommand ('deselectall');

                          iGroup.resize(50,50,true,true,true,true,50,Transformation=Transformation.CENTER);

                          // alert(iGroup.typename);

                     }

                }

           }

      }

      It fails AFTER it does the clipping mask so the for loop is functioning properly. It resizes properly the first TWO times so the resize function is written correctly. If I comment out the resize command and uncomment the alert of iGroup's type name it completes successfully so the identity of the group in question is not an issue.

       

      ?!?!?!?!?!?

        • 1. Re: Resize top level groups
          elDudereno Level 3

          Interesting note: I was testing each of the elements of the resize function.

          pageItem.resize (scaleX, scaleY, changePositions, changeFillPatterns, changeFillGradients, changeStrokePattern, changeLineWidths, scaleAbout)

          I haven't messed with the scaleX, scaleY but I tested each of the following elements. In my original script they were all true, so I changed them to false one at a time.

          iGroup.resize(50,50,false,true,true,true,50,Transformation=Transformation.CENTER);// did not resize stopped clipping after third group

          iGroup.resize(50,50,true,false,true,true,50,Transformation=Transformation.CENTER);// resize first two clipped third and failed

          iGroup.resize(50,50,true,true,false,true,50,Transformation=Transformation.CENTER);// ditto

          iGroup.resize(50,50,true,true,true,false,50,Transformation=Transformation.CENTER);// ditto

          iGroup.resize(50,50,true,true,true,true,true,Transformation=Transformation.CENTER);// Changed 50 to 'true'. result ditto

           

          iGroup.resize(50,50,true,true,true,true,true,Transformation=Transformation.TOPLEFT); // kept the changeStrokePattern as 'true' and changed the transformation point and got this result:

          Before:

          Screen shot 2015-07-20 at 10.12.51 AM.png

          After:

          Screen shot 2015-07-20 at 10.13.00 AM.png

          The first group appears to have followed the instructions from the script, changing from top left, but the second group changed from the center.

          I checked and I get the same result if I revert to 50 for changeStrokePattern.

          iGroup.resize(50,50,true,true,true,true,50,Transformation=Transformation.TOPLEFT);

           

          Same result if I comment out the clipping mask function, except it's not clipped obviously.

           

          Are things changing INSIDE of the resize function as it runs? If so why?

          • 2. Re: Resize top level groups
            o-marat Level 3

            There you do not have an error in the function meetTheParents()?

            Must be PageItem.parent.typename == "Layer"?

            • 3. Re: Resize top level groups
              elDudereno Level 3

              Good catch, thank you, but it doesn't change the result. After correcting that oversight the result is the same.

              • 4. Re: Resize top level groups
                o-marat Level 3

                And this is working?

                 

                (function () {

                  var groups = activeDocument.groupItems,

                      iGroup,

                      j;

                 

                  for ( j = 0; j < groups.length; j++ ) {

                    iGroup = groups[ j ];

                    if ( iGroup.hidden == false && iGroup.locked == false ) {

                      if ( meetTheParents ( iGroup ) == "Layer" ) {

                        if ( isLayerFree ( iGroup.parent ) ) {

                          iGroup.selected = true;

                          app.executeMenuCommand ( 'makeMask' );

                          app.executeMenuCommand ( 'deselectall' );

                          iGroup.resize ( 50, 50, true, true, true, true, 50, Transformation.CENTER );

                        }

                      }

                    }

                  }

                 

                  function meetTheParents ( PageItem ) {

                    if ( PageItem.parent.typename = "Layer" ) {

                      return "Layer";

                    }

                  }

                 

                  function isLayerFree ( layer ) {

                    if ( layer.locked == false && layer.visible == true ) {

                      var gpa = layer.parent;

                      isLayerFree ( gpa );

                      return true;

                    } else {

                      return false;

                    }

                  }

                } ())

                • 5. Re: Resize top level groups
                  elDudereno Level 3

                  So the error was:

                  iGroup.resize(50,50,true,true,true,true,50,Transformation=Transformation.CENTER);

                   

                  The "Transformation=" was unnecessary and screwing things up after a few iterations.

                   

                  The correct syntax is: iGroup.resize( 50, 50, true, true, true, true, 50, Transformation.CENTER);