4 Replies Latest reply on Oct 11, 2011 7:33 AM by John Hawkinson

    JS CS5.5-Moving Selected Objects-Please Help!!

    Danny Noonan Level 1

      Hello,

      I have a scritpt that will apply a certain object style to the selected items in the document. I am trying to add a "move" to this scirpt. I would like every item selected in the document to move down .125 inches. I know this is much simpler than I think it is, I've always had trouble with bounds in scripts. If anyone could please help me with this, I'd greatly appreciate it.

       

      Thanks for your time,

       

      Danny

        • 1. Re: JS CS5.5-Moving Selected Objects-Please Help!!
          Danny Noonan Level 1

          Never mind, it is much easier than I was making it.

           

          for (i = 0; i < app.selection.length; i++)

          app.selection[i].move(undefined, [0, +.125]);

          • 2. Re: JS CS5.5-Moving Selected Objects-Please Help!!
            John Hawkinson Level 5

            Note that this script depends on the units being set to inches -- if they are not, then you will need to change them, otherwise the items will move by 0.125 {something}s (e.g. picas, points, centimeters, etc.).

            • 3. Re: JS CS5.5-Moving Selected Objects-Please Help!!
              csm_phil Level 4

              Hi Danny,

               

              Use this JS code is change the preferences to inches value.

               

               

              var myDoc = app.activeDocument;
              myDoc.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.inches;
              myDoc.viewPreferences.verticalMeasurementUnits = MeasurementUnits.inches; 
              
              for (i = 0; i < app.selection.length; i++){
                  app.selection[i].move(undefined, [0, +.125]);
                  }
              app.selection= null;
              

               

               

              thx

              csm_phil

              • 4. Re: JS CS5.5-Moving Selected Objects-Please Help!!
                John Hawkinson Level 5

                I suppose if I was going to be picky, I should have provided my code initially, rather than waiting for csm_phil and then posting something else. Anyhow, I think it is better to use:

                 

                var
                    i,
                  savedUnits = app.scriptPreferences.measurementUnit;
                try {
                  app.scriptPreferences.measurementUnit =
                      MeasurementUnits.INCHES;
                
                  for (i = 0; i < app.selection.length; i++){
                      app.selection[i].move(undefined, [0, +.125]);
                  }
                } finally {
                  app.scriptPreference.measurementUnit = savedUnits;
                }
                

                This changes the scriptPreferences, not the viewPreferences, which means that even if you screw up and don't set it back, it does not affect the user. It also means it is one variable to change and not two. We take care to save the units and set them back, so other scripts and users are not affected.  We even use a try/finally block so that if an error condition is thrown by the move, we reset the measurement unit afterwards anyhow.

                 

                Lastly, unlike csm_phil, I don't cancel the selection at the end. I didn't see Danny asking for that and I don't think it's what you would want.