6 Replies Latest reply on Nov 15, 2016 5:55 AM by Obi-wan Kenobi

    Code to be evaluated! [007] // Resizing and distributing ! …

    Obi-wan Kenobi Adobe Community Professional

      Hi Scripters,

       

      I would like to apply the same width/height to a selection of frames and distribute them in the original area!

      To be a little clearer:

      I select the four frames (above) and I would like to have the result below!

       

      Capture d’écran 2016-11-14 à 23.38.57.png

       

      … I get this:

       

      Capture d’écran 2016-11-15 à 00.12.46.png

       

      Yes!  It seems to be good! Aha! … but not really!! I've an error message!

       

      Capture d’écran 2016-11-15 à 00.23.13.png

       

      The code I've written is:

       

      var myDoc = app.activeDocument;
      var mySel = app.selection;  
      var myGroup = myDoc.groups.add(mySel);   
      var myGB = myGroup.geometricBounds;
      myGroup.ungroup();
      var myNewWidth = UnitValue('25mm').as('pt');   
      var myNewHeight = UnitValue('10mm').as('pt');
      for (i = 0; i < mySel.length; i++) {
            if (mySel[i].geometricBounds[1] == myGB[1])  mySel[i].resize(CoordinateSpaces.INNER_COORDINATES, AnchorPoint.TOP_LEFT_ANCHOR, ResizeMethods.REPLACING_CURRENT_DIMENSIONS_WITH, [myNewWidth, myNewHeight]);
            if (mySel[i].geometricBounds[3] == myGB[3])  mySel[i].resize(CoordinateSpaces.INNER_COORDINATES, AnchorPoint.TOP_RIGHT_ANCHOR, ResizeMethods.REPLACING_CURRENT_DIMENSIONS_WITH, [myNewWidth, myNewHeight]);
            mySel[i].resize(CoordinateSpaces.INNER_COORDINATES, AnchorPoint.CENTER_ANCHOR, ResizeMethods.REPLACING_CURRENT_DIMENSIONS_WITH, [myNewWidth, myNewHeight]);
      }
      myDoc.distribute(mySel, DistributeOptions.HORIZONTAL_SPACE, AlignDistributeBounds.ITEM_BOUNDS);
      

       

      Thanks in advance! 

       

      (^/)

        • 1. Re: Code to be evaluated! [007] // Resizing and distributing ! …
          Loic.Aigon Adobe Community Professional

          The problem doesn't seem to be linked to your code specifically but from the Rorohiko API implementation which weirdly you doesn't seem to call here.

           

          Do you include it somewhere in the code ?

           

          Plus, if you really want to be effective and error free, you should secure your code by avoiding reaching objects that may not be avalaile at some point.

           

          var myDoc = app.activeDocument; 
          
          That relies on a existing document but you may run your script when there aren't any.
          
          So you may prefer
          
          function main() {
               var doc;
               if ( app.documents.length) return;
               //Now you can go safe
               doc = app.activeDocument;
          
               //do something with doc
          }
          
          

           

          HTH

          • 2. Re: Code to be evaluated! [007] // Resizing and distributing ! …
            Obi-wan Kenobi Adobe Community Professional

            Hi Loic,

             

            No real reason to run this script without open file! 

            … but you're right: no reason to run it without a selection!! … So [including an "undo!"]:

             

             

            app.doScript("main()", ScriptLanguage.javascript, undefined, UndoModes.FAST_ENTIRE_SCRIPT, "Resize and Distribute! …");
            
            
            function main()
            {
                
                var myDoc = app.activeDocument;
                var mySel = app.selection;
            
            
                if (mySel.length == 0)  exit();
                else {
                    var myGroup = myDoc.groups.add(mySel);   
                    var myGB = myGroup.geometricBounds;
                    myGroup.ungroup();
                    var myNewWidth = UnitValue('25mm').as('pt');   
                    var myNewHeight = UnitValue('10mm').as('pt');
                    
                    for (i = 0; i < mySel.length; i++) {
                        if (mySel[i].geometricBounds[1] == myGB[1])  mySel[i].resize(CoordinateSpaces.INNER_COORDINATES, AnchorPoint.TOP_LEFT_ANCHOR, ResizeMethods.REPLACING_CURRENT_DIMENSIONS_WITH, [myNewWidth, myNewHeight]);
                        if (mySel[i].geometricBounds[3] == myGB[3])  mySel[i].resize(CoordinateSpaces.INNER_COORDINATES, AnchorPoint.TOP_RIGHT_ANCHOR, ResizeMethods.REPLACING_CURRENT_DIMENSIONS_WITH, [myNewWidth, myNewHeight]);
                        mySel[i].resize(CoordinateSpaces.INNER_COORDINATES, AnchorPoint.CENTER_ANCHOR, ResizeMethods.REPLACING_CURRENT_DIMENSIONS_WITH, [myNewWidth, myNewHeight]);
                    }
                    myDoc.distribute(mySel, DistributeOptions.HORIZONTAL_SPACE, AlignDistributeBounds.ITEM_BOUNDS);
                }
            }
            

             

             

            However, I've the same error message! 

             

            (^/)

            • 3. Re: Code to be evaluated! [007] // Resizing and distributing ! …
              Jump_Over Level 5

              @@Hi,

               

              Notice an error message:

              Source: if(theNewLabel == "subscribe") ....

               

              I cant see this line in provided code so make it clear first.

               

              Jarek

              • 4. Re: Code to be evaluated! [007] // Resizing and distributing ! …
                Obi-wan Kenobi Adobe Community Professional

                Hi Jarek and Loic!

                 

                I've just discussed with Kris from Rorohiko!

                The problem came from "PlaceAutoSynchronizer" plugin installed in the ID "Plug-Ins" folder, sub-folder "Rorohiko".

                Kris told me to remove this plugin and the problem is fixed now!

                 

                Cool! No more error now! … That doesn't mean my script is … perfect! 

                But I keep learning! …

                 

                (^/)

                • 5. Re: Code to be evaluated! [007] // Resizing and distributing ! …
                  Loic.Aigon Adobe Community Professional
                  No real reason to run this script without open file!

                  Ha ha ha . Well you may not work much with end users

                  I mean it's better to take good habits

                  • 6. Re: Code to be evaluated! [007] // Resizing and distributing ! …
                    Obi-wan Kenobi Adobe Community Professional

                    Well! I know you're right! I read, especially here, so "nice" comments of users who really don't know how to use ID!

                     

                    … and not only newbies! …    So, I need to have good habits before they apply on a "pretty" button and trigger World War III by inadvertence! Knowledge give us Responsibility, as Uwe Laubender said once in another topic!

                     

                    (^/)