1 Reply Latest reply on Jan 17, 2017 4:22 PM by Jump_Over

    How to loop through a selection and apply a script

    Kiwi_5000

      Hi there,

       

      I'm having real trouble trying to get the below script to work on more than one selected object. At the moment it works perfect with one frame selected with an image in it. What it does is it scales it down to 10% and centres it in a frame. I've tried different script samples I've found online but I can't seem to get them to work with the below. Can anyone help show me what I'm failing to do.

       

      Below is the working script (1 selected)

       

      function fit10(){

      var sel = app.selection[0]; 

          sel.fit(FitOptions.centerContent); 

      var myImage = sel;

      myImage.graphics[0].absoluteHorizontalScale = 10;

      myImage.graphics[0].absoluteVerticalScale = 10;

          }

       

      ____________________

       

      Below is the script that is not working (more than 1 selected)

       

      function fit10(){

      var sel = app.selection[i]; 

          sel.fit(FitOptions.centerContent); 

          for (var i = 0; sel > i; i++)

      var myImage = sel;

      myImage.graphics[0].absoluteHorizontalScale = 10;

      myImage.graphics[0].absoluteVerticalScale = 10;

          }

       

      Thank you

        • 1. Re: How to loop through a selection and apply a script
          Jump_Over Level 5

          Hi,

           

          Let me induce you to study javascript since it will make your work easier and faster.

          Copying and pasting unreadable codes from network can lead you to wrong or even danger places.

           

          • A selection - if made - is an array of objects.
          • Your function fit10() should be called for each element of this array.
          • So all you need is to create a loop.
          • Checking if:

          1. selection is composed from graphic' containers

          and

          2. these containers are not empty

          makes a code more universal.

           

          Exam this:

          var 
            mTarget = app.selection,
            cObject;
          while (cObject = mTarget.pop()) 
            fit10(cObject);
          function fit10(anObject){
            if (!anObject.hasOwnProperty ("graphics") ) return;
            if (!anObject.graphics.length) return;
            anObject.graphics[0].absoluteHorizontalScale = 10;
            anObject.graphics[0].absoluteVerticalScale = 10;
            anObject.fit(FitOptions.centerContent); 
            }
          

           

          Jarek