• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

679

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

Community Expert , 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
...

Votes

Translate

Translate
Community Expert , 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
...

Votes

Translate

Translate
Community Expert ,
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

Votes

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.

Votes

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?

Votes

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
Community Expert ,
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

Votes

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!

 

Votes

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
Community Expert ,
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

Votes

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
Community Expert ,
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');

 

 

Votes

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.

Votes

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.

Votes

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
Community Expert ,
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).

Votes

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!  🙂 

Votes

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