6 Replies Latest reply on Feb 2, 2009 2:47 AM by (Rasmus_Olsen)

    Update link - js assistance

    Level 1
      Hi folks,


      for (var i = 0; i < app.activeDocument.links.length; i++){
      app.activeDocument.links[i].update();
      }


      This returns an object ([object Link]). I have tried using the following: app.activeDocument.links.item(i).update(), but I still get the object 'Link'. A little guidance is needed :-)

      Thanks.

      Regards,
      Rasmus
        • 1. Re: Update link - js assistance
          Level 1
          BTW Im using this to prevent automated export of docs with missing links with a try/catch.
          • 2. Re: Update link - js assistance
            Kasyan Servetsky Level 5
            Hi Rasmus,

            When you update links, iterate backwards, because updating a link changes links collection.

            Kasyan


            function UpdateAllOutdatedLinks(){
               var myLinks = myDocument.links;
               for(var myCounter = myDocument.links.length-1; myCounter >= 0; myCounter--){
                  var myLink = myDocument.links[myCounter];
                  if (myLink.status == LinkStatus.linkOutOfDate){
                     myLink.update();
                  }
               }
            }
            • 3. Re: Update link - js assistance
              Level 1
              Hi Kasyan,

              I would never have figured that out on my own - thanks!

              It appears that myDocument.links.everyItem().update() also gives me the exception catch when links are missing. Is there any downfalls to this approach in your opinion?

              Regards,
              Rasmus
              • 4. Re: Update link - js assistance
                Kasyan Servetsky Level 5
                I use this function to check for modified/missing/embedded links. It finds and shows such links.


                var myDoc = app.activeDocument;
                checkLinkNormal();

                function checkLinkNormal(){
                   for(var myCounter = 0; myCounter < myDoc.links.length; myCounter++){
                   var myLink = myDoc.links[myCounter];
                      if (myLink.status != LinkStatus.normal){
                         if (myLink.status == LinkStatus.linkOutOfDate){
                            myLink.show();
                            alert ("This link is modified.");
                            }
                         else if (myLink.status == LinkStatus.linkMissing){
                            myLink.show();
                            alert("This link is modified.");
                            }
                         else if (myLink.status == LinkStatus.linkEmbedded){
                            myLink.show();
                            alert ("This link is embeded.");
                         }
                         exit();
                      }
                   }
                }

                If we use your approach, we just get an error that some link, someware in the document is missing:


                var myDoc = app.activeDocument;
                try {
                   myDoc.links.everyItem().update();
                }
                catch(e) {
                   err(e);
                }
                function err(e){
                   alert(e);
                   exit();
                }
                • 5. Re: Update link - js assistance
                  Level 1
                  Dear Kasyan, thanks for taking time to reply. I've learned a lot from your replies :-)

                  Best regards,
                  Rasmus
                  • 6. Re: Update link - js assistance
                    Level 1
                    Kasyan,

                    I've used your script to alert out a sort of link report - se script below. Can myLog variable be written to a txtfile with same path as current document using InDesign API? I've looked in the Core Javascript Core Objectmodel viewer under the File classes but got lost in the code.. :-)


                    var myDoc = app.activeDocument;
                    checkLinkNormal();
                    function checkLinkNormal() {
                    var myLog1 = new String;
                    var myLog2 = new String;
                    var myLog3 = new String;
                    for(var myCounter = 0; myCounter < myDoc.links.length; myCounter++) {

                    var myLink = myDoc.links[myCounter];
                    if(myLink.status != LinkStatus.normal) {
                    if(myLink.status == LinkStatus.linkOutOfDate) {
                      myLog1 += 'Link #'+myCounter+' name: '+myLink.name+'.\n';
                      myLink.update();
                    }
                    else if(myLink.status == LinkStatus.linkMissing) {
                      myLog2 += 'Link #'+myCounter+' name: '+myLink.name+' Link #'+myCounter+' path: '+myLink.filePath+'.\n';
                    }
                    else if(myLink.status == LinkStatus.linkEmbedded) {
                      myLog3 += 'Embedded links: \nLink name: '+myLink.name+'.\n';
                    }
                    }
                    }
                    if(myLog1.length > 0) {
                    alert('Links updated: \n'+myLog1);
                    }
                    if(myLog2.length > 0) {
                    alert('Missing links: \n'+myLog2);
                    }
                    if(myLog3.length > 0) {
                    alert('Embedded links: \n'+myLog3);
                    }
                    }


                    BTW Isn't there an easier method to posting script with formatting preserved? I've googled a "script poster" with no luck.

                    Best regards,
                    Rasmus