Skip to main content
Participant
February 2, 2015
Answered

A script to batch rename and relink images?

  • February 2, 2015
  • 5 replies
  • 7700 views

Hello, everyone!

First of all, I did search for this before asking and found many similar scripts for this, but as I don't know how to make or edit scripts, I couldn't use them...

I work at a company that makes school books. Each InDesign file we make can have hundreds of images, of varied extensions (.ai, .eps, .tif, .psd).

We now need to rename those images to a default naming system we defined. This is taking hours, even days to do manually.

Those images currently have varied names, so we'd need to ignore what's there, we just need a configurable prefix (like "EF6_ESP_C1_"), then a 4-digit number starting at 0001 and numbering the rest (if there's say a .tif and then a .ai, it'd have to be 0001.tif and 0002.ai), then the extension it already had.

Preferably, but not required, if it could read that the image already had an "EQ" in the file name, it could rename them to another prefix and count those separately from 0001, as we work Equation images separately, but if this can't be done or is too difficult to do, then just have them named and numbered with the others.
Could anyone be so kind as to help me with this? Thank you very much!

This topic has been closed for replies.
Correct answer Chinnadk

Try this,

var doc = app.activeDocument,

    links = doc.allGraphics, count = 1, eqcount = 1, a = "0000";

for(var i=links.length-1;i>=0;i--)

{

        if(links.itemLink.name.match(/^EQ.+/))

        {

                var ext = links.itemLink.name.substr(links.itemLink.name.lastIndexOf(".")),

                    old = File(links.itemLink.filePath),

                    eqnum = (eqcount++).toString(),

                    newname = "EQ_EF6_ESP_C1_" + a.substring(0, a.length - eqnum.length) + eqnum + ext;

                old.rename(newname);

                links.itemLink.relink(File(old.toString().replace(links.itemLink.name,newname)))

            }

        else

        {

                var ext = links.itemLink.name.substr(links.itemLink.name.lastIndexOf(".")),

                    old = File(links.itemLink.filePath),

                    num = (count++).toString(),

                    newname = "EF6_ESP_C1_" + a.substring(0, a.length - num.length) + num + ext

                old.rename(newname);

                links.itemLink.relink(File(old.toString().replace(links.itemLink.name,newname)))

            }

    }


Please note:It is not reversible.


Regards,

Chinna

5 replies

Chinnadk
ChinnadkCorrect answer
Legend
February 3, 2015

Try this,

var doc = app.activeDocument,

    links = doc.allGraphics, count = 1, eqcount = 1, a = "0000";

for(var i=links.length-1;i>=0;i--)

{

        if(links.itemLink.name.match(/^EQ.+/))

        {

                var ext = links.itemLink.name.substr(links.itemLink.name.lastIndexOf(".")),

                    old = File(links.itemLink.filePath),

                    eqnum = (eqcount++).toString(),

                    newname = "EQ_EF6_ESP_C1_" + a.substring(0, a.length - eqnum.length) + eqnum + ext;

                old.rename(newname);

                links.itemLink.relink(File(old.toString().replace(links.itemLink.name,newname)))

            }

        else

        {

                var ext = links.itemLink.name.substr(links.itemLink.name.lastIndexOf(".")),

                    old = File(links.itemLink.filePath),

                    num = (count++).toString(),

                    newname = "EF6_ESP_C1_" + a.substring(0, a.length - num.length) + num + ext

                old.rename(newname);

                links.itemLink.relink(File(old.toString().replace(links.itemLink.name,newname)))

            }

    }


Please note:It is not reversible.


Regards,

Chinna

Participant
February 3, 2015

Chinnadk:

That worked out perfectly! Thank you so much!

Any chance you could make it prompt the user for a prefix instead of having it fixed? It was easy enough for us to figure out how to change the script for that specific thing, but we have a big rotation of designers in the company and it could be troublesome to explain to each new one how to change the script.

Again, thank you very much

Participant
February 3, 2015

Nevermind, we were able to frankenstein it from other scripts we found in these forums (I hope that's ok!) and it worked beautifully.

If anyone else needs it:

var myDoc = app.activeDocument; 

var myPrepend = prompt("Example: thebook_08765", "Job description", "Please enter job description"); 

if (!myPrepend) exit(); 

var response = confirm("Warning: You are about to rename all images linked to the foremost Indesign Document - proceed? Keep in mind - it is not reversible!", false, "Rename Links Script");

var doc = app.activeDocument, 

    links = doc.allGraphics, count = 1, eqcount = 1, a = "0000"; 

for(var i=links.length-1;i>=0;i--) 

                var ext = links.itemLink.name.substr(links.itemLink.name.lastIndexOf(".")), 

                    old = File(links.itemLink.filePath), 

                    num = (count++).toString(), 

                    newname = myPrepend + a.substring(0, a.length - num.length) + num + ext 

                old.rename(newname); 

                links.itemLink.relink(File(old.toString().replace(links.itemLink.name,newname))) 

    }

February 2, 2015

No, when you rename all the files, InDesign still keep the original links and asks you to update them.

Once you renamed all the files, just go on the link panel in InDesign: you will see that all the links have a yellow triangle which means you have to update them

> than, you click botton in the top-right corner of the links menu and click "update all links"

February 2, 2015

Well, I had the same problem some days ago. Automator is a default application in machintosh, it allows you to choose a folder and rename al the files in it. You can automate the renaming in sequence (EQ01, EQ02, EQ03). If the link folder of InDesign has different names, all the files will be renamed in sequence.

Instructions:

1 - open Automator

2 - chose workflow

3 - type "get specified finder items" and double click it to add this action in the workflow

4 - type "rename finder items" and double click it to add this action in the workflow

5 - add the folder you have to rename in the first action

6 - in the second action choose "make sequential"

7 - set the other name setting in order to get the name you want

8 - save and choose as "application" in the saving options

Now you have an action that rename all the files in that folder anytime you click it

In alternative, you can choose to not save and play the action in automator

Participant
February 2, 2015

Harihara: the logic of that makes perfect sense to me, thanks! However, as neither I nor anybody else in the company know how to make scripts, I wouldn't be able to put that to practice.

Lorenzo: That would rename all the images, yes... but wouldn't that require us to relink the images one by one in InDesign after the batch renaming?

Adobe-InDesign_CS4
Inspiring
February 2, 2015

Hi Maira

This is Possible in InDesign Windows also.

(1) Collect All images

(2) Segregate the Extension and Stored one variable

(3) Store Variable to Add your numbering ....etc...

(4) now once again add the separated extension value

(5) Finally rename using Re-Link options.

Thanks & Regards

T.R.Harihara Sudhan

February 2, 2015

Hi, I don't know a way to rename all the images in InDesign, but if you have a machintosh computer I can easly tell you how to do it

Participant
February 2, 2015

We use Macs in the company, yes. I'd appreciate it. Would that way rename all images AND update the links in InDesign? If it were just renaming it, we could do it in Bridge...