Links Shorten Filenames

Script for Adobe InDesign

More than once I’ve encountered projects using image filenames as a means to convey caption text. This practice leads to some incredibly long filenames. A recent project had filenames so long that once down a few subfolders, the path length exceeded the maximum my Windows server could handle. It was a mess to clean up, which became the reason to create this script.

Or a project’s filenames are long for some other reason. Whether intended as caption text or not, excessively long filenames can be a pain, for the file system, applications, and operators trying to identify one file from another. This script truncates actual filenames (as each appears in Finder or File Explorer) to a reasonable length specified by the user, then update the links in the InDesign layout to the new filenames, all in one operation. As can be imagined, accomplishing this manually, link-by-link, would be time-consuming and error-prone.

In the case of caption text in filenames, see the Photoshop script Filename to Description. Before shortening filenames with this script, use the related Photoshop script to copy filenames to each file’s metadata, and preserve the caption text before it is truncated.

Also consider the related script Links Remove Number Prefix. For files labeled with numbered prefixes (i.e. chapter numbers or image order), it may be desirable to strip this excess first, then further shorten the filenames.

  • Truncate filenames to specified maximum and update links
  • Auto-resolves duplicate filenames
  • Adapt open source to customize or create other scripts
Links Shorten Filenames
Download
Links Shorten Filenames

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

How-to Video

How to use the script

The user is prompted to enter the maximum length for filenames. This does not include the extension, which remains the same. All links are examined and any filenames that exceed the specified value are truncated, then the new name is tested to ensure a file of the same name does not already exist. If a file does exist, the name is further shortened to allow for a version number 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 Shorten Filenames
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 Shorten Filenames";

    var count = 0;
    var doc;
    var ext;
    var file;
    var fileNew;
    var fileVersion;
    var i;
    var ii;
    var link;
    var maxLength = 31; // Default max filename length. Set as desired.
    var name;
    var nameChanged;
    var nameNew;

    if (!app.documents.length) {
        alert("Open a document.", title, false);
        return;
    }
    doc = app.activeDocument;
    maxLength = Number(prompt("Enter maximum filename length:", maxLength, title));
    if (!maxLength) {
        // User cleared value or pressed ESC.
        return;
    }
    // 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];
        progress.message(link.name);
        file = new File(link.filePath);
        // Split filename into name and extension.
        name = link.name.replace(/.[^.]+$/, "");
        ext = link.name.replace(/^.*\./, "");
        // Transformations to truncate long filenames.
        if (link.name.length > maxLength) {
            // Truncate name portion to maxLength less extension length.
            nameChanged = name.slice(0, (maxLength - ext.length - 1));
            // Trim whitespace.
            nameChanged = nameChanged.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, "");
            // Did name change?
            if (nameChanged !== name) {
                // Test if new name already exists.
                nameNew = nameChanged;
                fileVersion = 1;
                fileNew = new File(file.path + "/" + nameNew + "." + ext);
                while (fileNew.exists) {
                    // File exists. Add version suffix.
                    // Shorten nameShort by 2 characters to allow for suffix.
                    fileVersion++;
                    nameNew = nameChanged.substring(0, nameChanged.length - 2) + "~" + fileVersion;
                    fileNew = new File(file.path + "/" + nameNew + "." + ext);
                }
                // Rename and relink.
                if (file.exists) {
                    // Rename file.
                    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];
        if (steps) {
            b = w.add("progressbar", undefined, 0, steps);
            b.preferredSize = [450, -1];
        }
        progress.close = function () {
            w.close();
        };
        progress.increment = function () {
            b.value++;
        };
        progress.message = function (message) {
            t.text = message;
            w.update();
        };
        w.show();
    }

})();
Download
Links Shorten Filenames

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.