Links Rename Selected

Script for Adobe InDesign

Rename and update selected link.

  • Ensures the new name does not already exist
  • Adapt open source to customize or create other scripts
Download
Links Rename Selected

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.

How-to Video

How to use the script

Select a placed graphic or its frame, then run the script. The script displays an input that defaults to the current filename. Revise the name as desired and click OK. The name does not include the file extension, which remains the same. If the revised name matches another file that already exists, the user is notified, and the file is not renamed. Otherwise the file is renamed, the placed graphic is relinked to the new filename, and the link is updated.

Source code

(download button below)

/*

Links Rename Selected
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 Rename Selected";

    var doc;
    var extension;
    var file;
    var fileNew;
    var link;
    var baseName;
    var nameNew;
    var selection;

    if (!app.documents.length) {
        alert("Open a document", title, false);
        return;
    }
    doc = app.activeDocument;
    selection = doc.selection[0];
    if (selection) {
        try {
            if (selection instanceof Rectangle) {
                link = selection.allGraphics[0].itemLink;
            } else {
                link = selection.itemLink;
            }
        } catch (_) {
            // Ignore.
        }
        if (link) {
            // Split filename into name and extension.
            baseName = link.name.replace(/\.[^\.]*$/, "");
            extension = String(String(link.name.match(/\..*$/) || "").match(/[^\.]*$/) || "");
            nameNew = prompt("Revise name as desired\n(extension remains the same)", baseName, title);
            if (nameNew && nameNew != baseName) {
                // Add back extension.
                nameNew += "." + extension;
                // Test if new name exists.
                fileNew = new File(new File(link.filePath).path + "/" + nameNew);
                if (fileNew.exists) {
                    alert(nameNew + " already exists", title, true);
                    return;
                }
                // Rename and relink.
                file = new File(link.filePath);
                file.rename(nameNew);
                link.relink(file);
                link.update();
            }
            return;
        }
    }
    alert("Select a placed graphic or its frame", title, false);

})();
× Stripe

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

Download
Links Rename Selected

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.