Links Remove Number Prefix

Script for Adobe InDesign

Many of my jobs are preparing photo books for the printer. For these projects it is common for content providers to organize images by chapter using numbered prefixes, and many times further numbering images to indicate the order images appear within chapters. This practice is an understandable means of organizing assets and certainly an aid to the designer when assembling the layout, but all too often many changes occur during design and images change order or are moved to other chapters, making the file naming misleading once design is complete and ready to prep for printing. Perhaps not a big deal, but it annoys me, and besides, the numbering at this late stage is excess. Once the layout is complete, the numbering is no longer needed, and for any images re-purposed for future projects, it is meaningless. So I made the script to strip this excess.

  • Remove number prefix from filenames and update links
  • Adapt open source to customize or create other scripts
Download
Links Remove Number Prefix

You decide. Reward the author an
amount the solution is worth to you.

How to use the script

The script has no interface other than alert on launch to warn that files on disk will be altered, and alert when complete.

All links in the active InDesign document are analyzed to find any that begin with numbers and optionally a single dot or lowercase letter. For example "15 the filename", "15.3 the filename", "18a the filename", "20.5b the filename". When such a pattern is detected, the prefix is removed. Next white space is trimmed from the begin and end of the filename (not including the file extension), and the new name is tested to ensure a file of the same name does not already exist. If a file does exist, a version number is appended to the name as a suffix, for example "~2" prior to the file extension. Finally, for any filenames that are modified, the placed graphic is then relinked to the new filename.

Source code

(download button below)

/*

Links Remove Number Prefix
Copyright 2021 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 = "Links Remove Number Prefix";

    var count = 0;
    var doc;
    var ext;
    var file;
    var fileNew;
    var fileVersion;
    var i;
    var ii;
    var link;
    var name;
    var nameChanged;
    var nameNew;

    if (!app.documents.length) {
        alert("Open a document.", title, false);
        return;
    }
    doc = app.activeDocument;
    // Last chance to backup data first.
    if (!confirm("WARNING!\nFiles on disk will be renamed and this cannot be undone. Make backup copies of files before proceeding. Are you sure you want to continue?", true, title)) {
        return;
    }
    // Proceed.
    progress(doc.links.length);
    for (i = 0; i < doc.links.length; i++) {
        link = doc.links[i];
        file = new File(link.filePath);
        progress.message(link.name);
        // Split link.name into base name and extension.
        name = link.name.replace(/.[^.]+$/, "");
        ext = link.name.replace(/^.*\./, "");
        // Remove leading digits, dots/digits, single lowercase letter
        // followed by more dots/digits, all followed by one or more spaces.
        // Handles names such as '15 the link.name', '15.1 ', '15.1a '
        nameChanged = name.replace(/^\d+[.\d]*[a-z]{0,1}[.\d]* */, "");
        // Remove leading spaces and/or dots.
        // Handles names such as '15 .1 the link.name' where prior changes leave dot
        // the first character (which would otherwise signal it is now a hidden file).
        // And names that begin with with a space character are not desirable.
        nameChanged = nameChanged.replace(/^[. ]*/, "");
        // Trim whitespace.
        nameChanged = nameChanged.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, "");
        // Did name change?
        if (nameChanged !== name) {
            // Test if file with name exists.
            nameNew = nameChanged;
            fileVersion = 0;
            fileNew = new File(file.path + "/" + nameNew + "." + ext);
            while (fileNew.exists) {
                // File exists. Add version suffix.
                fileVersion++;
                nameNew = nameChanged + "~" + fileVersion;
                fileNew = new File(file.path + "/" + nameNew + "." + ext);
            }
            // Rename and relink.
            if (file.exists) {
                file.rename(nameNew + "." + ext);
                // Loop through all graphics and relink.
                // Graphic could be placed more than once.
                for (ii = 0; ii < doc.links.length; ii++) {
                    if (doc.links[ii].name === link.name) {
                        doc.links[ii].relink(file);
                        doc.links[ii].update();
                        count++;
                    }
                }
            }
        }
        progress.increment();
    }
    progress.close();
    alert(count + " links renamed", title, false);

    function progress(steps) {
        var b;
        var t;
        var w;
        w = new Window("palette", "Progress", undefined, {
            closeButton: false
        });
        t = w.add("statictext");
        t.preferredSize = [450, -1]; // 450 pixels wide, default height.
        if (steps) {
            b = w.add("progressbar", undefined, 0, steps);
            b.preferredSize = [450, -1]; // 450 pixels wide, default height.
        }
        progress.close = function () {
            w.close();
        };
        progress.increment = function () {
            b.value++;
        };
        progress.message = function (message) {
            t.text = message;
            w.update();
        };
        w.show();
    }

})();
Download
Links Remove Number Prefix

License details included in download

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

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