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

Is there a way to apply Generic Static Caption to all graphic containers

Explorer ,
Nov 23, 2022 Nov 23, 2022

Copy link to clipboard

Copied

I'm laying out a lot of photo intense work these days and would like to have the ability to activate the "Generic Static Caption" command on all photographs in the layout. Currently, I have to go page by page and select all, then apply the "Generic Static Caption". For 50 or 60 pages, it eats up an awful lot of time.

 

Is there a keyboard shortcut or setting I'm missing? Are there scripts available that do this?

 

Your assistance is greatly appreciated.

TOPICS
EPUB , How to , Import and export , Performance , Print , Publish online , Scripting , Type , UXP Scripting

Views

163

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 1 Correct answer

Community Expert , Nov 23, 2022 Nov 23, 2022

Hi @ht354, I have written a couple of functions that will help you I think.

 

However, there's a lot you haven't specified, such as the contents of the caption. In my script example I've used the XMP "description" field, and the filename if that doesn't exist. You can adjust that, or derive your caption some other way. Or do you already have a text variable set up for the captions? If so, I can use that—let me know and I can adjust script. Note that this script applies to all graphics in the doc

...

Likes

Translate

Translate
Community Expert ,
Nov 23, 2022 Nov 23, 2022

Copy link to clipboard

Copied

Hi @ht354, I have written a couple of functions that will help you I think.

 

However, there's a lot you haven't specified, such as the contents of the caption. In my script example I've used the XMP "description" field, and the filename if that doesn't exist. You can adjust that, or derive your caption some other way. Or do you already have a text variable set up for the captions? If so, I can use that—let me know and I can adjust script. Note that this script applies to all graphics in the document, except if they are on pasteboard. Also the function accepts a parameter for Object Style name—I recommend setting all your caption textframes to an object style so you can control them all together. In my script example the object style is called "Caption" but you can change this.

- Mark

 

/**
 * @discussion https://community.adobe.com/t5/indesign-discussions/is-there-a-way-to-apply-generic-static-caption-to-all-graphic-containers/m-p/13367937#M503941
 */
function main() {

    var doc = app.activeDocument,
        graphics = doc.allGraphics;

    // add caption to each graphic in document
    for (var i = 0; i < graphics.length; i++) {

        var contents = (

            // try to get the graphic's xmp "description"
            getXMPProperty(graphics[i], 'description')

            // or fall back to the link filename
            || graphics[i].itemLink.name

        );

        addCaptionToGraphic(
            {
                doc: doc,
                graphic: graphics[i],
                contents: contents,
                objectStyleName: 'Caption',
                captionBoxHeight: 40, // pts
                groupCaptionWithGraphic: true,
            }
        );

    }


    /**
     * Adds a caption text frame
     * to the graphic.
     * @author m1b
     * @version 2022-11-24
     * Note: will use "No contents" if contents is empty.
     * @param {Object} options
     * @param {Graphic} options.graphic - an Indesign Graphic.
     * @param {String} options.contents - the text of the caption.
     * @param {String} [options.objectStyleName] - name of the object style to apply.
     * @param {Number|String} [options.captionBoxHeight] - height of the caption box (default: to fit).
     * @param {Boolean} [options.groupCaptionWithGraphic] - whether to group the caption with the graphic's frame.
     * @returns {TextFrame|Group} - the caption's text frame or, if grouped, the group.
     */
    function addCaptionToGraphic(options) {

        app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS;

        options = options || {};

        var doc = options.doc || app.activeDocument,
            graphic = options.graphic,
            contents = options.contents || 'No contents',
            objectStyle = getByName(doc.allObjectStyles, options.objectStyleName),
            captionBoxHeight = options.captionBoxHeight || 10,
            captionBox,
            frame = graphic.parent,
            b = frame.geometricBounds;

        if (graphic.parentPage == null)
            // don't process graphics on pasteboard
            return;

        // add a frame for the caption text
        captionBox = graphic.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 = String(contents);

        // expand to handle overflow
        var counter = 0;
        while (
            captionBox.overflows == true
            && counter++ < 1000
        )
            // add 5 pts height
            captionBox.resize(CoordinateSpaces.PARENT_COORDINATES, AnchorPoint.TOP_LEFT_ANCHOR, ResizeMethods.ADDING_CURRENT_DIMENSIONS_TO, [0, 5])

        if (
            objectStyle != undefined
            && objectStyle.isValid
        )
            // set captionBox's object style
            captionBox.applyObjectStyle(objectStyle);

        if (options.groupCaptionWithGraphic !== false)

            // group
            try {
                doc.groups.add([frame, captionBox]);
            } catch (error) { }

    };


    /**
     * Returns a graphic's XMP property contents.
     * Example XMP property names:
     *   author, copyrightInfoURL, copyrightNotice,
     *   copyrightStatus, creationDate, creator,
     *   description, documentTitle, format, jobName,
     *   keywords, modificationDate, serverURL
     * @author m1b
     * @version 2022-11-24
     * @param {Graphic} graphic - an Indesign Graphic.
     * @param {String} property - the desired XMP property name.
     * @returns {String}
     */
    function getXMPProperty(graphic, property) {

        if (!graphic.isValid)
            return;

        try {
            return graphic.itemLink.linkXmp[property];

        } catch (error) {
            /* metadata not available */
        }

    };


    /**
     * Returns a style matching name.
     * @author m1b
     * @version 2022-08-14
     * @param {Array<style>} styles - an array or collection of styles.
     * @param {String} name - the name to match.
     * @returns {style}
     */
    function getByName(styles, name) {

        for (var i = 0; i < styles.length; i++)
            if (styles[i].name == name)
                return styles[i];

    };


} // 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
Explorer ,
Nov 23, 2022 Nov 23, 2022

Copy link to clipboard

Copied

I follow the concept of your advice on using object styles for captions, but I'm unsure how you connect an object style with enabling Captions. I build the captions in Lightroom using jb Search+Replace and dump it all into the actual metadata Caption field which ImDesign interprets as the Description field. I have a paragraph style set and used for the captions embedded into each photograph. 

 

How do I attach an Object style to hundreds of potential Captions?

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
Community Expert ,
Nov 23, 2022 Nov 23, 2022

Copy link to clipboard

Copied

LATEST
How do I attach an Object style to hundreds of potential Captions?

 

This topic was about creating a static caption for your photos. If you want to apply an object style to the captions that the script creates, you can set the "objectStyleName" in the script (my example called it "Caption").

 

It sounds like you want something different? Have you tried the script as it is? It will make captions with the xmp "description" field, but you can change that. If you spell out exactly what you expect, and supply a small demo file (indesign and photo), that will be quickest way to get to the solution I think.

- 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