4 Replies Latest reply on Dec 4, 2015 6:53 AM by suzec57781700

    Need a Script to total space of images on a spread, then label each image with percentage of total image space

    suzec57781700 Level 1

      By editing other scripts I've found here and other sites (including rorohiko size labels), I worked out a script that will label each image with the dimensions and calculate the percentage of the entire spread (including text, white space).

       

      Now I've been asked for a script that will label images with the percentage of total image space on a spread. So this script needs to total space of only images on a spread, then label each image with percentage of total image space. For example if there is one image that is 10x10 inches and four images that are 2.5x2.5 images, the 10x10 would be labeled with 50% and the 2.5x2.5 would be labeled with 12.5%.

       

      This is the script that does percentage of total spread. Can some smart person get it to do what I need?

       

      Suze

       

       

      (sorry, I don't know how to post the script here with the line numbers!)

      //

      // This script adds printing info labels to every graphic frame. The labels are on a separate layer

      // named "PercentageLabels" so all of these added labels can be deleted again by simply deleting

      // the layer.

      //

      // If you move or resize items, simply re-run the script to update the labels.

      //

      // Before (re)running the script, you can define a paragraph style "SizeLabelStyle".

      // This style will be applied to all the contents of the labels.

      //

      // Before (re)running the script, you can define an object style "SizeLabelObjectStyle".

      // This object style will be applied to all label frames - so if you want a specific background

      // fill or frame, do it via this object style.

      //

      // You can change the units of the size labels. I've set it to inches. Change the line below that has

      // kUnitToUse - you can use:

      // MeasurementUnits.CENTIMETERS

      // MeasurementUnits.CICERO

      // MeasurementUnits.INCHES

      // MeasurementUnits.MILLIMETERS

      // MeasurementUnits.PICAS

      // MeasurementUnits.PIXELS

      // MeasurementUnits.POINTS

      //

      // By default the script will not attempt to label 'nested' frames. If you want to also label

      // nested frames (e.g. inline frames, anchored frames, frames pasted into frames,...) change the line

      // below to read:

      // kLabelNestedItems = 1

      //

      // If you want a specific frame to remain unlabeled, you can use the Script Label palette to

      // assign a script label "ignore" to this frame. The script will then not label this frame until

      // the script label is removed again.

      //

      // Change the lines with kDontLabelImageFrames, kDontLabelTextFrames, kDontLabelOtherFrames

      // below if you want to limit the labeling to only certain types of frames: change the '0'

      // into '1' to enable these options.

      //

      // Change the line with kShowImageFileName if you want to also show image names in addition to the

      // size for image frames.

      //

      // Small frames are not labeled - the cut off point is currently any frame less than 0.1 inch high

      // or 0.1 inch wide. You can change the cutoff point by adjusting the lines with kDontLabelBelowWidth

      // or kDontLabelBelowHeight below.

      //

       

       

      var gErr;

      var gScriptFile;

      var gCaches;

      // ****************

       

       

       

       

      //To have the spread squinch automatically calculated

      var doc = app.activeDocument;

      doc.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.inches;

      doc.viewPreferences.verticalMeasurementUnits = MeasurementUnits.inches;

      var pagewidth = doc.documentPreferences.pageWidth;

      var spreadwidth = pagewidth * 2;

      var spreadheight = doc.documentPreferences.pageHeight;

      var spreadsquinch = spreadwidth * spreadheight;

      //alert("The Total Square Inch Measurement of the Spread is: " + spreadsquinch);

       

       

      //******************

       

       

      try

      {

          if (app.activeScript instanceof File)

          {

              var gScriptFile = app.activeScript;

          }

      }

      catch (gErr)

      {

      }

       

       

      if (typeof(kLogLevel) == "undefined")

      {

          const kLogLevel = 5;

          const kMaxLogNestLevel = 5;

          const kLogToMessageBox = 0;

          const kLogToConsole = 0;

          const kLogToFile = 0;

          const kLogFileName = "PercentageLabels.log";

          const kIgnoreFrameLabel = "ignore";

          const kSizeLabelLayerName = "PercentageLabels";

          const kLabelNestedItems = 0;

          const kDontLabelImageFrames = 0;

          const kDontLabelTextFrames = 1;

          const kDontLabelOtherFrames = 0;

          const kUnitToUse = MeasurementUnits.INCHES;

          const kShowImageFileName = 0; // Set this to 1 to prepend the image file name to the label

          // All measurements below are expressed in kUnitToUse (e.g. inches if MeasurementUnits.INCHES is used)

          const kRoundToDecimals = 2;

          // Separators to use between items - e.g. MyImage.jpg - 7.22 x 9.34

          const kSeparatorBetweenNameAndSize = " - ";

          const kSeparatorBetweenHeightAndWidth = " H x ";

          const kSeparatorAfterWidth = " W";

          const kDontLabelBelowWidth = 0.1; // Expressed in kUnitToUse

          const kDontLabelBelowHeight = 0.1; // Expressed in kUnitToUse

          const kLabelStyleName = "SizeLabelStyle";

          const kLabelObjectStyleName = "SizeLabelObjectStyle";

          const kLabelHeight = 0.6; // Expressed in kUnitToUse

          const kLabelWidth = 2.5; // Expressed in kUnitToUse (e.g. inch).

       

      }

       

       

      var gRunState =

      {

          logToMessageBox: kLogToMessageBox,

          logToConsole: kLogToConsole,

          logToFile: kLogToFile,

          functionNestLevel: 0,

          logFunctionEntry: true,

          logFunctionExit: true,

          logLevel: kLogLevel,

          logFileName: kLogFileName

      };

       

       

      Main();

       

       

      // ****************

       

       

      function ConvertToFrame(thePageItem)

      {

          /**///LogEntry("ConvertToFrame");

          var retVal = thePageItem.getElements()[0];

          /**///LogExit("ConvertToFrame");

        

          return retVal;

      }

      // ****************

       

       

      function CollectionToArray(theCollection)

      {

          /**///LogEntry("CollectionToArray");

          var retVal = theCollection.everyItem().getElements().slice(0);

          /**///LogExit("CollectionToArray");

        

          return retVal;

      }

       

       

      // ****************

       

       

      function GetImageFile(pageItem)

      {

          /**///LogEntry("GetImageFile");

       

       

          var imageFile = null;

          do

          {

              try

              {

                  pageItem = ConvertToFrame(pageItem);

       

       

                  if ( !("allGraphics" in pageItem))

                  {

        break;

                  }

        

          var allGraphics = pageItem.allGraphics.slice(0);

          if (allGraphics.length == 0)

          {

          break;

          }

        

          var graphic = allGraphics[0];

          var link = graphic.itemLink;

          imageFile = new File(link.filePath);

              }

              catch (gErr)

              {          

              }

          }

          while (false);

       

       

          /**///LogExit("GetImageFile");

       

       

          return imageFile;

      }

       

       

      // ****************

       

       

      function GetImageName(pageItem)

      {

          /**///LogEntry("GetImageName");

       

       

          var imageName = "";

          do

          {

              var imageFile = GetImageFile(pageItem);

              if (imageFile == null)

              {

                  break;

              }

       

       

              imageName = imageFile.displayName;

          }

          while (false);

       

       

          /**///LogExit("GetImageName");

       

       

          return imageName;

      }

       

       

      // ****************

       

       

      function GetOuterPageItem(pageItem)

      {

          /**///LogEntry("GetOuterPageItem");

        

          var retVal = null;

          do

          {

              try

              {

                  retVal = pageItem.parent;

                  if (retVal instanceof Spread || retVal instanceof Page)

                  {

                      retVal = pageItem;

                      break;

                  }

            

                  if (retVal instanceof Character)

                  {

                      retVal = GetOuterPageItem(retVal.parentTextFrames[0]);            

                      break;

                  }

              }

              catch (gErr)

              {

                  retVal = null;

              }

          }

          while (false);

       

       

          /**///LogExit("GetOuterPageItem");

        

          return retVal;

      }

       

      // ****************

       

       

      function LogMessage(message,collapseLines)

      {

          if (gRunState.logToConsole)

          {

              $.writeln(message);

          }

          if (gRunState.logToFile)

          {

              var logFile = new File(gScriptFile.parent + "/" + gRunState.logFileName);

              var now = new Date();

              logFile.open("a");

              logFile.write(now.toString() + ": " + message + "\n");

              logFile.close();

          }

          if (gRunState.logToMessageBox)

          {

              alert(message);

          }

      }

       

       

      // ****************

       

       

      function LogEntry(message)

      {

          if (gRunState.logFunctionEntry)

          {

              if (gRunState.functionNestLevel < kMaxLogNestLevel) {

                  for (var idx = 0; idx < gRunState.functionNestLevel; idx++)

                  {

                      message = "  " + message;

                  }

                  LogNote(message + "<",true);

              }

          }

          gRunState.functionNestLevel++;

      }

       

       

      // ****************

       

       

      function LogError(message)

      {

          if (gRunState.logLevel >= 1)

          {

              LogMessage("***ERROR***: "+message,false);

          }

      }

       

       

      // ****************

       

       

      function LogExit(message)

      {

          gRunState.functionNestLevel--;

          if (gRunState.functionNestLevel < 0)

          {

              LogError("LogExit: unbalanced function nesting");

          }

          if (gRunState.logFunctionExit)

          {

              if (gRunState.functionNestLevel < kMaxLogNestLevel) {

                  for (var idx = 0; idx < gRunState.functionNestLevel; idx++)

                  {

                      message = "  " + message;

                  }

                  LogNote(message + ">",true);

              }

          }

      }

       

       

      // ****************

       

       

      function LogNote(message)

      {

          if (gRunState.logLevel >= 3)

          {

              LogMessage("Note: "+message,false);

          }

      }

       

       

      // ****************

       

       

      function LogWarning(message)

      {

          if (gRunState.logLevel >= 2)

          {

              LogMessage("WARNING: "+message,false);

          }

      }

       

       

      // ****************

       

       

      function Main()

      {

          /**///LogEntry("Main");

        

          var savedUserInteractionLevel = app.scriptPreferences.userInteractionLevel;

          app.scriptPreferences.userInteractionLevel = UserInteractionLevels.NEVER_INTERACT;

        

          do

          {

              try

              {

                  var activeDocument = app.activeDocument;

                  if (! (activeDocument instanceof Document))

                  {

                      LogError("Main: activeDocument is not a Document");

                      break;

                  }

                

                  var savedHorizontalMeasurementUnits = activeDocument.viewPreferences.horizontalMeasurementUnits;

                  activeDocument.viewPreferences.horizontalMeasurementUnits = kUnitToUse;

                  var savedVerticalMeasurementUnits = activeDocument.viewPreferences.verticalMeasurementUnits;

                  activeDocument.viewPreferences.verticalMeasurementUnits = kUnitToUse;

                  var savedRulerOrigin = activeDocument.viewPreferences.rulerOrigin;

                  activeDocument.viewPreferences.rulerOrigin = RulerOrigin.PAGE_ORIGIN;

                  do

                  {

                      try

                      {

                          ProcessFrames(activeDocument);

                      }

                      catch (gErr)

                      {

                          alert("The script encountered a situation it does not know how to handle. The error it received into says '" + gErr + "' in Main()");

                      }

                  }

                  while (false);

                  activeDocument.viewPreferences.rulerOrigin = savedRulerOrigin;

                  activeDocument.viewPreferences.horizontalMeasurementUnits = savedHorizontalMeasurementUnits;

                  activeDocument.viewPreferences.verticalMeasurementUnits = savedVerticalMeasurementUnits;

              }

              catch (gErr)

              {

                  alert("The script encountered a situation it does not know how to handle. The error it received into says '" + gErr + "' in Main()");

                  LogError("Main throws " + gErr);

              }

          }

          while (false);

          app.scriptPreferences.userInteractionLevel = savedUserInteractionLevel;

       

       

          /**///LogExit("Main");

      }

       

       

      // ****************

       

       

      function ProcessFrames(doc)

      {

          /**///LogEntry("ProcessFrames");

       

       

          var err;

          do

          {

              if (! (doc instanceof Document))

              {

                LogError("ProcessFrames: doc should be a Document");

                break;

              }

            

              try

              {

                  var existingLayer = doc.layers.itemByName(kSizeLabelLayerName);

                  existingLayer.id;

                  existingLayer.remove();

              }

              catch (err)

              {

              }

            

              if (kLabelNestedItems)

              {

                  var pageItems = doc.allPageItems.slice(0);

              }

              else

              {

                  var pageItems = CollectionToArray(doc.pageItems);

              }

            

              var frameReferences = [];

              for (var pageItemIdx = 0; pageItemIdx < pageItems.length; pageItemIdx++)

              {

                  do

                  {

                      var pageItem = pageItems[pageItemIdx];

                      if (pageItem.itemLayer.name == kSizeLabelLayerName)

                      {

                          break;

                      }

       

       

                      var pageItem = ConvertToFrame(pageItem);

                      if (pageItem instanceof TextFrame)

                      {

                          if (kDontLabelTextFrames)

                          {

                              break;

                          }

                      }

                      else if (pageItem instanceof Graphic)

                      {

                          if (kDontLabelImageFrames)

                          {

                              break;

                          }

                      }

                      else

                      {

                          if (kDontLabelOtherFrames)

                          {

                              break;

                          }

                      }

                    

                      if (pageItem.label.toLowerCase() == kIgnoreFrameLabel)

                      {

                          break;

                      }

                

                      var width = pageItem.geometricBounds[3] - pageItem.geometricBounds[1];

                      var height = pageItem.geometricBounds[2] - pageItem.geometricBounds[0];

       

       

                      if (width < kDontLabelBelowWidth)

                      {

                          break;

                      }

                      if (height < kDontLabelBelowHeight)

                      {

                          break;

                      }

       

       

                      frameReferences.push(pageItem);

                  }

                  while (false);

              }

            

              var sizeLabelLayer = doc.layers.add({ name: kSizeLabelLayerName });

            

              for (var pageItemIdx = 0; pageItemIdx < frameReferences.length; pageItemIdx++)

              {

                  try

                  {

                      var pageItem = frameReferences[pageItemIdx];

                      var width = pageItem.geometricBounds[3] - pageItem.geometricBounds[1];

                      var height = pageItem.geometricBounds[2] - pageItem.geometricBounds[0];

                      var labelItem = doc.textFrames.add(null, LocationOptions.BEFORE, GetOuterPageItem(pageItem));

                      labelItem.itemLayer = sizeLabelLayer;

                      var boxsquinch = width * height

                      var boxpercent = (boxsquinch / spreadsquinch) * 100

                      var roundpercent = Math.round(boxpercent)

                    

                      var labelText = "";

                      if (kShowImageFileName)

                      {

                          var imageName = GetImageName(pageItem);

                          if (imageName != "")

                          {

                              labelText += imageName + kSeparatorBetweenNameAndSize;

                          }

                      }

                      labelText += RoundString(height, kRoundToDecimals) + kSeparatorBetweenHeightAndWidth + RoundString(width, kRoundToDecimals)+ kSeparatorAfterWidth+ '\r\n'+ roundpercent+ '%';

                      labelItem.contents = labelText;

                      var xPos = pageItem.geometricBounds[1];

                      var yPos = pageItem.geometricBounds[0] + kLabelHeight;

                      labelItem.geometricBounds = [ yPos, xPos, yPos + kLabelHeight, xPos + kLabelWidth ];

                      try

                      {

                          labelItem.parentStory.characters.everyItem().appliedCharacterStyle = doc.characterStyles.item(0);

                      }

                      catch (err)

                      {

                      }

                

                      try

                      {

                          labelItem.parentStory.paragraphs.everyItem().appliedParagraphStyle = kLabelStyleName;

                      }

                      catch (err)

                      {

                      }

                    

                      try

                      {

                          labelItem.appliedObjectStyle = doc.objectStyles.itemByName(kLabelObjectStyleName);

                      }

                      catch (err)

                      {

                      }

                  }

                  catch (err)

                  {

                  }         

              }

            

          }

          while (false);

        

          /**///LogExit("ProcessFrames");

      }

       

       

      // ****************

       

       

      function RoundString(numVal, decimals)

      {

        

          /**///LogEntry("Round");

          var retVal = Math.round(numVal * Math.pow(10,decimals)) + "";

          retVal = retVal.substring(0,retVal.length-decimals) + "." + retVal.substring(retVal.length-decimals);

          /**///LogExit("Round");

        

          return retVal;

      }

        • 1. Re: Need a Script to total space of images on a spread, then label each image with percentage of total image space
          BSKTCreation Level 2

          Hi Suze,

           

          I've put a rudimentary script together (making it less daunting to edit), it calculates the percentage of each rectangle within a spread. I chose rectangles because the image is inside the rectangle and images can be different sizes to them. I didn't put any labeling in the script but you shouldn't have much issues adding that part.

           

          var myDoc = app.activeDocument;
          var mySpreads = myDoc.spreads;
          var myRectangles = myDoc.rectangles;
          var myImages = 0;
          for (var i = 0; i < mySpreads.length; i++) {
              var mySpreadWidth = 0;
              var mySpreadHeight = myDoc.documentPreferences.pageHeight;
              for (var j = 0; j < mySpreads[i].pages.length; j++) {
                  mySpreadWidth += myDoc.documentPreferences.pageWidth;
                  }
              var mySpreadArea = mySpreadWidth * mySpreadHeight;
              var myImages = mySpreads[i].rectangles;
              for (var j = 0; j < myImages.length; j++) {
                  var myImageGeometricBounds = myImages[j].geometricBounds;
                  var myImageWidth = myImageGeometricBounds[3] - myImageGeometricBounds[1];
                  var myImageHeight = myImageGeometricBounds[2] - myImageGeometricBounds[0];
                  var myImageArea = myImageWidth * myImageHeight;
                  var myImagePercentage = (myImageArea * 100) / mySpreadArea;
                  alert(myImagePercentage);
                  }
              }
          

           

          Let me know how you go.

           

          Brett

          • 2. Re: Need a Script to total space of images on a spread, then label each image with percentage of total image space
            suzec57781700 Level 1

            Hi Brett,

             

            Thanks for taking a crack at this. This seems to do something similar to the script that I already  have - return the percentage of the entire spread. What I need is for it to total the image area on the spread (space that is used by images) and then return the percentage that each image takes of that total image area.

             

            Suze

            • 3. Re: Need a Script to total space of images on a spread, then label each image with percentage of total image space
              BSKTCreation Level 2

              Hows this?

               

              var myDoc = app.activeDocument;  
              var mySpreads = myDoc.spreads;  
              var myRectangles = myDoc.rectangles;  
              var myImages = 0;  
              for (var i = 0; i < mySpreads.length; i++) {  
                  var mySpreadWidth = 0;  
                  var mySpreadHeight = myDoc.documentPreferences.pageHeight;  
                  for (var j = 0; j < mySpreads[i].pages.length; j++) {  
                      mySpreadWidth += myDoc.documentPreferences.pageWidth;  
                      }  
                  var mySpreadArea = mySpreadWidth * mySpreadHeight;
                  var myImages = mySpreads[i].rectangles;  
                  var myTotalImageArea = 0;
                  var myIndividualImageArea = [];
                  for (var j = 0; j < myImages.length; j++) {  
                      var myImageGeometricBounds = myImages[j].geometricBounds;  
                      var myImageWidth = myImageGeometricBounds[3] - myImageGeometricBounds[1];  
                      var myImageHeight = myImageGeometricBounds[2] - myImageGeometricBounds[0];
                      myIndividualImageArea[j] = (myImageWidth * myImageHeight); 
                      myTotalImageArea += myIndividualImageArea[j];
                      } 
                  for (var j = 0; j < myIndividualImageArea.length; j++) {
                      alert("Image percentage of total images on spread " + (i + 1) + " = " + (myIndividualImageArea[j] * 100) /myTotalImageArea);
                      }
                 alert("Total image percentage on spread " + (i + 1) + " = " + myTotalImageArea);
                 } 
              
              • 4. Re: Need a Script to total space of images on a spread, then label each image with percentage of total image space
                suzec57781700 Level 1

                Yes!!! Thank you so much. This returns the percentages that I need!

                 

                Suze.