Skip to main content
Known Participant
March 19, 2019
Question

relink images with different names

  • March 19, 2019
  • 3 replies
  • 4091 views

Hi everyone,

My first post here after doing a bit of research.

This thread comes close to what I'm after: relink images with different names

So, I have folder of pictures, each starting with a unique number:

1 fleur orange.tif

45 arbre vert.tif

678 cactus bleu.tif

1001 champignon violet.itf

For stability reasons I had to clean the names of these files. I removed accented letters, periods and so on, but always kept the unique starting number.

So, I'm looking for a script to relink them using only that (pattern) number.

Thanks in advance for your help,

Mathieu Christe

This topic has been closed for replies.

3 replies

willcampbell7
Legend
March 20, 2019

Do you have the original links, before they were renamed? Because it is easier to fix the file names and relink in InDesign at the same time, which is quite easy to do in script. If you don't have the originally named files and have changed all the names, I'm afraid it might be a manual process to relink, unless there is a clearly defined method used to rename the files, in which case a script could be devised to duplicate the method used to figure out name changes, and by that, fetch the new file name and relink. That's a little more work.

But if you do have the files as originally named, below is an example of a simple script that could be modified easily for your purpose. This script is for making all links web/ebook legal (all lowercase and all spaces/punctuation changed to dash character). This particular script is so my ebook conversions or other export to HTML doesn't have links that end up as URLs looking like "This%20Is%20The%File%20Name.jpg". Instead, the script converts the actual file names and relinks in InDesign so the same example ends up "this-is-the-file-name.jpg"

The area to pay attention to is "FILE NAME TRANSFORMATIONS". This area is where names are changed. Code before and after are to collect links and update them. This area you could modify to achieve the name changes you desire, whatever it may be. Just be careful -- this renames files on disk. Make a backup copy of files before trying it. If something goes wrong, the script could leave you with a folder full of screwed up file names. Use with care.

/*

Links Rename Web or Ebook

Copyright 2019 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 doc;

    var ext;

    var file;

    var i;

    var link;

    var name;

    if(!app.documents.length) {

        alert("Open a document.");

        return;

    }

    doc = app.activeDocument;

    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;

    };

    progress(doc.links.length);

    for (i = 0; i < doc.links.length; i++) {

        link = doc.links;

        progress.message(link.name);

        file = new File(link.filePath);

        name = link.name.replace(/.[^.]+$/, "");

        ext = link.name.replace(/^.*\./, "");

        // FILE NAME TRANSFORMATIONS

        // Make lowercase.

        name = name.toLowerCase();

        ext = ext.toLowerCase();

        // Replace punctuation with dash.

        name = name.replace(/[ ~`@#$%\^&\*()_=:;,\."'_~+<>\/\?\{\}\[\]\|\\]/g, "-");

        // Delete any leading dashes.

        name = name.replace(/^-/g, "");

        // Delete any leading underscores.

        name = name.replace(/^_/g, "");

        // Reduce double dashes to one.

        name = name.replace(/--/g, "-");

        // END FILE NAME TRANSFORMATIONS

        if (file.exists) {

        // Rename the file on disk.

            file.rename(name + "." + ext);

            // Then relink to renamed file and update.

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

    }

})();

William Campbell
Known Participant
March 21, 2019

Before clean-renaming all filenames “manually” (using a find-change tool), I would have hoped for such a script to be in my folder. Thanks a lot for sharing, very useful!

Known Participant
March 20, 2019

You could use this script written by Kasyan
Batch Rename and Relink

Known Participant
March 21, 2019

Thanks for this suggestion, I'll give it a try as it can fit another project.

Community Expert
March 19, 2019

Hi Mathieu,

Try the following code and see if it works for your needs. I have modified the correct answer from the thread you posted.

var

  sourceFolder = Folder.selectDialog("Show me a source folder"),

  mLinks = app.activeDocument.links.everyItem().getElements(),

  cLink, cFile;

while ( cLink = mLinks.pop() )

{

  //Get the number in this fileName

  var num = cLink.name.match(/(\d+).*/)[1]

  var dest = File(sourceFolder.getFiles(num+"*"))

  cLink.relink(dest);

}

-Manan

-Manan
Known Participant
March 19, 2019

Hi Manan,

Thank you for your help.

Unfortunately I get an error message after running the bit of code.

It seems it can't find the folder:

JavaScript Error!

Error Number: 48

Error String: Cannot find the folder “/Users/christem/Documents/CJB-publications/Flore du Tchad/images/dessins/648 Elionurus royleanus Nees ex A Rich.tif,~/Documents/CJB-publications/Flore du Tchad/images/dessins/661 Uvaria chamae P de Beauv.tif, …

Engine: main

File: /Users/christem/Library/Preferences/Adobe InDesign/Version 14.0/en_US/Scripts/Scripts Panel/RelinkPictures.jsx

Line: 11

Source: cLink.relink(dest);  

Community Expert
March 19, 2019

Are these paths valid? Is this is the folder that you are choosing when prompted?

-Manan