6 Replies Latest reply on Oct 1, 2009 10:16 AM by Danny Noonan

    [JS] [CS4] Need help Scaling

    Danny Noonan

      I have a script built up to the point of selecting everything I want to scale down. I just can't figure out how to scale my selection down 50%. If at all possible, I want to scale the document size down 50% as well. As usual, thanks in advance... here's what I've got so far.


      function mySetup(){

      var myDocument = app.activeDocument;



      function mySnippet(){

      var myDocument = app.documents.item(0);

      var myPage = myDocument.pages.item(0);

      var myDocumentH = app.activeDocument.documentPreferences.pageHeight;

      var myDocumentW = app.activeDocument.documentPreferences.pageWidth;


      //Make a large rectangle to scale all objects to the center point of the page...

           var myRectangle = myPage.rectangles.add({geometricBounds:[-100, -100, myDocumentH+100, myDocumentW+100], fillColor:myDocument.swatches.item("Black"), fillTint:20});




      function myScale(){

      var myDocument = app.activeDocument;

      var mySelection = myDocument.select(SelectAll.ALL);

      //This is where I'm trying to scale all selected items-PLEASE HELP!




      function myTeardown(){


        • 1. Re: [JS] [CS4] Need help Scaling
          Peter Kahrel Adobe Community Professional & MVP

          We all need help with scaling (no joke!). You'll have to look under transformationMatrix. There used to be a tutorial around here in this forum as a sticky topic. Try searching the forum or try a google.



          • 2. Re: [JS] [CS4] Need help Scaling
            Danny Noonan Level 1

            I'm glad to here I'm not the only one. It's so damn frustrating.

            Thanks for the tip Peter, I'll give that a try.



            • 3. Re: [JS] [CS4] Need help Scaling
              Kasyan Servetsky Level 5

              I would use the following approach to scale everything on a page:


              var myDocument = app.activeDocument;
              app.transformPreferences.whenScaling = WhenScalingOptions.adjustScalingPercentage;
              var myPage = myDocument.pages.item(0);
              var myObjects = myPage.allPageItems;


              if (myObjects.length > 1) {
              var myGroup = myPage.groups.add(myObjects);
                  myScale(myGroup, AnchorPoint.centerAnchor, .5, .5);
                  alert("Scaled " + myObjects.length + " objects by 50 percent.");
              else if (myObjects.length == 1) {
                  myScale(myObjects[0], AnchorPoint.centerAnchor, .5, .5);
                  alert("Scaled 1 object by 50 percent.");
              else {
                  alert("There is nothing to scale.");
              function myScale(myObject, myAnchor, myXScale, myYScale){
                  var myTransformationMatrix =  app.transformationMatrices.add({horizontalScaleFactor:myXScale, verticalScaleFactor:myYScale});
                  myObject.transform(CoordinateSpaces.PARENT_COORDINATES, myAnchor, myTransformationMatrix);


              I think you don't need to select anything.



              1 person found this helpful
              • 4. Re: [JS] [CS4] Need help Scaling
                [Jongware] Most Valuable Participant

                This shows some information:


                matrixes = app.selection[0].transformValuesOf (CoordinateSpaces.SPREAD_COORDINATES);
                alert (matrixes[0].matrixMapping);

                -- you can see it's  3 x 4 matrix. That doesn't tell me anything at all but the individual properties of the transformationMatrix can be read as well:


                alert (matrixes[0].horizontalScaleFactor);


                These are read-only values (because they link to the other values), but you can use the supplied functions to manipulate them:


                matrixes = app.selection[0].transformValuesOf (CoordinateSpaces.SPREAD_COORDINATES);
                new_matrix = matrixes[0].scaleMatrix (0.5,0.5);
                app.selection[0].transform (CoordinateSpaces.SPREAD_COORDINATES, AnchorPoint.CENTER_ANCHOR, new_matrix, MatrixContent.SCALE_VALUES);

                (This creates a new matrix based on the old one, then 'applies' the scaling part to the original.)


                I wonder why it's an array. Perhaps you can have more than one matrix per object (not uncommon in 3D graphics).

                1 person found this helpful
                • 5. Re: [JS] [CS4] Need help Scaling
                  Olav Martin Kvern Level 3

                  Fellow Scripters,


                  There's a discussion of transformations in the "Working with Page Items" chapter of the Scripting Guide (and the associated scripts). It's not perfect, but it might help. resize and reframe are not covered, as they came late in the CS4 cycle. The chapter includes a number of "wrapper" functions (like Kasyan's "myScale" above--it might be one of them) that simplify the process of transforming objects.


                  I need to mention: page items created via scripting will almost always have an x and y transform applied to them, even if you have not moved the object since its creation. This is because they're created at the spread origin (which has nothing to do with the current ruler settings) and then moved into the position you specify. Even a rectangle created with page.rectangles.add()--which will be placed at 0,0 on the page--will have these transform values applied. Page items created by dragging drawing tools (how quaint!) will not have x and y transform values unless they've actually been moved from their original location.





                  • 6. Re: [JS] [CS4] Need help Scaling
                    Danny Noonan Level 1

                    Hello Scripters,

                    These responses to the question work, however, after the script has been run, it is putting all element on one layer. Is there a way to scale and keep all elements on the layer they were originally on?


                    Thanks for your help.