Skip to main content
Inspiring
August 18, 2021
Answered

Script to create static caption

  • August 18, 2021
  • 1 reply
  • 1427 views

There are other manners to automate captions (checkbox on import, variable text + object and p-styles), but I thought I could get this to work and now at a loss.    If someone has a moment to look at this, target the line    myCaption.contents = <Metadata:Description></Metadata:Description>;  

 

For if I was to use    myCaption.contents = "caption text goes here";      you would be served placeholder text.  I thought I would be closer to snagging the description, but no cigar as yet.    🙂

 

 

capset ();
function capset()
{
var
mDoc = app.activeDocument,
//anchored or grouped items excluded
myGraphics = mDoc.splineItems.everyItem().getElements(),
picContainergbmyCaption;
while(picContainer = myGraphics.pop() )
//exclude empty frames
if (!picContainer.graphics.lengthcontinue;
gbpicContainer.geometricBounds;
//add a frame to to picture’s parent, which is a Page
myCaption = picContainer.parentPage.textFrames.add ();
//set position and size of the caption
myCaption.geometricBounds = [gb[2], gb[1], gb[2]+3gb[3]];
//add placeholder contents
myCaption.contents = <Metadata:Description></Metadata:Description>;
mDoc.groups.add ([picContainermyCaption]);
}
}
This topic has been closed for replies.
Correct answer m1b

Hi @MadMac55, just for learning purposes, here's a version of your script with a bit of error handling added. For example, the other script will throw an error if there isn't metadata for the linked image.

 

function main() {

    addCaptionToImages('My Object Style', 30);

    function addCaptionToImages(objStyleName, captionBoxHeight) {

        // setting this explicitly so we can
        // be sure what captionBoxHeight means!
        app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS;
        captionBoxHeight = captionBoxHeight || 20;

        var doc = app.activeDocument,
            imgs = doc.allGraphics,
            img,
            captionBox;

        while (img = imgs.pop()) {
            try {
                var frame = img.parent,
                    b = frame.geometricBounds,
                    // get metadata
                    lnk = img.itemLink,
                    filename = lnk.name,
                    imgTitle = lnk.linkXmp.documentTitle,
                    imgAuthor = lnk.linkXmp.author,
                    imgDescription = lnk.linkXmp.description;
            } catch (error) {
                /* metadata not available */
            }
            // exclude images that aren't at the root level of a page or spread
            if (!(frame.parent.constructor.name == 'Page' || frame.parent.constructor.name == 'Spread')) continue;
            // add a frame for the caption text
            captionBox = img.parentPage.textFrames.add();
            // set position and size of the caption
            captionBox.geometricBounds = [b[2], b[1], b[2] + captionBoxHeight, b[3]];
            //add caption text
            captionBox.contents = imgDescription || filename + ': no metadata found.';
            // object style
            try {
                if (objStyleName != undefined)
                    captionBox.applyObjectStyle(doc.objectStyles.itemByName(objStyleName));
            } catch (error) {
                /* object style not found in document */
            }
            // group
            try {
                doc.groups.add([frame, captionBox]);
            } catch (error) {
                /* can't group objects */
            }
        }
    }


} // end main

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Add Caption To Images');

 

 

1 reply

m1b
Community Expert
Community Expert
August 19, 2021

Something like this?

capset();
function capset() {
    var mDoc = app.activeDocument,
        //anchored or grouped items excluded
        myGraphics = mDoc.splineItems.everyItem().getElements(),
        picContainer,
        gb,
        myCaption;

    while (picContainer = myGraphics.pop()) {
        //exclude empty frames
        if (!picContainer.graphics.length) continue;
        gb = picContainer.geometricBounds;
        //add a frame to to picture’s parent, which is a Page
        myCaption = picContainer.parentPage.textFrames.add();
        //set position and size of the caption
        myCaption.geometricBounds = [gb[2], gb[1], gb[2] + 3, gb[3]];
        // get xmp data
        var img = picContainer.images[0],
            lnk = img.itemLink,
            imgTitle = lnk.linkXmp.documentTitle,
            imgAuthor = lnk.linkXmp.author,
            imgDescription = lnk.linkXmp.description;
        //add placeholder contents
        myCaption.contents = imgDescription;
        mDoc.groups.add([picContainer, myCaption]);
    }
}

 Using the linkXmp property of the link.

- Mark

MadMac55Author
Inspiring
August 19, 2021

So on the money!  And more.  🙂

 

Thank you for taking me there.  This is fantastic.

MadMac55Author
Inspiring
August 19, 2021

Hey Mark, where would you put a line of code for Paragraph or Object style?