3 Replies Latest reply on Aug 7, 2013 1:16 AM by BEGINNER_X

    Find dpi & Align center of Image

    BEGINNER_X Level 3

      Hi All,

       

      I have new request but struggling in the below points:

       

      1. Find less than 300 dpi image in the active document

      2. Create text frame and fix center of the image vertically and horizotally.

       

      Hope I completed the script by 90%

       

      Completed Script:

       

      var myGraphics = app.activeDocument.allGraphics;
      var targetResolution = 300;
       for (var i = 0; i < myGraphics.length; i++) {
          try
          {
          if (myGraphics[i].effectivePpi[0] < 300) {
              var myPPI = myGraphics[i].effectivePpi[0] + " dpi"             //please ensure this line should be correct
              var myName = myGraphics[i].itemLink.name;
              var myPage = myGraphics[i].parentPage;
      
              var myCaption = myPage.textFrames.add();
              myCaption.textFramePreferences.verticalJustification = VerticalJustification.TOP_ALIGN;
              myCaption.contents = "FPO\r" + myName + "\r" + "Low Res " + myPPI;
      
              var bnds = myGraphics[i].parent.visibleBounds;
              myCaption.visibleBounds = [bnds[2] + 3, bnds[1], bnds[2] + 6, bnds[3]];
      
              myCaption.fit(FitOptions.FRAME_TO_CONTENT);
      
              try {
                  var myPStyle = app.activeDocument.paragraphStyles.add({name: "Caption_lowres", justification: Justification.CENTER_ALIGN});
      
              } catch (myError) {
                    myCaption.parentStory.appliedParagraphStyle = app.activeDocument.paragraphStyles.item("Caption_lowres");
                 }
              myPage.groups.add([myCaption, myGraphics[i].parent]);
          };
      }
      catch(myError)
      {
          }
      };
      

       

      REQUIRED OUTPUT

      output.png

       

      But Current Output using above script

       

      current output.png

       

      Thanks

      Beginner_X

        • 1. Re: Find dpi & Align center of Image
          Dave Saunders Level 4

          What you need to do is work out the center-point of the graphic:

           

          var center = {x : (bnds[1] + bnds[3])/2, y : (bnds[0] + bnds[2])/2}

           

          Then, after making your text frame, populating and fitting it, get its center point by using its bounds, and assign it to a similar variable:

           

          var capCenter = {x : (capbnds[1] + capbnds[3])/2, y : (capbnds[0] + capbnds[2])/2}

           

          Armed with this information, you can now do a relative move:

           

          myCaption.move(undefined, [center.x - capCenter.x, center.y - capCenter.y]);

           

          And your caption will be centered over the graphic.

           

          Dave

          1 person found this helpful
          • 2. Re: Find dpi & Align center of Image
            BEGINNER_X Level 3

            Hi Mr.Dave,

             

            Thank you for your response...

             

            I trying your code but I am not able to get the output.

             

            Could you help to get the output for me.

             

             

            var myGraphics = app.activeDocument.allGraphics;
             var targetResolution = 300;
             for (var i = 0; i < myGraphics.length; i++) {
                
                try
                {
                if (myGraphics[i].effectivePpi[0] < 300) {
                   var myPPI = myGraphics[i].effectivePpi[0] + " dpi"
            
                    var myName = myGraphics[i].itemLink.name;
                    var myPage = myGraphics[i].parentPage;
            
                    var myCaption = myPage.textFrames.add();
            
                    myCaption.textFramePreferences.verticalJustification = VerticalJustification.TOP_ALIGN;
                    myCaption.contents = "FPO\r" + myName + "\r" + "Low Res " + myPPI;
            
                    var bnds = myGraphics[i].parent.visibleBounds;
                    myCaption.visibleBounds = [bnds[2] + 3, bnds[1], bnds[2] + 6, bnds[3]];
                            
                    var center = {x : (bnds[1] + bnds[3])/2, y : (bnds[0] + bnds[2])/2}
                    var capCenter = {x : (capbnds[1] + capbnds[3])/2, y : (capbnds[0] + capbnds[2])/2}
                    myCaption.move(undefined, [center.x - capCenter.x, center.y - capCenter.y]);
            
            
                     myCaption.fit(FitOptions.FRAME_TO_CONTENT);
            
                    try {
                        var myPStyle = app.activeDocument.paragraphStyles.add({name: "Caption_lowres", justification: Justification.CENTER_ALIGN});
            
                    } catch (myError) {
                          myCaption.parentStory.appliedParagraphStyle = app.activeDocument.paragraphStyles.item("Caption_lowres");
                       }
                    myPage.groups.add([myCaption, myGraphics[i].parent]);
                };
            }
            catch(myError)
            {
                }
            };
            

             

            Help me to get output as per attached screenshot

             

            Thanks

            Beginner

            • 3. Re: Find dpi & Align center of Image
              BEGINNER_X Level 3

              Hi ALL,

               

              I found the answers in different way...

               

              Thanks to forum and Mr.Dave

               

               

              var myGraphics = app.activeDocument.allGraphics;
              for (var i = 0; i < myGraphics.length; i++) {
                      var myPage = myGraphics[i].parentPage;
                      var myCaption = myPage.textFrames.add();
                      var bnds = myGraphics[i].parent.visibleBounds;
                      myCaption.visibleBounds = [bnds[2] + 3, bnds[1], bnds[2] + 6, bnds[3]];
                      myCaption.contents = "FPO\r";
                      myCaption.fit(FitOptions.FRAME_TO_CONTENT);
              
                      app.select([myCaption, myGraphics[i].parent])
                      app.activeDocument.align(app.activeDocument.selection, AlignOptions.TOP_EDGES, AlignDistributeBounds.ITEM_BOUNDS);
                      app.activeDocument.align(app.activeDocument.selection,AlignOptions.VERTICAL_CENTERS, AlignDistributeBounds.ITEM_BOUNDS);
              
                      var myPageGroup = myPage.groups.add([myCaption, myGraphics[i].parent]);
                   };
              

               

              Thanks Forum

              Beginner