Skip to main content
Participant
August 8, 2012
Question

change filename into link

  • August 8, 2012
  • 2 replies
  • 577 views

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;

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

This topic has been closed for replies.

2 replies

tomaxxi
Inspiring
August 8, 2012

Change

var myLinks = myDoc.links;

to

var myLinks = myDoc.links.everyItem().getElements();

This should work, because myLinks will not change during the updates.

Hope that helps.

--

Marijan (tomaxxi)

http://tomaxxi.com

Jongware
Community Expert
Community Expert
August 8, 2012

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.