Skip navigation
Gu@n@b@n@
Currently Being Moderated

change filename into link

Aug 8, 2012 6:33 AM

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

 
Replies
  • Currently Being Moderated
    Aug 8, 2012 6:41 AM   in reply to Gu@n@b@n@

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 8, 2012 6:48 AM   in reply to Gu@n@b@n@

    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

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points