Label Graphic Links

Script for Adobe InDesign

Add a label to placed graphics that lists the file name.

  • Label all graphics or a selected graphic
  • Choose layer, paragraph style, and object style
  • Adapt open source to customize or create other scripts
Download
Label Graphic Links

FREE to download
Please make a contribution

Many scripts are free to download thanks to the support of users. Help me keep developing new scripts by supporting my work. Click any one of the buttons below to make a contribution of any amount. Thank you.

× Stripe

Choose an amount to contribute.
Thank you for supporting my work.

Script adds label with file name to placed graphics as shown.

THIS IS AN AD. Beware of anything in the ad artwork that resembles a 'DOWNLOAD' button meant to confuse you into thinking it downloads something from this website. It does not.

How to use the script

Set desired options and click the OK button to proceed. Labels are added in the corner of placed graphics, on the selected layer and styled as described below.

Layer — the layer to which the labels are added. A choice of layer is required. It is recommended to create a layer for the labels before launching the script so the labels can be easily hidden, or the layer set to non-printing.

Paragraph style — the paragraph style assigned to the text of each label. Choose an existing style or create a new one for label text before launching the script. Or choose the default, [No Paragraph Style], which does not assign any paragraph style. In that case, the text defaults to Arial, 9 points, swatch Black. For other defaults, edit the script code.

Object style — the object style assigned to the frame of each label. Choose an existing style or create a new one for label frames before launching the script. Or choose the default, [None], which does not assign any object style. In that case, the frame fill color is set to 100% Yellow, the swatch for which is created if it does not exist. Whether choosing a style or none, label frames are set to auto-sizing height and width. For other defaults, edit the script code.

Source code

(download button below)

/*

Label Graphic Links
Copyright 2022 William Campbell
All Rights Reserved
https://www.marspremedia.com/contact

Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

*/

//@target indesign

(function () {

    var title = "Label Graphic Links";

    // Script variables.
    var characterStyleNone;
    var count;
    var doc;
    var error;
    var font;
    var layer;
    var layerNames;
    var objectStyle;
    var objectStyles;
    var paragraphStyle;
    var paragraphStyles;
    var progress;
    var swatchFrame;
    var swatchText;

    // Reusable UI variables.
    var g; // group
    var gc1; // group (column)
    var gc2; // group (column)
    var p; // panel
    var w; // window

    // Permanent UI variables.
    var btnCancel;
    var btnOk;
    var listLayers;
    var listObjectStyles;
    var listParagraphStyles;
    var rbAll;
    var rbSelected;

    // SETUP

    try {
        // Check for valid doc.
        if (!app.documents.length) {
            alert("Open a document", title, false);
            return;
        }
        doc = app.activeDocument;
        characterStyleNone = doc.characterStyles[0];
        // Make list of layers.
        layerNames = doc.layers.everyItem().name;
        // Load paragraph styles.
        paragraphStyles = getParagraphStyles(doc);
        // Load object styles.
        objectStyles = getObjectStyles(doc);
    } catch (e) {
        // Setup failed.
        alert("An error has occurred.\nLine " + e.line + ": " + e.message, title, true);
        return;
    }

    // CREATE PROGRESS WINDOW

    progress = new Window("palette", "Progress", undefined, {
        "closeButton": false
    });
    progress.t = progress.add("statictext");
    progress.t.preferredSize = [450, -1];
    progress.b = progress.add("progressbar");
    progress.b.preferredSize = [450, -1];
    progress.display = function (message) {
        message && (this.t.text = message);
        this.show();
        this.update();
    };
    progress.increment = function () {
        this.b.value++;
    };
    progress.set = function (steps) {
        this.b.value = 0;
        this.b.minvalue = 0;
        this.b.maxvalue = steps;
    };

    // CREATE USER INTERFACE

    w = new Window("dialog", title);
    w.alignChildren = "fill";
    // Panel.
    p = w.add("panel");
    // Group of 2 columns.
    g = p.add("group");
    // Groups, columns 1 and 2.
    gc1 = g.add("group");
    gc1.orientation = "column";
    gc1.alignChildren = "left";
    gc2 = g.add("group");
    gc2.orientation = "column";
    gc2.alignChildren = "left";
    // Rows.
    gc1.add("statictext", undefined, "Graphics:").preferredSize = [-1, 23];
    g = gc2.add("group");
    g.margins = [0, 3, 0, -3];
    rbAll = g.add("radiobutton", undefined, "All");
    rbAll.preferredSize = [-1, 23];
    rbSelected = g.add("radiobutton", undefined, "Selected");
    rbSelected.preferredSize = [-1, 23];
    gc1.add("statictext", undefined, "Layer:").preferredSize = [-1, 23];
    listLayers = gc2.add("dropdownlist", undefined, layerNames);
    listLayers.preferredSize = [150, 23];
    gc1.add("statictext", undefined, "Paragraph style:").preferredSize = [-1, 23];
    listParagraphStyles = gc2.add("dropdownlist");
    listParagraphStyles.preferredSize = [150, 23];
    gc1.add("statictext", undefined, "Object style:").preferredSize = [-1, 23];
    listObjectStyles = gc2.add("dropdownlist");
    listObjectStyles.preferredSize = [150, 23];
    // Action Buttons.
    g = w.add("group");
    g.alignment = "center";
    btnOk = g.add("button", undefined, "OK");
    btnCancel = g.add("button", undefined, "Cancel");
    // Panel Copyright.
    p = w.add("panel");
    p.add("statictext", undefined, "Copyright 2022 William Campbell");

    // SET DEFAULT VALUES / RESTORE LAST VALUES USED.
    // MUST PRECEDE UI EVENT HANDLERS SO THEY DON'T FIRE WHILE SETTING VALUES.

    // Load lists and set default.
    (function () {
        var i;
        // Layers.
        listLayers.selection = 0;
        // Paragraph styles.
        for (i = 0; i < paragraphStyles.length; i++) {
            listParagraphStyles.add("item", paragraphStyles[i].name);
        }
        listParagraphStyles.selection = 0;
        // Object styles.
        for (i = 0; i < objectStyles.length; i++) {
            listObjectStyles.add("item", objectStyles[i].name);
        }
        listObjectStyles.selection = 0;
    })();

    if (doc.selection.length) {
        // Something is selected.
        rbSelected.enabled = true;
        rbAll.value = false;
        rbSelected.value = true;
    } else {
        rbSelected.enabled = false;
        rbAll.value = true;
        rbSelected.value = false;
    }

    // UI ELEMENT EVENT HANDLERS

    btnOk.onClick = function () {
        w.close(1);
    };

    btnCancel.onClick = function () {
        w.close(0);
    };

    // DISPLAY THE DIALOG

    if (w.show() == 1) {
        try {
            app.doScript(process, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, title);
            alert(count + " graphic links labeled", title, false);
        } catch (e) {
            if (!error) {
                error = e;
            }
            alert("An error has occurred.\nLine " + error.line + ": " + error.message, title, true);
        }
    }

    //====================================================================
    //               END PROGRAM EXECUTION, BEGIN FUNCTIONS
    //====================================================================

    function getListSelection(uiList, list) {
        var i;
        var name;
        if (uiList.selection) {
            name = uiList.selection.text;
            for (i = 0; i < list.length; i++) {
                if (list[i].name == name) {
                    return list[i];
                }
            }
        }
        return null;
    }

    function getObjectStyles(o) {
        var a = [];
        var i;
        // Root styles.
        for (i = 0; i < o.objectStyles.length; i++) {
            a.push(o.objectStyles[i]);
        }
        // Style groups.
        for (i = 0; i < o.objectStyleGroups.length; i++) {
            // Recursive; calls itself.
            a = a.concat(getObjectStyles(o.objectStyleGroups[i]));
        }
        return a;
    }

    function getParagraphStyles(o) {
        var a = [];
        var i;
        // Root styles.
        for (i = 0; i < o.paragraphStyles.length; i++) {
            a.push(o.paragraphStyles[i]);
        }
        // Style groups.
        for (i = 0; i < o.paragraphStyleGroups.length; i++) {
            // Recursive; calls itself.
            a = a.concat(getParagraphStyles(o.paragraphStyleGroups[i]));
        }
        return a;
    }

    function process() {
        var graphic;
        var graphics;
        var i;
        var link;
        var selection;
        app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS;
        try {
            layer = doc.layers.itemByName(listLayers.selection.text);
            if (listParagraphStyles.selection != 0) {
                paragraphStyle = getListSelection(listParagraphStyles, paragraphStyles);
            } else {
                font = app.fonts.itemByName("Arial\tRegular");
                try {
                    font.fullName;
                } catch (_) {
                    // Couldn't get font by name. Use font from [Basic Paragraph]
                    font = paragraphStyles[1].appliedFont;
                }
                swatchText = doc.swatches.itemByName("Black");
                if (!swatchText.isValid) {
                    // Couldn't get swatch by name. Try index 3, usual position for 'Black'.
                    swatchText = doc.swatches[3];
                }
            }
            if (listObjectStyles.selection != 0) {
                objectStyle = getListSelection(listObjectStyles, objectStyles);
            } else {
                swatchFrame = doc.swatches.itemByName("C=0 M=0 Y=100 K=0");
                if (!swatchFrame.isValid) {
                    // Couldn't get swatch by name. Create it.
                    swatchFrame = doc.colors.add();
                    swatchFrame.name = "C=0 M=0 Y=100 K=0";
                    swatchFrame.space = ColorSpace.CMYK;
                    swatchFrame.colorValue = [0, 0, 100, 0];
                    swatchFrame.model = ColorModel.PROCESS;
                }
            }
            if (rbAll.value) {
                // Label all graphics.
                try {
                    count = 0;
                    graphics = doc.allGraphics;
                    progress.set(graphics.length);
                    for (i = 0; i < graphics.length; i++) {
                        graphic = graphics[i];
                        try {
                            link = graphic.itemLink;
                            if (link) {
                                progress.display(link.name);
                                if (processGraphic(graphic, link.name)) {
                                    count++;
                                }
                            }
                        } finally {
                            progress.increment();
                        }
                    }
                } finally {
                    progress.close();
                }
            } else {
                // Label selected graphic(s).
                count = 0;
                for (i = 0; i < doc.selection.length; i++) {
                    selection = doc.selection[i];
                    if (!(selection instanceof Rectangle || selection.parent instanceof Rectangle)) {
                        // Selection is not a graphic or its frame.
                        continue;
                    }
                    if (selection instanceof Rectangle) {
                        graphic = selection.allGraphics[0];
                    } else {
                        graphic = selection;
                    }
                    link = graphic.itemLink;
                    if (link) {
                        processGraphic(graphic, link.name);
                        count++;
                    }
                }
            }
        } catch (e) {
            error = e;
            throw e;
        }
    }

    function processGraphic(graphic, name) {
        var gb; // geometric bounds
        var parent;
        var textFrame;
        // Test if graphic is overset.
        try {
            gb = graphic.parent.geometricBounds;
        } catch (_) {
            // Probably overset.
            // Ignore.
            return false; // Failed
        }
        // Get parent page or spread.
        parent = graphic.parentPage || graphic.parent.parent;
        // Create and configure text frame.
        textFrame = parent.textFrames.add();
        textFrame.itemLayer = layer;
        // Character style 'None' must precede adding contents.
        textFrame.insertionPoints[0].appliedCharacterStyle = characterStyleNone;
        textFrame.contents = name;
        // Set styles.
        if (paragraphStyle) {
            textFrame.paragraphs[0].appliedParagraphStyle = paragraphStyle;
        } else {
            // Style paragraph manually.
            textFrame.paragraphs[0].appliedFont = font;
            textFrame.paragraphs[0].fillColor = swatchText;
            textFrame.paragraphs[0].pointSize = 9;
        }
        // These paragraph properties always set manually.
        textFrame.paragraphs[0].justification = Justification.LEFT_ALIGN;
        textFrame.paragraphs[0].alignToBaseline = false;
        if (objectStyle) {
            textFrame.appliedObjectStyle = objectStyle;
        } else {
            // Style frame manually.
            textFrame.fillColor = swatchFrame;
            textFrame.textFramePreferences.verticalJustification = VerticalJustification.CENTER_ALIGN;
            textFrame.textFramePreferences.insetSpacing = 2;
        }
        // These frame properties always set manually.
        textFrame.textFramePreferences.autoSizingReferencePoint = AutoSizingReferenceEnum.TOP_LEFT_POINT;
        textFrame.textFramePreferences.autoSizingType = AutoSizingTypeEnum.HEIGHT_AND_WIDTH;
        textFrame.textFramePreferences.useNoLineBreaksForAutoSizing = true;
        textFrame.textFramePreferences.ignoreWrap = true;
        textFrame.textWrapPreferences.textWrapMode = TextWrapModes.NONE;
        // Move text frame to top-left corner of graphic.
        textFrame.move([gb[1], gb[0]]);
        return true; // Success
    }

})();
× Stripe

Choose an amount to contribute.
Thank you for supporting my work.

Download
Label Graphic Links

License details included in download

For help installing scripts, see How to Install and Use Scripts in Adobe Creative Cloud Applications.

Also available for hire to program custom solutions. Contact William for more information.

IMPORTANT: by downloading any of the scripts on this page you agree that the software is provided without any warranty, express or implied. USE AT YOUR OWN RISK. Always make backups of important data.