I want to change the filename of a link into Indesign. For example: the existing filename is table_EX.jpg and now I want it to change into table_CZ.jpg
I wrote a script, but it seems that it only change ONE link. Who can help me?
#target indesign
// Relink file fileName.js
if (app.documents.length == 0) {
err("No open document. Please open a document and try again.", true);
}
var myDoc = app.activeDocument;
var myLinks = myDoc.links;
var myCounter = 0;
if (myLinks.length == 0) {
err("This document doesn't contain any links.", true);
}
for (i = myLinks.length-1; i >= 0 ; i--) {
var myLink = myLinks[i];
var myOldfileName = myLink.name;
var myNewfileName = myOldfileName.replace("_EX", "_CZ");
var myNewFile = new File(myNewfileName);
if (myNewFile.exists) {
myLink.relink(myNewFile);
myCounter++;
}
}
document.save()
document.close()
Probably relinking messes up the myLinks collection that you saved earlier.
A quick fix might to be to place this line inside your for..loop:
myLinks = myDoc.links;
.. it's kind of a dirty trick. I suppose the loop will keep working. If not, you'll have to use an infinite loop and keep on checking from the start, until you made no change.
North America
Europe, Middle East and Africa
Asia Pacific