Links Shorten Filenames

JavaScript 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 incredible 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
  • Open source
  • Adapt source code to create other scripts
Links Shorten Filenames screen
Download
Links Shorten Filenames

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

Instructions for use

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.

The script is open source so it may be altered to suit other needs.

Source code

(download button below)

/*

Links Shorten Filenames
Copyright 2019 William Campbell
All Rights Reserved
Questions, comments, or custom programming, contact:
    william@marspremedia.com
    willcampbell7@gmail.com
    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 doc;
    var ext;
    var file;
    var fileNew;
    var fileVersion;
    var i;
    var link;
    var maxLength = 31; // Default max filename length. Set as desired.
    var name;
    var nameEdit;
    var nameNew;

    if (!app.documents.length) {
        alert("Open a document.");
        return;
    }
    doc = app.activeDocument;
    maxLength = Number(prompt("Enter maximum filename length:", maxLength));
    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)) {
        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);
        // Spilt 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.
            nameEdit = name.slice(0, (maxLength - ext.length - 1));
            // Trim whitespace.
            nameEdit = nameEdit.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, "");
            // Did name change?
            if (nameEdit != name) {
                // Test if new name already exists.
                nameNew = nameEdit;
                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 = nameEdit.substring(0, nameEdit.length - 2) + "~" + fileVersion;
                    fileNew = new File(file.path + "/" + nameNew + "." + ext);
                }
                // Rename and relink.
                if (file.exists) {
                    file.rename(nameNew + "." + ext);
                    link.relink(file);
                    link.update();
                }
            }
        }
        progress.increment();
    }

    progress.close();
    alert("Links successfully renamed and relinked.");

    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 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.