9 Replies Latest reply on Sep 15, 2014 12:46 PM by Laubender

    How to transform many selected objects all together, as a group, without grouping them?

    eutheneia

      Hello everybody!

       

      I have a selection containing all the page items contained in a certain layer:

       

      app.select(NothingEnum.nothing, undefined);

      for (var i = 0; i < doc.pageItems.length; i++) {

           if (doc.pageItems[i].itemLayer == doc.layers.item('MYLAYER')) {           doc.select(doc.pageItems[i], SelectionOptions.ADD_TO);

           }

      }

       

      How can i now work on the entire selection, for example flipping it horizontally, as if it were a group?

       

       

      Moreover, for "culture": where could I find officially this solution? I was not able to find any useful information nor on the official documentation, neither on the Net or on some book I buyed.

       

       

      Many thanks

       

      Roberto

        • 1. Re: How to transform many selected objects all together, as a group, without grouping them?
          TᴀW Adobe Community Professional & MVP

          Not sure it's possible.

           

          Why not group them first and then ungroup?

          • 2. Re: How to transform many selected objects all together, as a group, without grouping them?
            Laubender Adobe Community Professional & MVP

            @Roberto – have a look at the powerful transform() method and coordinate spaces.

            But be cautious! A selection of page items cannot span more than a single spread!

             

            Just one example for rotating all selected objects around a mutual x/y coordinate related to the global pasteboard coordinate space, that spans the whole document:

             

            //Example Rotate all selected objects around a mutual x/y coordinate
            //In this case the upper left corner of an added rectangle
            
            var myDoc = app.documents[0];
            var myLayoutWindow = app.documents[0].layoutWindows[0];
            var myRotationAngle = -30;
            var mutualPoint = [20,10];
            
            var myTempObject = myLayoutWindow.activeSpread.rectangles.add({fillColor:"Magenta"});
            myTempObject.move(mutualPoint);
                
            var myXYglobal = myTempObject.resolve(AnchorPoint.TOP_LEFT_ANCHOR,CoordinateSpaces.PASTEBOARD_COORDINATES);
            
            for(var n=0;n<app.selection.length;n++){
                
                app.selection[n].transform(
                    CoordinateSpaces.PASTEBOARD_COORDINATES,
                    myXYglobal,
                    app.transformationMatrices.add(
                        undefined, //horizontalScaleFactor
                        undefined, //verticalScaleFactor
                        undefined, //clockwiseShearAngle
                        myRotationAngle, //counterclockwiseRotationAngle
                        undefined, //horizontalTranslation
                        undefined, //verticalTranslation
                        undefined, //matrixValues
                        undefined, //matrixMapping
                        {} //withProperties
                        )
                    );
                    
                };
            
            //Optional:
            myTempObject.remove();
            

             

            Or flipping horizontally in regards to a mutual point (horizontalScaleFactor is set to -1):

             

            var myDoc = app.documents[0];
            var myLayoutWindow = app.documents[0].layoutWindows[0];
            var mutualPoint= [20,10]
            
            var myTempObject = myLayoutWindow.activeSpread.rectangles.add({fillColor:"Magenta"});
            myTempObject.move(mutualPoint);
                
            var myXYglobal = myTempObject.resolve(AnchorPoint.TOP_LEFT_ANCHOR,CoordinateSpaces.PASTEBOARD_COORDINATES);
            
            for(var n=0;n<app.selection.length;n++){
                
                app.selection[n].transform(
                    CoordinateSpaces.PASTEBOARD_COORDINATES,
                    myXYglobal,
                    app.transformationMatrices.add(
                    
                        -1, //horizontalScaleFactor
                        undefined, //verticalScaleFactor
                        undefined, //clockwiseShearAngle
                        undefined, //counterclockwiseRotationAngle
                        undefined, //horizontalTranslation
                        undefined, //verticalTranslation
                        undefined, //matrixValues
                        undefined, //matrixMapping
                        {} //withProperties
                        )
                    );
                    
                };
            
            //Optional:
            myTempObject.remove();
            

             

             

             

            Uwe

            • 4. Re: Re: How to transform many selected objects all together, as a group, without grouping them?
              eutheneia Level 1

              I follow your suggestion; in my naivety, I was simply thinking that grouping were not the right way for some presumed waste of performances.

               

              It works perfectly:

               

              app.select(NothingEnum.nothing, undefined);

              for (var i = 0; i < doc.pageItems.length; i++) {

                  if (doc.pageItems[i].itemLayer == doc.layers.item('MYLAYER')) {

                       doc.select(doc.pageItems[i], SelectionOptions.ADD_TO);

                  }

              }

              var myObjs = [];

              myObjs = app.selection;

              app.activeWindow.activePage.groups.add(myObjs);

              var myTransformationMatrix = app.transformationMatrices.add({horizontalScaleFactor:-1, verticalScaleFactor:1});

              app.activeWindow.activePage.groups[-1].transform(CoordinateSpaces.innerCoordinates,

                   AnchorPoint.CENTER_ANCHOR, myTransformationMatrix, undefined, false);

              app.activeWindow.activePage.groups[-1].ungroup();

               

              So, many thanks!

               

              Roberto

              • 5. Re: Re: How to transform many selected objects all together, as a group, without grouping them?
                eutheneia Level 1

                Many thanks to you, too; your suggestion are very useful!

                 

                Roberto

                • 6. Re: Re: Re: How to transform many selected objects all together, as a group, without grouping them?
                  Laubender Adobe Community Professional & MVP

                  @Roberto – did you try my code?
                  Grouping is not necessary at all.

                   

                  And that would be important, if you want to transform some objects together on different layers.

                   

                  And: Be aware, that your code will break, if there are some page items on layer "MYLAYER" on a different spread.

                   

                  //Will throw an error, in case that there are pageItems of "MYLAYER" on different spreads:
                  doc.select(doc.pageItems[i], SelectionOptions.ADD_TO);
                  

                   

                  Uwe

                  • 7. Re: Re: Re: How to transform many selected objects all together, as a group, without grouping them?
                    eutheneia Level 1

                    I confess I didn't understand well what you told me in your answer; now I realize that really the transformation can be applied to every object separately, even without selecting them.

                    I guessed if it was slower than selecting and grouping; instead, it is even faster!

                     

                    So, please accept my apologize.

                    I learned a lot with this workaround.

                     

                    Roberto

                    • 8. Re: Re: Re: Re: How to transform many selected objects all together, as a group, without grouping them?
                      Laubender Adobe Community Professional & MVP

                      @Roberto – speed (or the lack of it) should not bother us in this state of development…

                       

                      My snippets also have one "flaw" (or maybe something implicit you may not see at first glance), if you want to address pageItems on different spreads in one go. The coordinate system I am using is the global one for the whole document. And if we'd like to rotate all fetched pageItems around a mutual center, you'll propably expect different things to happen.

                       

                      Let's assume the following situation:

                      1. The active spread is spread with index 1, the one with the crosshairs

                      2. The script will access all pageItems on the next spread with index 2 (the two green rectangles)

                      3. The mutual point for the rotation is the center of the crosshair (guides) where the zero point is positioned

                       

                      BeforeScript.png

                       

                      The following code will rotate the two rectangles around the center of the crosshairs:

                       

                      var myDoc = app.documents[0]; 
                      var myLayoutWindow = app.documents[0].layoutWindows[0]; 
                      var myRotationAngle = -30; 
                      var mutualPoint = [0,0];
                      
                      var myTempObject = myLayoutWindow.activeSpread.rectangles.add({fillColor:"Magenta"}); 
                      myTempObject.move(mutualPoint); 
                           
                      var myXYglobal = myTempObject.resolve(AnchorPoint.TOP_LEFT_ANCHOR,CoordinateSpaces.PASTEBOARD_COORDINATES); 
                      
                      for(var n=0;n<myDoc.spreads[2].pageItems.length;n++){ 
                           
                          myDoc.spreads[2].pageItems[n].transform( 
                              CoordinateSpaces.PASTEBOARD_COORDINATES, 
                              myXYglobal, 
                              app.transformationMatrices.add( 
                                  undefined, //horizontalScaleFactor 
                                  undefined, //verticalScaleFactor 
                                  undefined, //clockwiseShearAngle 
                                  myRotationAngle, //counterclockwiseRotationAngle 
                                  undefined, //horizontalTranslation 
                                  undefined, //verticalTranslation 
                                  undefined, //matrixValues 
                                  undefined, //matrixMapping 
                                  {} //withProperties 
                                  ) 
                              ); 
                               
                          }; 
                      
                      //Optional: 
                      //myTempObject.remove();
                      

                       

                      Result after running the script:

                       

                      AfterScript.png

                       

                      (Remember, the active spread in this example is the spread with index 1: app.documents[0].spreads[1] ).

                      The left upper corner of the tiny red square at the crosshairs is the mutual rotation point. the script is rotating the two green rectangles by -30 degrees in respect to the coordinate space of the whole document. The gaps between the spreads do count as well forming this space.

                       

                      Uwe

                      • 9. Re: How to transform many selected objects all together, as a group, without grouping them?
                        Laubender Adobe Community Professional & MVP

                        Not only the gaps between the spreads; actually also the gaps between the single pasteboards are counting.

                        In this "well-formed" example (and every "normal" situation) the gaps between these pasteboards are 32 pt high.

                         

                        Uwe