Reply To: A script/plugin to write position and size of object

#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

This article was last modified on July 1, 2016

Comments (0)

Loading comments...