6 Replies Latest reply on Jun 22, 2014 12:53 PM by HarveyLiu

    Can someone help me to change the script function?

    HarveyLiu Level 1

      I have a script for indesign:


      var link, image, frame, 
      doc = app.activeDocument, 
      links = doc.links, 
      counter = 0; 
       
      for (var i = links.length-1; i >= 0; i--) { 
          if (links[i].status == LinkStatus.LINK_MISSING) { 
              try { 
                  link = links[i]; 
                  image = link.parent; 
                  frame = image.parent; 
                  frame.remove(); 
                  counter++; 
              } 
              catch (err) { 
                  $.writeln(i + " - " + err); 
              } 
          } 
      }


      which is made by Kasyan Servetsky, function: remove all missing link objects, it runs very well, But only work on single file,
      now I want change the function aim to let it work on all the open file,


      for example I opened over ten files, all the file have missing like objects, I want to remove all the missing link objects in all the file by just one click the script,


      is that possible?

        • 1. Re: Can someone help me to change the script function?
          Laubender Adobe Community Professional & MVP

          @HarveyLiu – access every link in every open document like that:

           

          var myLinks = app.documents.everyItem().links.everyItem().getElements();
          

           

          Then loop through the myLinks array.

           

          Uwe

          • 2. Re: Can someone help me to change the script function?
            HarveyLiu Level 1

            Is that I can it like this:

             

             

            var myLinks = app.documents.everyItem().links.everyItem().getElements(); 

               

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

                if (links[i].status == LinkStatus.LINK_MISSING) { 

                    try { 

                        link = links[i]; 

                        image = link.parent; 

                        frame = image.parent; 

                        frame.remove(); 

                        counter++; 

                    } 

                    catch (err) { 

                        $.writeln(i + " - " + err); 

                    } 

                } 

            }

             

             

            can you teach me how to change it?

            • 3. Re: Can someone help me to change the script function?
              Laubender Adobe Community Professional & MVP

              @HarveyLiu – read the name of the variable used for the links. It's myLinks in line one.
              You are using the variable name links in the for loop.

               

              Either change the variable name in the first line from myLinks to links or change the variable name in the for loop to myLinks.

               

              Hm. I think you have to read a bit about core JavaScript to grab some concepts on syntax before using or trying to change code snippets…

               

              Uwe

              • 4. Re: Re: Can someone help me to change the script function?
                Laubender Adobe Community Professional & MVP

                @HarveyLiu – so to make it absolutely clear:

                 

                This one is a short form for using the var statement for every variable used:

                 

                var link, image, frame, 
                doc = app.activeDocument, 
                links = doc.links, 
                counter = 0;
                

                 

                You could write it like that:

                 

                var link;
                var image;
                var frame;
                var doc = app.activeDocument;
                var links = doc.links;
                var counter = 0;
                

                 

                And then change the var links statement:
                The variable doc is not needed anymore. You can remove that line. Or leave it in, if you want to do something to the active document.

                 

                var link;
                var image;
                var frame;
                var doc = app.activeDocument;
                
                var links = app.documents.everyItem().links.everyItem().getElements();
                
                var counter = 0;
                

                 

                Or you could change the original code like that:

                 

                var link, image, frame,
                doc = app.activeDocument,
                links = app.documents.everyItem().links.everyItem().getElements(),
                counter = 0;
                

                 

                The variable links now holds all the [ object Link ] objects in every document stored in an array made by getElements()

                 

                The for loops accesses this array called by its variable name links now. Do not confuse this name or the array represented by the variable called links with the "links" part of the code in doc.links or in links.everyItem() .

                 

                doc.links is addressing a collection. The collection of every [ object Link ] .

                 

                Uwe

                • 5. Re: Can someone help me to change the script function?
                  Augustinek001 Level 1

                  Hi Harvey,

                   

                  I guess below is what you expect

                  ***********************************************************************

                  var link, image, frame;

                  var openDocs = app.documents;

                  var openDocsLen = openDocs.length;

                  for (var d = 0; d < openDocsLen; d ++){

                  currDoc = openDocs[d], 

                  links = currDoc.links, 

                  counter = 0; 

                   

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

                          if (links[i].status == LinkStatus.LINK_MISSING) { 

                              try { 

                                  link = links[i]; 

                                  image = link.parent; 

                                  frame = image.parent; 

                                  frame.remove(); 

                                  counter++; 

                              } 

                              catch (err) { 

                                  $.writeln(i + " - " + err); 

                              } 

                          } 

                      }

                  }

                  *****************************************************************************

                  Please add an additional code to save the document if you require

                   

                  Regards

                  Augustine

                  • 6. Re: Can someone help me to change the script function?
                    HarveyLiu Level 1

                    all you guys are correct, but can take all of you correct?