• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

relink images with different names

Community Beginner ,
Mar 19, 2019 Mar 19, 2019

Copy link to clipboard

Copied

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

TOPICS
Scripting

Views

2.2K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 19, 2019 Mar 19, 2019

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 19, 2019 Mar 19, 2019

Copy link to clipboard

Copied

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);  

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 19, 2019 Mar 19, 2019

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 19, 2019 Mar 19, 2019

Copy link to clipboard

Copied

Yes, I'm targeting the folder “dessins”. Here's the path I get with “Get info” when I select that folder: Macintosh HD⁩ ▸ ⁨Users⁩ ▸ ⁨christem⁩ ▸ ⁨Documents⁩ ▸ ⁨CJB-publications⁩ ▸ ⁨Flore du Tchad⁩ ▸ ⁨images⁩

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 19, 2019 Mar 19, 2019

Copy link to clipboard

Copied

Hi Mathieu,

Try the following

function getMyFiles(num)

{

    return function(f)

    {

          var regEx = new RegExp("^" + num + "\\D+")

          return regEx.test(f.displayName)

    }

}

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(getMyFiles(num)))

      cLink.relink(dest);

}

-Manan

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 19, 2019 Mar 19, 2019

Copy link to clipboard

Copied

Hi again,

It seems to find its way now but I get another error message:

Error Number: 21

Error String: null is not an object

Line: 18

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 19, 2019 Mar 19, 2019

Copy link to clipboard

Copied

The issue might be that you have images in the document that don't confirm with the naming convention that you mentioned. Replace the code in the while loop with the following

var num = cLink.name.match(/^(\d+).+/)

if(num)

{

    var dest = File(sourceFolder.getFiles(getMyFiles(num[1])))

    cLink.relink(dest);

}

-Manan

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 19, 2019 Mar 19, 2019

Copy link to clipboard

Copied

Sorry about that and for the trouble, I forgot to mention that some filenames don't have the starting number pattern.

I've also realised that some files have been removed (pictures that didn't end up being selected) according to this new error message.

Error Number: 29446

Error String: Either the files does not exist, you do not have permission, or the file may be in use by another application

Line: 22

Source:  cLink.relink(dest);

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 19, 2019 Mar 19, 2019

Copy link to clipboard

Copied

Try this modified code

function getMyFiles(num)

{

    return function(f)

    {

          var regEx = new RegExp("^" + num + "\\D+")

          return regEx.test(f.displayName)

    }

}

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+).+/)

  if(num)

  {

        var f = sourceFolder.getFiles(getMyFiles(num[1]))

        var dest = File(f)

        if(dest.exists)

            cLink.relink(dest);

  }

}

-Manan

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 20, 2019 Mar 20, 2019

Copy link to clipboard

Copied

Thank you for this amended script.

No more error messages but when I select the folder with the images, after a few seconds the spinning ball appears and InDesign CC (not responding). I tried with reduced document (12 pp. instead of 800) with 28 missing links and it worked (missed one link though).

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 20, 2019 Mar 20, 2019

Copy link to clipboard

Copied

Update! Damn, this is endless, after going through the whole list and manually relinking the obvious troublemakers (188A for example) I've just spotted that some files share the very same starting number.

So I presume your script doesn't like it since I didn't forsee that possible trap, sorry about that.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 20, 2019 Mar 20, 2019

Copy link to clipboard

Copied

So i suppose we have hit a bottleneck to automate this. If you can identify rules to find which file to use in which case then we can modify the script, but we would need clear and defined rules.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 21, 2019 Mar 21, 2019

Copy link to clipboard

Copied

I agree. I should have looked more closely at the filenames to identify the exceptions to be taking care of. To make it simple, and avoid stealing more of your time, I'll relink manually all exceptions and then run your script. Many thanks for your help!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 20, 2019 Mar 20, 2019

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 21, 2019 Mar 21, 2019

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Mar 20, 2019 Mar 20, 2019

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 21, 2019 Mar 21, 2019

Copy link to clipboard

Copied

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!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 13, 2022 Jun 13, 2022

Copy link to clipboard

Copied

LATEST

I'm very new to running scripts - If I wanted to batch rename my linked files from er to elr could I use this code but modified (see below)? I'm getting an error about syntax when I try to run it though - any advice?

 

/

 


#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

        // Replace er with elr.

        name = name.replace(er, “elr”);

        // 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();

    }

 


})();

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines