14 Replies Latest reply on Jun 18, 2010 7:11 AM by Owen Linzmayer

    [AS] Find images and scale

    Jukka Lauhalahti

      Hi!

       

      I have a document with thousands of images most of them same size.

      However some of them are too big so i need to findthem and scale them down.

       

      How to find images that are too big, width > 50 mm and scale only those images 70% ?

       

      All images have fixed width frame so only the image within the frame should scale.

       

       

      Thanks

       

       

      jukka

        • 1. Re: [AS] Find images and scale
          sstanleyau Level 4

          Something like this should do it:

           

          tell application "Adobe InDesign CS3"

          set transform reference point of layout window 1 to center anchor

          tell document 1

          set theImages to all graphics

          repeat with i from 1 to count of theImages

          set {y1, x1, y2, x2} to geometric bounds of item i of theImages

          if x2 - x1 > 50 then

          set properties of item i of theImages to {horizontal scale:70, vertical scale:70}

           

          end if

          end repeat

          end tell

          end tell

          • 2. Re: [AS] Find images and scale
            Jukka Lauhalahti Level 1

            Hi  sstanleyau!

             

             

            Thanks for your code.

            With little modifications it did the trick.

             

             

            Thanks again!

             

             

             

            jukka

            • 3. Re: [AS] Find images and scale
              Owen Linzmayer Level 1

              I've been looking for something like this script, but am hoping you can make a few slight modifications, if it's not too much trouble.

               

              I want the script to look for all images in a doc that are 100%, then scale them to 50%. So I think all that needs to be done is change the if/then condition and the scale values. I know how to change the scale values, but haven't a clue as to how to properly alter the if/then condition to look for 100% image objects.

               

              Also, one other thing, I'd like the object frame to shrink down to the image's new reduced size.

               

              If anyone can help modify this script to suit my needs, I would be very grateful. Thanks in advance.

              • 4. Re: [AS] Find images and scale
                Kasyan Servetsky Level 5

                tell application "Adobe InDesign CS3"
                    set transform reference point of layout window 1 to center anchor
                    tell document 1
                        set theImages to all graphics
                        repeat with i from 1 to count of theImages
                            set MyImage to item i of theImages
                            if absolute horizontal scale of MyImage = 100 and absolute vertical scale of MyImage = 100 then
                                set absolute horizontal scale of MyImage to 50
                                set absolute vertical scale of MyImage to 50
                                tell parent of MyImage to fit given frame to content
                            end if
                        end repeat
                    end tell
                    display alert "Done"
                end tell

                • 5. Re: [AS] Find images and scale
                  Owen Linzmayer Level 1

                  Absolutely perfect! Works like a charm.

                   

                  That's going to save a lot of wear and tear on my hands and arms.

                   

                  I really appreciate the help.

                  • 6. Re: [AS] Find images and scale
                    M.Mahdi

                    can i use this script in Win7 ?!

                    (i have worked with extend scriipt toolkit only till now )

                    • 7. Re: [AS] Find images and scale
                      Kasyan Servetsky Level 5

                      No, you have translate it either to JS or VB.

                       

                      Kasyan

                      • 8. Re: [AS] Find images and scale
                        Kasyan Servetsky Level 5

                        Here is the same script translated into JS -- should work both on Mac and PC.

                        var doc = app.activeDocument;
                        app.layoutWindows[0].transformReferencePoint = AnchorPoint.CENTER_ANCHOR;
                        var theImages = doc.allGraphics;
                        
                        for (var i = 0; i < theImages.length; i++) {
                             var theImage = theImages[i];
                             if (theImage.absoluteHorizontalScale == 100 && theImage.absoluteVerticalScale == 100) {
                                  theImage.absoluteHorizontalScale = theImage.absoluteVerticalScale = 50;
                                  theImage.parent.fit(FitOptions.FRAME_TO_CONTENT);
                             }
                        }
                        
                        alert("Done");
                        
                        • 9. Re: [AS] Find images and scale
                          Owen Linzmayer Level 1

                          If it's not asking too much, can I ask for another slight alteration please?

                           

                          I'd like the script to check the image width after the 50% scale. If it's greater than 264 points wide (the width of columns in my layout), then I'd like the script to adjust the width down to 264 points, keeping the aspect ratio the same, so the height would need to scale relatively, too.

                           

                          I hope this is an easy tweak. Thanks in advance for any help you can provide.

                          • 10. Re: [AS] Find images and scale
                            Kasyan Servetsky Level 5

                            If it's not asking too much, can I ask for another slight alteration please?

                            Sure.

                             

                            if (app.documents.length == 0) {
                                 alert("No documents are open. Please open a document and try again.");
                            }
                            else {
                                 Main();
                            }
                            
                            function Main() {
                                 var doc = app.activeDocument;
                                 var images = doc.allGraphics;
                            
                                 var savedHorizontalMeasurementUnits = doc.viewPreferences.horizontalMeasurementUnits;
                                 var savedVerticalMeasurementUnits = doc.viewPreferences.verticalMeasurementUnits;
                                 doc.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.POINTS;
                                 doc.viewPreferences.verticalMeasurementUnits = MeasurementUnits.POINTS;
                            
                                 for (var i = 0; i < images.length; i++) {
                                      var image = images[i];
                                      var rec = image.parent;
                                      var gb = rec.geometricBounds;
                                      var width = gb[3] - gb[1];
                                      
                                      if (width > 264) {
                                           var scale = 264/width;
                                      }
                                      else {
                                           var scale = 0.5;
                                      }
                                 
                                      var myTransformationMatrix =  app.transformationMatrices.add({horizontalScaleFactor:scale, verticalScaleFactor:scale}); 
                                      rec.transform(CoordinateSpaces.pasteboardCoordinates, AnchorPoint.CENTER_ANCHOR, myTransformationMatrix); 
                                 }
                            
                                 doc.viewPreferences.horizontalMeasurementUnits = savedHorizontalMeasurementUnits;
                                 doc.viewPreferences.verticalMeasurementUnits = savedVerticalMeasurementUnits;
                            
                                 alert("Done");
                            }
                            
                            • 11. Re: [AS] Find images and scale
                              Owen Linzmayer Level 1

                              Thanks so much for the quick response to this request.

                               

                              The script seems to solve my problem of large images that would still be too wide to fit in the column after a 50% scale, however, the script resizes all images, even those previously scaled down.

                               

                              I'm hoping it's a simple fix to make it apply only to images that are 100 of their original size. That way if I run the script, then add more images and run the script again, the result is all images are 50% actual size, not the new additions being 50% and the previously reduced ones now 25%.

                               

                              And while I'm at it, can the script apply only to graphic objects of a given object style, specifically "Scans"?

                              • 12. Re: [AS] Find images and scale
                                Kasyan Servetsky Level 5

                                And while I'm at it, can the script apply only to graphic objects of a given object style, specifically "Scans"?

                                I assume that this object style in not nested inside a style group, otherwise the reference to it should include the group's name.

                                 

                                Kasyan

                                 

                                if (app.documents.length == 0) {
                                     alert("No documents are open. Please open a document and try again.");
                                }
                                else {
                                     Main();
                                }
                                
                                function Main() {
                                     var doc = app.activeDocument;       
                                     var images = doc.allGraphics;
                                
                                     var savedHorizontalMeasurementUnits = doc.viewPreferences.horizontalMeasurementUnits;
                                     var savedVerticalMeasurementUnits = doc.viewPreferences.verticalMeasurementUnits;
                                     doc.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.POINTS;
                                     doc.viewPreferences.verticalMeasurementUnits = MeasurementUnits.POINTS;
                                
                                     for (var i = 0; i < images.length; i++) {
                                          var image = images[i];
                                          if (image.absoluteHorizontalScale == 100 && image.absoluteVerticalScale == 100) {
                                               var rec = image.parent;
                                               if (rec.appliedObjectStyle == doc.objectStyles.item("Scans")) {
                                                    var gb = rec.geometricBounds;
                                                    var width = gb[3] - gb[1];
                                                    
                                                    if (width > 264) {
                                                         var scale = 264/width;
                                                    }
                                                    else {
                                                         var scale = 0.5;
                                                    }
                                               
                                                    var myTransformationMatrix =  app.transformationMatrices.add({horizontalScaleFactor:scale, verticalScaleFactor:scale}); 
                                                    rec.transform(CoordinateSpaces.pasteboardCoordinates, AnchorPoint.CENTER_ANCHOR, myTransformationMatrix); 
                                               }
                                          }
                                     } 
                                
                                     doc.viewPreferences.horizontalMeasurementUnits = savedHorizontalMeasurementUnits;
                                     doc.viewPreferences.verticalMeasurementUnits = savedVerticalMeasurementUnits;
                                
                                     alert("Done");
                                }
                                
                                • 13. Re: [AS] Find images and scale
                                  Owen Linzmayer Level 1

                                  That appears to have done the trick.

                                   

                                  Thank you so much!

                                   

                                  This is going to save me a tremendous amount of needless busy work tweaking my catalog's thousands of images.

                                   

                                  Have a great weekend Kasyan!

                                  • 14. Re: [AS] Find images and scale
                                    Owen Linzmayer Level 1

                                    I found one small problem, but was able to fix it myself.

                                     

                                    I had to double the value in the line that reads "  if (width > 264) {"