Links Rename Selected

JavaScript for Adobe InDesign

Rename and update selected link.

  • Ensures the new name does not already exist
  • Open source
  • Adapt source code to create other scripts
Links Rename Selected screen
Download
Links Rename Selected

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

Instructions for use

Select an image or its frame then launch the script. If an image or its frame is not the current selection, the user is prompted to select an image or its frame. Once launched, the script displays an input prompt 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 link that already exists, the user is notified and the file is not renamed. Otherwise the file is renamed and the placed graphic is relinked to the new filename provided.

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

Source code

(download button below)

/*

Links Rename Selected
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 link;
    var name;
    var nameNew;
    var objectType;
    var selection;

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

    if (doc.selection[0]) {
        selection = doc.selection[0];
        objectType = selection.constructor.name;
        if (objectType == "Image" || objectType == "Rectangle") {
            // Selection is or could be an image.
            if (objectType == "Image" || selection.allGraphics[0].constructor.name == "Image") {
                // Selection is image.
                if (objectType == "Image") {
                    link = selection.itemLink;
                } else {
                    link = selection.allGraphics[0].itemLink;
                }
                // Spilt filename into name and extension.
                name = link.name.replace(/.[^.]+$/, "");
                ext = link.name.replace(/^.*\./, "");
                nameNew = prompt("Revise name as desired\n(extension remains the same)", name);
                if (nameNew && nameNew != name) {
                    // Add back extension.
                    nameNew += "." + ext;
                    // Test if new name exists.
                    fileNew = new File(new File(link.filePath).path + "/" + nameNew);
                    if (fileNew.exists) {
                        alert(nameNew + " already exists");
                        return;
                    }
                    // Rename and relink.
                    file = new File(link.filePath);
                    file.rename(nameNew);
                    link.relink(file);
                    link.update();
                }
                return;
            }
        }
    }
    alert("Select an image or its frame.");

})();
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.

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.