Skip to main content
Community Expert
March 31, 2022
Answered

Relink images to similar file names

  • March 31, 2022
  • 2 replies
  • 1235 views

Hey

 

Looking for a way to relink images to a folder

I receive the files with the images embeded.

 

Sometimes the images in the links are ending with _01 for some reason or another.

I need to ignore the end string of the underscore digits to the end if possible.

And link to my images in a folder.

 

I found another script but have no idea how to amend it.

 

https://community.adobe.com/t5/indesign-discussions/a-script-to-batch-rename-and-relink-images/m-p/6894582#M283124

 

Thanks

 

This topic has been closed for replies.
Correct answer rob day

Huzah!

 

That seems to work - just 2 small things.

  1. It went through links one by one - confirming each one - anyway it can do it without confirming each one?
  2. And - is it possible just on the selected images in the links panel?

 

 


I’m not getting any dialogs, but this should prevent them:

 

var f = Folder.selectDialog("Select the folder");
var lks = app.documents[0].links;
var n, nn, na;
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.NEVER_INTERACT;
for (var i = 0; i < lks.length; i++){
    if (lks[i].status == LinkStatus.LINK_EMBEDDED) {
        n = lks[i].name;
        na = n.split( "." )
        nn = lks[i].name.replace(/_\d+(\.[^\.]+)$/, "."+ na[na.length-1])
        lks[i].unembed(f);
        File(lks[i].filePath).rename(nn);
        lks[i].relink(File(f+"/"+nn))
    } 
};   
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.INTERACT_WITH_ALL;

 

 

I don’t think there is a way to get a Link panel selection. You could do it via a range of pages if that helps.

2 replies

rob day
Community Expert
Community Expert
April 5, 2022

Hi Eugene, another approach would be to unembed the emedded links, rename them and relink to the renamed files. This asks for a link folder and unembeds 

 

 

 

 

var f = Folder.selectDialog("Select the links folder");
var lks = app.documents[0].links;
var n, nn;

for (var i = 0; i < lks.length; i++){
    if (lks[i].status == LinkStatus.LINK_EMBEDDED) {
        n = lks[i].name;
        nn = lks[i].name.replace(/_\d+(\.[^\.]+)$/, n.substring(n.length-4))
        lks[i].unembed(f);
        File(lks[i].filePath).rename(nn);
        lks[i].relink(File(f+"/"+nn))
    } 
};   

 

 

 

 

 

Community Expert
April 5, 2022

Thanks, I'll give it a go in the morning. 

Peter Kahrel
Community Expert
Community Expert
April 12, 2022

No that's great. Thanks so much. I'll try it out and see how it goes. 


Just a quick note to confirm that it's not possible in InDesign to have a script target items selected in the Links panel. It's generally the case that panel and dialog selections are exposed to scripting.

m1b
Community Expert
Community Expert
March 31, 2022

Hi @Eugene Tyson,

 

Edit: I made a small change to the script to accommodate Eugene's reply below.

 

Please try this script: (it's based on my relinker script)

 

function main() {

    updateLinks(app.activeDocument);

    function updateLinks(doc) {
        var links = doc.links,
            counter = 0,
            findWhat = /_\d+(\.[^\.]+)$/,
            changeTo = '$1';

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

            // ignore normal links
            if (links[i].status === LinkStatus.NORMAL) continue;

            // new file by replacing text within file path
            var newLinkedFile = File(links[i].filePath.replace(findWhat, changeTo));

            // if file exists at the new file path, relink
            if (newLinkedFile.exists) {
                links[i].relink(newLinkedFile);
                counter++;
            }
        }

        alert('Relinked ' + counter + ' out of ' + links.length + ' links.');
    }

} // end main

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Update Links');

 

 

This script will iterate over all the links in the active document and, if it's missing, will try to remove underscore and digits from the filename and then, if that new filename exists, it will relink to that file. Note that this will "Unembed" embedded links.

- Mark

Community Expert
April 1, 2022

Wow - thank you so much, Mark!

 

I'll give it a try later today - it looks exactly like what I was after!