Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


Script to create static caption

Contributor ,
Aug 18, 2021 Aug 18, 2021

Copy link to clipboard

Copied

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]);
}
}
TOPICS
Scripting

Views

66

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Enthusiast , Aug 18, 2021 Aug 18, 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 myCaptio...

Likes

Translate

Translate
Enthusiast , Aug 18, 2021 Aug 18, 2021
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.PO...

Likes

Translate

Translate
Enthusiast ,
Aug 18, 2021 Aug 18, 2021

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Aug 18, 2021 Aug 18, 2021

Copy link to clipboard

Copied

So on the money!  And more.  🙂

 

Thank you for taking me there.  This is fantastic.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Aug 18, 2021 Aug 18, 2021

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Aug 18, 2021 Aug 18, 2021

Copy link to clipboard

Copied

 

// object style applied to frame
myCaption.applyObjectStyle(mDoc.objectStyles.itemByName('My Object Style'));

// paragraph style applied to paragraphs of (story of) frame
myCaption.paragraphs.everyItem().appliedParagraphStyle = mDoc.paragraphStyles.itemByName('My Style');

 

You can read about the method: 'applyObjectStyle" here. Also you can include the paragraph style with the object style, which is probably sensible in most cases.

- Mark

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Aug 18, 2021 Aug 18, 2021

Copy link to clipboard

Copied

Thank you so much!

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Aug 18, 2021 Aug 18, 2021

Copy link to clipboard

Copied

By the way, here's a list of some more linkXmp properties:

linkXmp.author
linkXmp.copyrightInfoURL
linkXmp.copyrightNotice
linkXmp.copyrightStatus
linkXmp.creationDate
linkXmp.creator
linkXmp.description
linkXmp.documentTitle
linkXmp.format
linkXmp.jobName
linkXmp.keywords
linkXmp.modificationDate
linkXmp.serverURL

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Aug 18, 2021 Aug 18, 2021

Copy link to clipboard

Copied

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');

 

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Aug 18, 2021 Aug 18, 2021

Copy link to clipboard

Copied

This is just better than great!!   I am experimenting with where to insert the name of the object style as my initial guess is off.

 

captionBox.applyObjectStyle(doc.objectStyles.itemByName(name of style goes here));

Maybe i have to define that above.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Aug 18, 2021 Aug 18, 2021

Copy link to clipboard

Copied

The link to Jongware's API really helped also.  I have it running with the object style now.  Cant thank you enough.  :-). Great lesson right here.  I know others will appreciate this thread.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Aug 18, 2021 Aug 18, 2021

Copy link to clipboard

Copied

In this version I moved it out of the function so you can specify when you call the function. Look for 'My Object Style' string passed in to the function (close to top).

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Aug 19, 2021 Aug 19, 2021

Copy link to clipboard

Copied

LATEST

Well, Mark, this thread just keeps getting better and better!  🙂 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines