Links Rename Web Or Ebook

Script for Adobe InDesign

Rename and update links to names better suited to website and e-book use.

As a programmer of websites and e-books I have a distaste for filenames that are not HTML-friendly, most notably names that contain spaces, which are not legal in URLs and therefore are encoded to their hex ASCII value (%20). As e-books are basically a collection of HTML pages, the same applies. Even though the user never sees these URLs, I do while programming the work, and it annoys me. So I made the script to clean up the links before exporting to EPUB or as HTML text destined for a website.

  • Rename links and update
  • Adapt open source to customize or create other scripts
Download
Links Rename Web Or Ebook

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 the button to make a contribution of any amount. Thank 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 renamed to eliminate spaces and punctuation that would require URL encoding. This prevents URLs that look like ".../This%20is%20the%20File%20Name.jpg". Instead such a link becomes ".../this-is-the-file-name.jpg".

The script is open source so it may be altered to suit other needs. For example, if lowercase is not desired, remove the change to lowercase. Other file name transformations can be added or removed, including to replace specific characters for a unique situation.

Source code

(download button below)

/*

Links Rename Web Or Ebook
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.

*/

(function () {

    var title = "Links Rename Web Or Ebook";

    if (!/indesign/i.test(app.name)) {
        alert("Script for InDesign", title, false);
        return;
    }

    // Script variables.
    var count;
    var doc;
    var extension;
    var file;
    var fileNew;
    var fileVersion;
    var i;
    var ii;
    var link;
    var baseName;
    var nameChanged;
    var nameNew;
    var progress;

    // SETUP

    if (!app.documents.length) {
        alert("Open a document", title, false);
        return;
    }
    doc = app.activeDocument;

    // 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;
    };

    // EXECUTE

    // 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.
    count = 0;
    progress.display();
    try {
        progress.set(doc.links.length);
        for (i = 0; i < doc.links.length; i++) {
            link = doc.links[i];
            progress.display(link.name);
            file = new File(link.filePath);
            baseName = link.name.replace(/\.[^\.]*$/, "");
            extension = String(String(link.name.match(/\..*$/) || "").match(/[^\.]*$/) || "");
            nameChanged = baseName.toLowerCase();
            extension = extension.toLowerCase();
            // Replace punctuation with dash.
            nameChanged = nameChanged.replace(/[ ~`@#$%\^&\*()_=:;,\."'_~+<>\/\?\{\}\[\]\|\\]/g, "-");
            // Delete any leading dashes.
            nameChanged = nameChanged.replace(/^-/g, "");
            // Delete any leading underscores.
            nameChanged = nameChanged.replace(/^_/g, "");
            // Reduce double dashes to one.
            nameChanged = nameChanged.replace(/--/g, "-");
            // Did name change?
            if (nameChanged != baseName) {
                // Test if file with name exists.
                nameNew = nameChanged;
                fileVersion = 0;
                fileNew = new File(file.path + "/" + nameNew + "." + extension);
                while (fileNew.exists) {
                    // File exists. Add version suffix.
                    fileVersion++;
                    nameNew = nameChanged + "~" + fileVersion;
                    fileNew = new File(file.path + "/" + nameNew + "." + extension);
                }
                // Rename and relink.
                if (file.exists) {
                    progress.display(link.name + " -> " + nameNew + "." + extension);
                    // Rename with 'tmp-' prefix then without to
                    // ensure change to lowercase takes effect,
                    // in event it was the only change.
                    file.rename("tmp-" + nameNew + "." + extension);
                    file.rename(nameNew + "." + extension);
                    // 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();
        }
    } finally {
        progress.close();
    }
    alert(count + " links renamed", title, false);

})();
Many scripts are free to download thanks to the support of users. Help me keep developing new scripts by supporting my work. Click the button to make a contribution of any amount. Thank you.
Download
Links Rename Web Or Ebook

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.