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

Relink images to similar file names

Community Expert ,
Mar 31, 2022 Mar 31, 2022

Copy link to clipboard

Copied

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/6...

 

Thanks

 

TOPICS
Scripting

Views

308

Likes

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

correct answers 1 Correct answer

Community Expert , Apr 06, 2022 Apr 06, 2022

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
...

Likes

Translate

Translate
Community Expert ,
Mar 31, 2022 Mar 31, 2022

Copy link to clipboard

Copied

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

Likes

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 31, 2022 Mar 31, 2022

Copy link to clipboard

Copied

Wow - thank you so much, Mark!

 

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

 

 

Likes

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 ,
Apr 01, 2022 Apr 01, 2022

Copy link to clipboard

Copied

Ok 

 

I see what I missed from your screenshots

 

It would look something like this 

Where the image is embedded and it ends in _01

 

I just need to relink them to the filename - but without the _01

Similar to something how Relink to Folder works - but when I try this it doesn't find them because of the _01

 

Screenshot 2022-04-01 at 09.16.32.png

 

My workflow is to relink each manually.

Which works. But it's a continious thing.

 

I have no control over this - just need to relink to the original filename. 

Screenshot 2022-04-01 at 09.20.02.png

 

 

Thanks

Likes

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 ,
Apr 01, 2022 Apr 01, 2022

Copy link to clipboard

Copied

Thanks @Eugene Tyson, that clarifies your situation. I've made a change to the script I posted that should work for your case. Let me know how it goes.

- Mark

Likes

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 ,
Apr 01, 2022 Apr 01, 2022

Copy link to clipboard

Copied

Wow - thank you so much.

I won't get to this until Monday - but I'll certainly let you know how it goes.

 

Thanks

Likes

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 ,
Apr 05, 2022 Apr 05, 2022

Copy link to clipboard

Copied

When I double click the script to run it - nothing happens - it just says 0 of 3 links 

 

What am I doing wrong?

Likes

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 ,
Apr 05, 2022 Apr 05, 2022

Copy link to clipboard

Copied

I think the weak link is me—not understanding exactly your situation. Are you able to post an indesign packaged file that shows the problem scenario?

For example, if my script shows 0 of 3 links it may mean that the file path associated with the embedded links is no longer accessible. This would happen if the file was embedded, then the original linked file was moved, or renamed, or a folder in it's path was renamed.

I have an idea to make the script more robust in this case, so I'll update it if I have time.

- Mark

Likes

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 ,
Apr 05, 2022 Apr 05, 2022

Copy link to clipboard

Copied

This would happen if the file was embedded, then the original linked file was moved

 

Hi Mark, if a link is embedded you don’t need the original in order to save it to a file. The .unembed method‘s parameter is a folder, so all you have to do is point to a folder and the embedded file will get saved out to the folder even if the original no longer exists.

Likes

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 ,
Apr 05, 2022 Apr 05, 2022

Copy link to clipboard

Copied

Thanks Rob, it's a really good idea to take that route.

 

I don't understand if that would work for Eugene. Eugene mentions "And link to my images in a folder." by which I understood the images are already in the folder, but need to be re-linked. In this case it might be messy to create new files for each embedded image. Eugene, when you read this, you might be able to clarify your exact situation.

 

In my opinion, Rob's approach is very robust because Rob is creating the files in a location you choose, rather than trying to match up the paths. However it will possibly give you new files as well as the existing ones (if they do exist).

- Mark

Likes

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 ,
Apr 06, 2022 Apr 06, 2022

Copy link to clipboard

Copied

Yes - so the path of the embedded would say be

C:\\unknown user\from a different computer\from a long time ago\image_01.jpg

 

My file would be

/users/eugene/desktop/desktop_wip/folder/folder/folder/folder/image.jpg

 

The thing is - the files could be 3 years old.

And someone at some point puts in _01 probably a legacy workflow.

 

I download the image from a central source

And the file name is always the same - but without the _01

 

One document I have has say 16 links

It's just time consuming to go through 16 links

 

 

@rob day method might work

Let me try it later today and see how I get on.

 

I always felt this is a missing feature from InDesign - relinking to a folder of images that were appended.

It's bugged me for almost a decade.

 

So I thought I'd ask about it.

Likes

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 ,
Apr 05, 2022 Apr 05, 2022

Copy link to clipboard

Copied

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

 

 

 

 

Screen Shot 3.png

 

Screen Shot 2.png

Likes

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 ,
Apr 05, 2022 Apr 05, 2022

Copy link to clipboard

Copied

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

Likes

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 ,
Apr 05, 2022 Apr 05, 2022

Copy link to clipboard

Copied

Also, I’m adding the extension back to the new file name, but the way I’m doing it won‘t work if the extension is more than 3 characters (e.g., jpeg). This might be better:

 

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

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

 

OSX lets you save without an extension, and in that case there still would be a problem—Mark or others might have a better way of checking for the extension

Likes

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 ,
Apr 06, 2022 Apr 06, 2022

Copy link to clipboard

Copied

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?

 

 

Likes

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 ,
Apr 06, 2022 Apr 06, 2022

Copy link to clipboard

Copied

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.

Likes

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 ,
Apr 06, 2022 Apr 06, 2022

Copy link to clipboard

Copied

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

Likes

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 ,
Apr 12, 2022 Apr 12, 2022

Copy link to clipboard

Copied

LATEST

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.

Likes

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