A script/plugin to write position and size of object

Learn / Forums / InDesign Add-ons (Scripts, Scripting, and Plug-ins) / A script/plugin to write position and size of object

Viewing 17 reply threads
  • Author
    Posts
    • #86184

      HI there,

      I am working on a 800 page document and have to record on each page the x/y position and the size of every object. It is info for further software developement. The last time it was all input manually and took 3 months to finish but I’m wondering if there is a script or plugin that could do this for me. Pain in the neck!!

    • #86186
      Ari Singer
      Member

      This can probably be done. But more details is needed, such as what is considered an ‘object’? Text frames, images, etc. And where do you want to output this info? To a text file, or on the page directly?

    • #86187

      Yes every text frame and image needs to have info on size and position on page. We have been doing this manually and putting info next to the corresponding objects but on a different layer so that you can see the clean design. For 800 pages that’s a lot of work.

    • #86192
      Ari Singer
      Member

      Here you go:

      app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Get XY Coords");
      function main() {
          var myDoc = app.activeDocument;
          var myPages = myDoc.pages;
          var myLayers = myDoc.layers;
          for (var k = 0; k < myLayers.length; k++) {
              if (myLayers[k].name == "Info Layer")
              myLayers[k].remove();
              break;
              }
          var myObjectStyle;
          var myParaStyle;
          myDoc.layers.add({name: "Info Layer"});
          try {
              myParaStyle = myDoc.paragraphStyles.item("InfoStyle");
              myParaStyle.name;       
              }
          catch (myError){
              myParaStyle = myDoc.paragraphStyles.add({name:"InfoStyle", pointSize:10, leading:14, noBreak: true});
              }
          try{
              myObjectStyle = myDoc.objectStyles.item("InfoFrame");
              myObjectStyle.name;
              }
          catch (myError){
              myObjectStyle = myDoc.objectStyles.add();
              with(myObjectStyle) {
                  name = "InfoFrame";
                  basedOn = "[None]";
                  enableFrameFittingOptions = true;
                  enableAnchoredObjectOptions = false;
                  enableFill = false;
                  enableParagraphStyle = true;
                  enableStroke = false;
                  enableStrokeAndCornerOptions = false;
                  enableTextFrameAutoSizingOptions = true;
                  enableTextFrameGeneralOptions = true;
                  enableTextWrapAndOthers = false;
                  myObjectStyle.textFramePreferences.autoSizingType = AutoSizingTypeEnum.HEIGHT_AND_WIDTH;
                  myObjectStyle.textFramePreferences.autoSizingReferencePoint = AutoSizingReferenceEnum.BOTTOM_LEFT_POINT;
                  myObjectStyle.appliedParagraphStyle = myParaStyle;
                  }
              }
          for (i = 0; i <myPages.length; i++) {
              var allStuff = myPages[i].allPageItems;
              for (j = 0; j < allStuff.length; j++) {
                  var curStuff = allStuff[j];
                  var myBounds = curStuff.geometricBounds;
                  var myY = myBounds[0];
                  var myX = myBounds[1];
                  var myH = myBounds[2] - myY;
                  var myW = myBounds[3] - myX;
                  var myString = "Y: " + myY.toFixed(1) + " X: " + myX.toFixed(1) + " H: " + myH.toFixed(1) + " W: " + myW.toFixed(1);
                  var myFrame = myPages[i].textFrames.add();
                  myFrame.geometricBounds = [(myY - 5), myX, (myY - 2), (myX + 1)];
                  myFrame.appliedObjectStyle = myObjectStyle;
                  myFrame.contents = myString;
                  //alert(myString);
                  }
              }
          }
      

      This creates a new layer called “Info Layer”, and places all the info text on that layer. Don’t place anything yourself on this layer, because whenever you run the script again this layer will automatically be destroyed to make sure all the info is up to date.

      You can play around with the paragraph style (“InfoStyle”), and the object style (“InfoFrame”) that the script automatically creates, and the good news is that your settings won’t be lost when you run the script again.

      This script assumes that you have a later version of InDesign that supports auto-sizing text frames.

      Good luck!

      Ari

    • #86199

      Wow, thanks very much for this—I can’t wait to try it!!

    • #86200

      Ummmm, I’ve not done this before but I followed all the instructions to get it into InDesign (not hard) but it didn’t work. Is it Javascript or Applescript. Sorry, just a front end user!! :-(

    • #86201
      Ari Singer
      Member

      Please upload a screenshot of the error. Did you copy the code from here or from your email? Because copying from the email can be problematic.

    • #86202
      Ari Singer
      Member

      And it’s JavaScript.

    • #86203
      Ari Singer
      Member
    • #86204

      Yeeeha, it worked! I can’t thank you enough. can I donate something to you?

    • #86205
      Ari Singer
      Member

      My pleasure! I did not do it for money, but donations are always accepted :) Please send me an email to designerjoe[at]outlook.com

    • #86206

      Will do. A slight problem though. It only works the first time. As its 800 pages long, you cannot select all the objects at once. It works fine the first time but then errors as it tries to make another layer of the same name each time. Is there any way it can be scripted so that I can make the layer first and give it a name then you use this name in the script?

    • #86207
      Ari Singer
      Member

      That’s interesting, because the script is supposed to delete that layer every time it runs. Did you try to manually delete the entire layer?

    • #86208

      I think I see how it works now. I think I am correct in saying that you don’t select the objects first. You just double click the script. That is where I was going wrong perhaps. So works when I test on a 2 page doc but freezes with the real one

    • #86209

      I will test by breaking up the document into managable sizes

    • #86210
      Ari Singer
      Member

      Who says that it actually freezes? Maybe you just have to let it finish. How long did you wait?

    • #86211

      You are correct but can’t handle 800 pages. I broke it up to 200 pages and it took about 2 mins to process.Brilliant. Thanks.

    • #86270
      Ari Singer
      Member

      I fixed the script to use only frames, and not the inner objects in them, so not to duplicate the label frames. I also made sure that when the script ends executing, the active layer is not the “InfoLayer” so you don’t mistakenly put important objects on this layer (it gets deleted every time the script runs).

      app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Get XY Coords");
      function main() {
          var myDoc = app.activeDocument;
          var myPages = myDoc.pages;
          var myLayers = myDoc.layers;
          for (var k = 0; k < myLayers.length; k++) {
              if (myLayers[k].name == "Info Layer")
              myLayers[k].remove();
              break;
              }
          var myObjectStyle;
          var myParaStyle;
          myDoc.layers.add({name: "Info Layer"});
          try {
              myParaStyle = myDoc.paragraphStyles.item("InfoStyle");
              myParaStyle.name;       
              }
          catch (myError){
              myParaStyle = myDoc.paragraphStyles.add({name:"InfoStyle", pointSize:10, leading:14, noBreak: true});
              }
          try{
              myObjectStyle = myDoc.objectStyles.item("InfoFrame");
              myObjectStyle.name;
              }
          catch (myError){
              myObjectStyle = myDoc.objectStyles.add();
              with(myObjectStyle) {
                  name = "InfoFrame";
                  basedOn = "[None]";
                  enableFrameFittingOptions = true;
                  enableAnchoredObjectOptions = false;
                  enableFill = false;
                  enableParagraphStyle = true;
                  enableStroke = false;
                  enableStrokeAndCornerOptions = false;
                  enableTextFrameAutoSizingOptions = true;
                  enableTextFrameGeneralOptions = true;
                  enableTextWrapAndOthers = false;
                  myObjectStyle.textFramePreferences.autoSizingType = AutoSizingTypeEnum.HEIGHT_AND_WIDTH;
                  myObjectStyle.textFramePreferences.autoSizingReferencePoint = AutoSizingReferenceEnum.BOTTOM_LEFT_POINT;
                  myObjectStyle.appliedParagraphStyle = myParaStyle;
                  }
              }
          for (i = 0; i <myPages.length; i++) {
              var allStuff = myPages[i].allPageItems;
              for (j = 0; j < allStuff.length; j++) {
                  var curStuff = allStuff[j];
                  if ((curStuff.constructor.name == "Image") || (curStuff.constructor.name == "PDF") || (curStuff.constructor.name == "ImportedPage")) {
                      continue;
                      }
                  var myBounds = curStuff.geometricBounds;
                  var myY = myBounds[0];
                  var myX = myBounds[1];
                  var myH = myBounds[2] - myY;
                  var myW = myBounds[3] - myX;
                  var myString = "Y: " + myY.toFixed(1) + " X: " + myX.toFixed(1) + " H: " + myH.toFixed(1) + " W: " + myW.toFixed(1);
                  var myFrame = myPages[i].textFrames.add();
                  myFrame.geometricBounds = [(myY - 5), myX, (myY - 2), (myX + 1)];
                  myFrame.appliedObjectStyle = myObjectStyle;
                  myFrame.contents = myString;
                  }
              }
          myDoc.activeLayer = myDoc.layers[1];
          }
      
Viewing 17 reply threads
  • You must be logged in to reply to this topic.
>
Notice: We use cookies on our websites to give you a great online experience. If you keep browsing, we'll assume you're ok with this. For more information, see our privacy policy. By closing this banner, you agree to the use of cookies.I AGREENo