2 Replies Latest reply on Dec 22, 2010 1:51 AM by Mahesh_JW

    Relink All Instance Of

    Mahesh_JW Level 1

      Hi All,

       

      I want to rename and relink images, In my code it works well in a single instance of image. While running in Production I got error that some links are missing links, than only I know that some links are multiple instance in a document. If first one is renamed and relinked then another one shows missing link what can I do????

       


      My code is

       

      do{
           //try{
                var lin_nam = csv_art_name[Cnt];
                myDoc.links.item(lin_nam).show();
                                 
                var oldVScale = app.selection[0].verticalScale;

                var oldHScale =app.selection[0].horizontalScale;

       

                var filName = myDoc.links.item(lin_nam).filePath.toString().replace(/%20/g, " ");
                var filePath = File(filName).path.toString().replace(/%20/g, " ");
                File(filName).rename(csv_art_rename[Cnt]);
                var newFile = File(filePath + "/" + csv_art_rename[Cnt]);
                app.selection[0].parent.select();
                app.selection[0].graphics[0].itemLink.relink(newFile);
                app.selection[0].graphics[0].itemLink.update();
                                 
                app.selection[0].graphics[0].absoluteVerticalScale = oldVScale;
                app.selection[0].graphics[0].absoluteHorizontalScale = oldHScale;
               //}catch(e){}
                             
                Cnt++;

      }while(csv_indd_name[Cnt] == "");

       

      Thanks in Advance

      Mahesh

        • 1. Re: Relink All Instance Of
          Kasyan Servetsky Level 5

          Dealing with multi-placed links is a tricky business because the Links collection is changed every time you update a link.

           

          My approach is to address all links by their IDs: first, create an array of IDs for links placed more than once and then relink the multi-placed links using their IDs.
          Here is a function from my script illustrating the process:

          function CollectImages() {
               var myAllLinks = gDoc.links;
               var myLinks = [];
               var linksIdList = [];
               var myMultiUsedLinks = [];
               
               var tmpLinksNames = [];
               var tmpMultiUsedLinksNames = [];
               
               for (var i =  0; i < myAllLinks.length; i++) {
                    var myLink = myAllLinks[i];
                    try {
                         if (myLink.parent.clippingPath.clippingType == ClippingPathType.DETECT_EDGES) myLink.parent.clippingPath.clippingType = ClippingPathType.NONE;
                    }
                    catch(err) {}
                         
                    if (CheckLink(myLink)) {
                         var myLinkUsedTimes = LinkUsage(myLink);
                         if (myLinkUsedTimes == 1) {
                              myLinks.push(myLink);
                              linksIdList.push(myLink.id);
                         }
                         else if (myLinkUsedTimes > 1) {
                              var myLargestLink = FindLargestLink(myLink);
                              if (!IsObjInArray(myLargestLink, myMultiUsedLinks)) {
                                   myMultiUsedLinks.push(myLargestLink);
                              }
                         }
                    }
               }
               
               if (myMultiUsedLinks.length > 0) {
                    for (var k = 0; k < myMultiUsedLinks.length; k++) {
                         myLinks.push(myMultiUsedLinks[k]);
                         linksIdList.push(myMultiUsedLinks[k].id);
                         tmpMultiUsedLinksNames.push(myMultiUsedLinks[k].name + "(" + myMultiUsedLinks[k].id +")");
                    }
               }
          
               for (var x = 0; x < myLinks.length; x++) {
                    tmpLinksNames.push(myLinks[x].name + "(" + myLinks[x].id + ")");
               }
               
               if (linksIdList.length > 0) { // main for loop
                    for (var n = linksIdList.length - 1; n  >= 0; n --) {
                         var myLink = gDoc.links.itemByID(linksIdList[n]);
                         if (CheckAvoidableRepository(myLink)) {
                              continue;
                         }
                         else {
                              ResizeImage(myLink);
                         }
                    }
               }
          }
          

           

          Hope this helps.

          Kasyan

          1 person found this helpful
          • 2. Re: Relink All Instance Of
            Mahesh_JW Level 1

            Hi

             

            Thanks for your mail, it helps me lot. Same way using link ID, I have solved this problem


            My code is:

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

            function CheckAll(){
                for(var j=0;j<myDoc.links.length;j++){
                    var tmpName = "#"+myDoc.links[j].name+"#";
                    for(var k=0;k<csv_li_name.length;k++){
                        var tmpName1 = "#"+csv_li_name[k]+"#";
                        if(tmpName.match(tmpName1)){
                            li_ID.push(myDoc.links[j].id);
                            li_NAME.push(myDoc.links[j].name);
                            li_STATUS.push(myDoc.links[j].status);
                            li_RENAME.push(csv_li_rename[k]);
                        }
                    }
                }
            }
            //---------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------ --------------------------
            function relink_Arr(){
                for(var m=myDoc.links.length-1;m>=0;m--){
                    var tID = "#"+myDoc.links[m].id+"#";
                    for(var n=0;n<li_ID.length;n++){
                       
                        var tID1 = "#"+li_ID[n]+"#";
                       
                        if(tID.match(tID1)){
                            if(myDoc.links[m].status == li_STATUS[n]){
                                myDoc.links[m].show();
               
                                var oldVScale = app.selection[0].verticalScale;
                                var oldHScale =app.selection[0].horizontalScale;
                               
                                var filName = myDoc.links[m].filePath.toString().replace(/%20/g, " ");
                                var filePath = File(filName).path.toString().replace(/%20/g, " ");
                                File(filName).rename(li_RENAME[n]);
                               
                                var newFile = File(filePath + "/" + li_RENAME[n]);
                                app.selection[0].parent.select();
                                app.selection[0].graphics[0].itemLink.relink(newFile);
                                app.selection[0].graphics[0].itemLink.update();
                               
                                app.selection[0].graphics[0].absoluteVerticalScale = oldVScale;
                                app.selection[0].graphics[0].absoluteHorizontalScale = oldHScale;
                            }
                            else{
                                myDoc.links[m].show();
                                var oldVScale = app.selection[0].verticalScale;
                                var oldHScale =app.selection[0].horizontalScale;
                               
                                var newFile = File(filePath + "/" + li_RENAME[n]);
                                app.selection[0].parent.select();
                                app.selection[0].graphics[0].itemLink.relink(newFile);
                                app.selection[0].graphics[0].itemLink.update();
                               
                                app.selection[0].graphics[0].absoluteVerticalScale = oldVScale;
                                app.selection[0].graphics[0].absoluteHorizontalScale = oldHScale;
                            }
                       
                            break;
                        }
                    }
                }
            }

             

             

            Thanks

            Mahesh