11 Replies Latest reply on Jun 3, 2015 6:35 AM by kajzica

    ID CS4 rename all links by page number/appearance order

    Matthew Scholes

      Hi

       

      I desperately need a script that will rename all linked files within an InDesgin CS4 file with a prefix followed by the page number(s) on which it apprears, and then a letter suffix to differentiate between images on the same page.

       

      eg. If the prefix I want to use is "JOB_001" then the filenames would be renamed and relinked like so:

       

      JOB_001_master_A.eps - (file appears on master page A)

      JOB_001_p1a.eps

      JOB_001_p2_3_4.eps (file appears on pages 2 3 and 4)

      JOB_001_p2a.eps

      JOB_001_p2b.eps

      JOB_001_p2c.tif

      JOB_001_p3a.tif

      JOB_001_p3b.eps

      JOB_001_p4a.eps

      JOB_001_p4b.eps

      JOB_001_p4c.tif

       

      Does anyone know of a script already out there that can do this?

       

      Thanks

        • 1. Re: ID CS4 rename all links by page number/appearance order
          Kasyan Servetsky Level 5

          Check out this post and a few posts above it — it's quite close to what you want.

          Here you can download the latest version of the script: http://forums.adobe.com/message/2436686#2436686

           

          Kasyan

          • 2. Re: ID CS4 rename all links by page number/appearance order
            Matthew Scholes Level 1

            Thanks for your reply.

             

            Unfortunately I'm not a scripter so editing an existing script is going to be a challenge.

             

            I thought there might be something already out there, but may be I can piece soething together from the thread you pointed out

             

            Thanks again.

            • 3. Re: ID CS4 rename all links by page number/appearance order
              Kasyan Servetsky Level 5

              I remade the script for you. However it doesn't deal with links placed on more than one page. I'm working so can't test it right now.

               

              Kasyan

              • 4. Re: ID CS4 rename all links by page number/appearance order
                Matthew Scholes Level 1

                That's very kind of you. Thank you very much.

                 

                I'm trying to work out a way of dealing with the duplicate placed images based on you original script. I'm getting thee slowly, if I get it working I'll post it so you can see

                 

                Thanks again

                • 5. Re: ID CS4 rename all links by page number/appearance order
                  Kasyan Servetsky Level 5

                  If you are interested, in this script you can see example of how to check how many times an image is placed --  LinkUsage function -- you can collect the names of the pages where the image is placed into array, then use .join("_") to create substring and use it for creating file name.

                   

                  Kasyan

                  • 6. Re: ID CS4 rename all links by page number/appearance order
                    Matthew Scholes Level 1

                    After a lot of trying it's almost working - I just ca't get the relink to work (line 108)

                     

                    Any help appreciated.

                     

                    Thanks

                    • 7. Re: ID CS4 rename all links by page number/appearance order
                      Kasyan Servetsky Level 5

                      The main problem in your script is that you are trying to rename the same file more than once, thus relinking the image to non existent file.

                      Moreover, you are using the same variables in global scope for different for loops: I – 3 times, p – 2 times.

                      Here is my implementation of what I suggested you to do in my previous post. It’s not totally tested yet, but works for me on CS3 for Windows.

                       

                      Kasyan

                      • 8. Re: ID CS4 rename all links by page number/appearance order
                        Matthew Scholes Level 1

                        Thanks for your help, again!

                         

                        I couldn't get your script to run, so I have tried tidying mine up.

                         

                        It's now a dedicated script for multiple linked images.

                         

                        It creates an array of existing link names and the new names as it renames the files.

                         

                        Then I'm trying to use this array to find the links that need updating and update them.

                         

                        Renaming the files is fine, but when I attempt to update the links it errors after the first link is updated on line 128, invalid object for this request

                         

                        In my test document there a two copies of the same image on page one and that's where it errors. I just can't figure it out.

                         

                        Any help gratefully received.

                        • 9. Re: ID CS4 rename all links by page number/appearance order
                          Matthew Scholes Level 1

                          Removed the line and it works!

                           

                          Thanks again for your help.

                          • 10. Re: ID CS4 rename all links by page number/appearance order
                            Kasyan Servetsky Level 5

                            I couldn't get your script to run

                            What error are you getting? Could you attach or send me a sample files at askoldich@yahoo.com, so that I could recreate the problem.

                             

                            Kasyan

                            • 11. Re: ID CS4 rename all links by page number/appearance order
                              kajzica Level 1

                              $.writeln("POČINJEM")

                              var myDoc = app.activeDocument;

                              var myAllLinks = myDoc.allGraphics; //myDoc.links

                              var myMultipleLinks = new Array();

                              var myMultipleLinksName = new Array();

                              var myLinksCounter = 1;

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

                              if (!myPrepend) exit();

                              myPrepend += "_str_";

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

                               

                              if ( response == true )

                              {

                                   WriteToFile("\r--------------------- Script started -- " + GetDate() + " ---------------------\n\n");

                                  

                                   for ( k = 0; k < myAllLinks.length; k++ )

                                   {

                                       if(myAllLinks[k].itemLink == null){

                                           $.writeln("OVAJ LINK JE NULL: " + k);

                                           myAllLinks.splice(k,1);

                                           k--;

                                           continue;

                                        }

                                        var myLinkName = myAllLinks[k].itemLink.name;

                                   }

                               

                                   crearLabels();

                               

                                   var myPages = myDoc.pages;

                                   // Pages

                                   for ( p = 0; p < myPages.length; p++ )

                                   {

                                        var myPageNumber = pad000(myPages[p].name);

                               

                                        var myLinks = myPages[p].allGraphics;

                                        var myASCII = 97;

                                        for ( k = myLinks.length - 1; k >= 0; k-- )

                                        {

                                             var myLink = myLinks[k].itemLink;

                                             if (myLink.extractLabel("relinked") != "yes") {

                                                  var myOldLinkName = myLink.name;

                                                  var myLinkUsage = LinkUsage( myLink );

                                                  var myExtension = myOldLinkName.substr(myOldLinkName.lastIndexOf( "." ));

                                                  if (LinkUsage(myLink) == 1)

                                                  {

                                                       var myNewLinkName = myPrepend + myPageNumber + String.fromCharCode( myASCII ) + myExtension;

                                                       var myOldImageHDfile = new File( myLink.filePath );

                                                       var myRenameResult = myOldImageHDfile.rename( myNewLinkName );

                                                       var postfix = 1;

                                                       while(!myRenameResult){

                                                           myNewLinkName = myPrepend + myPageNumber + String.fromCharCode( myASCII )  + "_" + postfix + myExtension;

                                                           myRenameResult = myOldImageHDfile.rename( myNewLinkName );

                                                           postfix++;

                                                       }

                                                       if (myRenameResult)     {

                                                            myLink.insertLabel("relinked", "yes");

                                                            myLink.relink( myOldImageHDfile );

                                                            try {

                                                                 myLink = myLink.update();

                                                            }

                                                            catch(err) {}

                                                            myASCII++;

                                                            WriteToFile(((myLinksCounter < 10) ? (" " + myLinksCounter) : myLinksCounter) + " - " + myOldImageHDfile.name + " --> " + myNewLinkName + "\n");

                                                            myLinksCounter++;

                                                       }

                                                       else {

                                                            if (new File(myOldImageHDfile.parent + "/" + myNewLinkName + myExtension).exists) {

                                                                 WriteToFile("CAN'T RENAME LINK -- " + myOldImageHDfile.name + " to " + myNewLinkName + " because the file already exists\n");

                                                            }

                                                            else {

                                                                 WriteToFile("CAN'T RENAME LINK -- " + myOldImageHDfile.name + "\n");

                                                            }

                                                       }

                                                  }

                                                  else {

                                                      if (!IsObjInArray(myLink, myMultipleLinks)) {

                                                            myMultipleLinks.push(myLink);

                                                            var myNewLinkName2 = myPrepend + myPageNumber + String.fromCharCode( myASCII ) + myExtension;

                                                            myMultipleLinksName.push(myNewLinkName2);

                                                            myASCII++;

                                                       }

                                                  }

                                             }

                                        }

                                   }

                               

                                   var myMasterSpreads = myDoc.masterSpreads;

                                   // Master spreads

                                   for ( m = 0; m < myMasterSpreads.length; m++ )

                                   {

                                        var myMastSpr = myMasterSpreads[m];

                                        var myPageNumber = myMastSpr.name;

                                        var myPrefix = myMastSpr.namePrefix;

                                        var myLinks = myMastSpr.allGraphics;

                                        var myASCII = 97;

                                        for ( n = myLinks.length - 1; n >= 0; n-- )

                                        {

                                             var myLink = myLinks[n].itemLink;

                                             if (myLink.extractLabel("relinked") != "yes") {

                                                  var myOldLinkName = myLink.name;

                                                  var myExtension = myOldLinkName.substr(myOldLinkName.lastIndexOf( "." ));

                                                  if (LinkUsage(myLink) == 1)

                                                  {

                                                       var myLinkLetter = (myLinks.length == 1) ? "" : String.fromCharCode( myASCII );

                                                       var myNewLinkName = myPrepend + '_master_' + myPrefix + myLinkLetter + myExtension;

                                                       var myOldImageHDfile = new File( myLink.filePath );

                                                       var myRenameResult = myOldImageHDfile.rename( myNewLinkName );

                                                       var postfix = 1;

                                                       while(!myRenameResult){

                                                           myNewLinkName = myPrepend + '_master_' + myPrefix + myLinkLetter + "_" + postfix + myExtension;

                                                           myRenameResult = myOldImageHDfile.rename( myNewLinkName );

                                                           postfix++;

                                                       }

                                                       if (myRenameResult) {

                                                            myLink.insertLabel("relinked", "yes");

                                                            myLink.relink( myOldImageHDfile );

                                                            try {

                                                                 myLink.update();

                                                            }

                                                            catch(err) {}

                                                            myASCII++;

                                                            WriteToFile(((myLinksCounter < 10) ? (" " + myLinksCounter) : myLinksCounter) + " - " + myOldImageHDfile.name + " --> " + myNewLinkName + "\n");

                                                            myLinksCounter++;

                                                       }

                                                       else     {

                                                            if (new File(myOldImageHDfile.parent + "/" + myNewLinkName + myExtension).exists) {

                                                                 WriteToFile("CAN'T RENAME LINK -- " + myOldImageHDfile.name + " to " + myNewLinkName + " because the file already exists\n");

                                                            }

                                                            else {

                                                                 WriteToFile("CAN'T RENAME LINK -- " + myOldImageHDfile.name + "\n");

                                                            }

                                                       }

                                                  }

                                                  else

                                                  {

                                                       if (!IsObjInArray(myLink, myMultipleLinks)) {

                                                            myMultipleLinks.push(myLink);

                                                            var myLinkLetter2 = (myLinks.length == 1) ? "" : String.fromCharCode( myASCII );

                                                            var myNewLinkName2 = myPrepend + '_master_' + myPrefix + myLinkLetter2 + myExtension;

                                                            myMultipleLinksName.push(myNewLinkName2);

                                                            myASCII++;

                                                      }

                                                  }

                                             }

                                        }

                                   }

                                   // Multiple images

                                   if (myMultipleLinks.length > 0) {

                                        for ( a = myMultipleLinks.length - 1; a >= 0; a-- )

                                        {

                                             processMultiUsedLinks(myMultipleLinks[a], myMultipleLinksName[a]);

                                        }

                                   }

                               

                                   WriteToFile("\r--------------------- Script finished -- " + GetDate() + " ---------------------\r\r");

                                  

                                   if (myLinksCounter == 0) {

                                        alert("No links have been renamed", "Rename Links Script");

                                   }

                                   if (myLinksCounter == 1) {

                                        alert("One link has been renamed", "Rename Links Script");

                                   }

                                   else if (myLinksCounter > 1) {

                                        alert(myLinksCounter  + " links have been renamed", "Rename Links Script");

                                   }

                               

                              }

                              //--------------------------------------------- Functions ------------------------------------------------

                              // Check how many times the link was placed

                              function LinkUsage(myLink) {

                                   var myLinksNumber = 0;

                                        for (var c =  0; c < myDoc.links.length; c++) {

                                        if (myLink.filePath == myDoc.links[c].filePath) {

                                             myLinksNumber += 1;

                                        }

                                   }

                                   return myLinksNumber;

                              }

                              //---------------------------------------------------------------------------------------- ----------------------

                              // Relink the links placed more than once

                              function processMultiUsedLinks(myLink, naziv) {

                                   var myExtension = "";

                                   var myMultiUsedLink = new Array();

                                   var myAllLinks = myDoc.links;

                                   for (var d = 0; d < myAllLinks.length; d++)  {

                                        if (myAllLinks[d].filePath == myLink.filePath) {

                                             myMultiUsedLink.push(myAllLinks[d]);

                                        }

                                   }

                                   try {

                                        myLink.show();

                                   }

                                   catch(err) {}

                                  

                                   var myNewLinkName = naziv; //prompt ("Enter a name for this image", GetFileNameOnly(myLink.name), "This image is placed " + myMultiUsedLink .length + " times");

                                   if (myNewLinkName) {

                                        if ( myNewLinkName + myExtension != myLink.name ) {

                                             var myOldImageHDfile = new File( myLink.filePath );

                                             var myRenameResult = myOldImageHDfile.rename( myNewLinkName + myExtension );

                                             var postfix = 1;

                                             while(!myRenameResult){

                                                 myRenameResult = myOldImageHDfile.rename( myNewLinkName + "_" + postfix + myExtension );

                                                 postfix++;

                                             }

                                             if (myRenameResult) {

                                                  myLink.insertLabel("relinked", "yes");

                                                  myLink.relink( myOldImageHDfile );

                                                  try {

                                                       myLink = myLink.update();

                                                  }

                                                  catch(err) {}

                                                 

                                                  WriteToFile(((myLinksCounter < 10) ? (" " + myLinksCounter) : myLinksCounter) + " - " + myOldImageHDfile.name + " --> " + myNewLinkName + "\n");

                                                  myLinksCounter++;

                               

                                                  for (f = myMultiUsedLink.length-1; f >= 0 ; f--)

                                                  {

                                                       var myCurrLink = myMultiUsedLink[f];

                                                       if ( myNewLinkName + myExtension != myCurrLink.name ) {

                                                            myCurrLink.insertLabel("relinked", "yes");

                                                            myCurrLink.relink( myOldImageHDfile );

                               

                                                            try {

                                                                 myCurrLink = myLink.update();

                                                            }

                                                            catch(err) {}

                                                       }

                                                  }

                                             }

                                             else     {

                                                  if (new File(myOldImageHDfile.parent + "/" + myNewLinkName + myExtension).exists) {

                                                       $.writeln("CAN'T RENAME LINK -- " + myOldImageHDfile.name + " to " + myNewLinkName + myExtension + " because the file already exists\n");

                                                  }

                                                  else {

                                                       $.writeln("CAN'T RENAME LINK -- " + myOldImageHDfile.name + "\n");

                                                  }

                                             }

                                        }

                                   }

                                   UpdateAllOutdatedLinks();

                              }

                              //---------------------------------------------------------------------------------------- ----------------------

                              // Clear labels in case this script has been already run on the current document

                              function crearLabels() {

                                   for (var f =  0; f < myDoc.links.length; f++) {

                                        if (myDoc.links[f].extractLabel("relinked") != "") {

                                             myDoc.links[f].insertLabel("relinked", "");

                                        }

                                   }

                              }

                              //---------------------------------------------------------------------------------------- ----------------------

                              function UpdateAllOutdatedLinks() {

                                   for(var myCounter = myDoc.links.length-1; myCounter >= 0; myCounter--){

                                        var myLink = myDoc.links[myCounter];

                                        if (myLink.status == LinkStatus.linkOutOfDate){

                                             myLink.update();

                                        }

                                   }

                              }

                              //---------------------------------------------------------------------------------------- ----------------------

                              function pad000(myNumber) {

                                   if (myNumber >= 1 && myNumber <= 9) {

                                        x =  "0" + "0" + myNumber;

                                   } else if (myNumber >= 10 && myNumber <= 99) {

                                        x = "0" + myNumber;

                                   } else if (myNumber >= 100 && myNumber <= 999) {

                                        x = myNumber;

                                   }

                                   return x;

                              }

                              //---------------------------------------------------------------------------------------- ----------------------

                              function GetFileNameOnly(myFileName) {

                                   var myString = "";

                                   var myResult = myFileName.lastIndexOf(".");

                                   if (myResult == -1) {

                                        myString = myFileName;

                                   }

                                   else {

                                        myString = myFileName.substr(0, myResult);

                                   }

                                   return myString;

                              }

                              //---------------------------------------------------------------------------------------- ----------------------

                              function IsObjInArray(myObj, myArray) {

                                   for (x in myArray) {

                                        if (myObj.filePath == myArray[x].filePath) {

                                             return true;

                                        }

                                   }

                                   return false;

                              }

                              //---------------------------------------------------------------------------------------- ----------------------

                              function WriteToFile(myText) {

                                   myFile = new File("~/Desktop/Rename Links Script.txt");

                                   if ( myFile.exists ) {

                                        myFile.open("e");

                                        myFile.seek(0, 2);

                                   }

                                   else {

                                        myFile.open("w");

                                   }

                                   myFile.write(myText);

                                   myFile.close();

                              }

                              //---------------------------------------------------------------------------------------- ----------------------

                              function GetDate() {

                                   var myDate = new Date();

                                   if ((myDate.getYear() - 100) < 10) {

                                        var myYear = "0" + new String((myDate.getYear() - 100));

                                   } else {

                                        var myYear = new String ((myDate.getYear() - 100));

                                   }

                                   var myDateString = (myDate.getMonth() + 1) + "/" + myDate.getDate() + "/" + myYear + " " + myDate.getHours() + ":" + myDate.getMinutes() + ":" + myDate.getSeconds();

                                   return myDateString;

                              }

                              //---------------------------------------------------------------------------------------- ----------------------