Skip to main content
dublove
Legend
August 3, 2026
Answered

Why did the caption generated by this script change to 'Default'?

  • August 3, 2026
  • 11 replies
  • 121 views

 Hi ​@m1b 

Long time no see, I miss you very much.

The original post cannot be replied to

Today I came across several strange pictures, some of which had their captions changed to 'Default' after using this script.
Some of them are errors below the prompt. I guess they are not recognizing the image, or the image is outside the layout, or there are locked or hidden images?
Also, can we avoid grouping images and captions together? Because this feature is actually not practical.

 

 

Here is the code (unchanged)

/**
* @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
main();

 

    Correct answer m1b

    @dublove Did you want more information about this? I had a look at the code and it was missing a few guards—I’ve added a few. There are two issues:

    (1) Your test document has 30 empty graphics that makes the original script very slow.

    (2) As Rob said, the metadata is “default”. I don’t know if some software puts “default” but I’ve put a guard in to ignore it so at least you get the file name.

    This script works on the selection but you can give it any array of items.

    – Mark

    /**
    * @version 2026-08-22
    * @discussion https://community.adobe.com/t5/indesign-discussions/is-there-a-way-to-apply-generic-static-caption-to-all-graphic-containers/m-p/13367937
    * @discussion https://community.adobe.com/questions-671/why-did-the-caption-generated-by-this-script-change-to-default-1635036
    */
    function main() {

    if (
    0 === app.documents.length
    || 0 === app.activeDocument.selection.length
    )
    return alert('Please select a graphic frame and try again.');

    var doc = app.activeDocument;
    var items = doc.selection;

    // add caption to each graphic in document
    for (var i = items.length - 1; i >= 0; i--) {
    var item = items[i];

    if (
    !item.hasOwnProperty('graphics')
    || 0 === item.graphics.length
    )
    continue;

    var graphic = item.graphics[0];

    // try to get the graphic's xmp "description"
    var contents = getXMPProperty(graphic, 'description');

    if (
    (!contents || 'default' === contents)
    && graphic.itemLink
    )
    // or fall back to the link filename
    contents = graphic.itemLink.name;

    addCaptionToGraphic(
    {
    doc: doc,
    graphic: graphic,
    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;
    var graphic = options.graphic;
    var contents = options.contents || 'No contents';
    var objectStyle = getByName(doc.allObjectStyles, options.objectStyleName);
    var captionBoxHeight = options.captionBoxHeight || 10;
    var captionBox;
    var frame = graphic.parent;
    var 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.
    * @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, 'Create Captions');

     

    11 replies

    m1b
    Community Expert
    m1bCommunity ExpertCorrect answer
    Community Expert
    August 22, 2026

    @dublove Did you want more information about this? I had a look at the code and it was missing a few guards—I’ve added a few. There are two issues:

    (1) Your test document has 30 empty graphics that makes the original script very slow.

    (2) As Rob said, the metadata is “default”. I don’t know if some software puts “default” but I’ve put a guard in to ignore it so at least you get the file name.

    This script works on the selection but you can give it any array of items.

    – Mark

    /**
    * @version 2026-08-22
    * @discussion https://community.adobe.com/t5/indesign-discussions/is-there-a-way-to-apply-generic-static-caption-to-all-graphic-containers/m-p/13367937
    * @discussion https://community.adobe.com/questions-671/why-did-the-caption-generated-by-this-script-change-to-default-1635036
    */
    function main() {

    if (
    0 === app.documents.length
    || 0 === app.activeDocument.selection.length
    )
    return alert('Please select a graphic frame and try again.');

    var doc = app.activeDocument;
    var items = doc.selection;

    // add caption to each graphic in document
    for (var i = items.length - 1; i >= 0; i--) {
    var item = items[i];

    if (
    !item.hasOwnProperty('graphics')
    || 0 === item.graphics.length
    )
    continue;

    var graphic = item.graphics[0];

    // try to get the graphic's xmp "description"
    var contents = getXMPProperty(graphic, 'description');

    if (
    (!contents || 'default' === contents)
    && graphic.itemLink
    )
    // or fall back to the link filename
    contents = graphic.itemLink.name;

    addCaptionToGraphic(
    {
    doc: doc,
    graphic: graphic,
    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;
    var graphic = options.graphic;
    var contents = options.contents || 'No contents';
    var objectStyle = getByName(doc.allObjectStyles, options.objectStyleName);
    var captionBoxHeight = options.captionBoxHeight || 10;
    var captionBox;
    var frame = graphic.parent;
    var 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.
    * @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, 'Create Captions');

     

    dublove
    dubloveAuthor
    Legend
    August 22, 2026

    Hi ​@m1b 

    Thank you.
    I commented a few sentences, and it seems like I haven't noticed any abnormalities in the past few days.
    I will use it this way for now, and if there are any issues, I will replace it with your current version.

                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

    );

     

    dublove
    dubloveAuthor
    Legend
    August 4, 2026

    Hi ​@rob day 

    Can you help me take a look?

    It's been a long time since I last saw m1b, his code architecture is too advanced. There are many places that I cannot understand.

    Thank you very much.

    rob day
    Community Expert
    Community Expert
    August 4, 2026

    Sorry, I also get an error and don’t understand the code. Looks like mlb is not using the app.menuActions.itemByName("Generate Live Caption") menuAction  I used in my example