10 Replies Latest reply on Feb 2, 2016 8:28 PM by tpk1982

    Pasteboard items information

    tpk1982 Level 4

      Hi,

       

      I need to clear my pasteboard. After the execution i need to know what are the element deleted. If my paste board contains 2 text frames, 4 rectangle and one image call "a.jpg". I need a report something like:

       

      Number of Text frames deleted: 2

      Number of Images deleted: 1

      Image Name: a.jpg

      Number of other elements(exclude text frame and image): 4

       

      So far i used the below codes:

       

      var myResult = confirm("Do you want to Clean PasteBoard?");  
      if(myResult){  
          removePasteboardItems(app.activeDocument);  
          };  
      else{  
          exit();  
          };  
        
      function removePasteboardItems(doc){  
        alert("TRYING TO DO SOMETHING ON: "+doc.name);  
          };  
        
      function removePasteboardItems(/*?Document*/doc) {      
          if( !doc ) return;  
            
          var items = doc.pageItems.everyItem().getElements(),      
          t = null;  
            
          while( t=items.pop() ) t.parentPage || removeItem(t);      
          t = items = null;  
            
          };  
        
      function removeItem(/*PageItem*/item){      
          try {      
              item.locked = false;      
              item.remove();      
              alert(item)
              }      
          catch(_){};  
          };  
      
      
      
      
      //somethin useful to get link name in pasteboard
      
      
      var doc = app.activeDocument;
      var links = doc.links;  
      for(var i = 0; i < links.length; i++)
      {  
      if(links[i].parent.parent.parentPage == null)
      {
              alert(links[i].name)
              links.remove()
          }
      }
      

       

       

      I am not sure about the parent concepts. Can you please help me anyone?

       

      Regards,

      Karthi

        • 1. Re: Pasteboard items information
          Kasyan Servetsky Level 5

          Here are a couple of links to the similar scripts (no need to reinvent a wheel):

          CleanupPasteboard.js by Marc Autret

          Script for deleting objects on pasteboard -- written by someone here on the forum

           

          Every time you remove objects you have to move backwards in a loop:

          Instead of

          for(var i = 0; i < links.length; i++)  
          

          use

          for (var i = links.length - 1; i >= 0; i--) {
          
          • 2. Re: Pasteboard items information
            tpk1982 Level 4

            Hi Kasyan,

             

            Thanks for your reply. The script so far i am using also delete all the paste board objects. But my requirement is the number of links/textframe/object deletion count. So that i asked a report like :

             

            Number of Text frames deleted: 2

            Number of Images deleted: 1

            Image Name: a.jpg

            Number of other elements(exclude text frame and image): 4

             

            All the scripts simply deleting objects.

             

            Regards,

            Karthi

            • 3. Re: Pasteboard items information
              Kasyan Servetsky Level 5

              Hi Karthi,

               

              Here's an example illustrating my approach:

              var scriptName = "Delete objects on pasteboard",
              doc;
              
              PreCheck();
              
              function Main() {
                  var obj,
                  objs = doc.pageItems.everyItem().getElements(),
                  count = 0;
                 
                  for (var i = objs.length - 1; i >= 0; i--) {
                      obj = objs[i];
                      obj.select();
                      if (obj.parent instanceof Spread) {
                          if (obj.locked) {
                              obj.locked = false;
                          }
                     
                          try {
                              obj.remove();
                              count++;
                          }
                          catch(err) {
                              $.writeln(err.message + ", line: " + err.line);
                          }
                      }
                  }
              
                  var report = count + " item" + ((count == 1) ? " was" : "s were") + " removed.";
                  alert("Finished. " + report, scriptName);
              }
              
              function PreCheck() {
                  if (app.documents.length == 0) ErrorExit("Please open a document and try again.", true);
                  doc = app.activeDocument;
                  if (doc.converted) ErrorExit("The current document has been modified by being converted from older version of InDesign. Please save the document and try again.", true);
                  if (!doc.saved) ErrorExit("The current document has not been saved since it was created. Please save the document and try again.", true);
                  Main();
              }
              
              function ErrorExit(error, icon) {
                  alert(error, scriptName, icon);
                  exit();
              }
              
              

               

              You can develop it further: check each item if it's instanceof or its constructor.name is TextFrame, or a rectangle containing a link to image, etc. and create a more complex report. But that's homework for you.

               

              Regards,
              Kasyan

               

              P.S. In my version if an object is locked, the script unlocks and deletes it; but if it's on a locked layer, it's skipped.

              • 4. Re: Pasteboard items information
                tpk1982 Level 4

                Hi Kasyan,

                 

                Thanks for your time for me. One small problem when i do homework.

                 

                var scriptName = "Delete objects on pasteboard", 
                doc; 
                
                PreCheck(); 
                
                function Main() { 
                    var obj, 
                    objs = doc.pageItems.everyItem().getElements(), 
                    count = 0;  txtFrame = rect = link = 0;
                     
                    for (var i = objs.length - 1; i >= 0; i--) { 
                        obj = objs[i]; 
                        obj.select(); 
                        if (obj.parent instanceof Spread) { 
                            if (obj.locked) { 
                                obj.locked = false; 
                            } 
                            try { 
                  obj.remove(); 
                  count++; 
                  if(obj instanceof TextFrame) { txtFrame++;}
                  if(obj instanceof Rectangle) {rect++;}
                  if(obj instanceof Link ) {link++;}
                  } 
                            catch(err) { 
                                alert(err.message + ", line: " + err.line); 
                            } 
                        } 
                    } 
                
                    var report = count + " item" + ((count == 1) ? " was" : "s were") + " removed."; 
                  var report = report + "Rectangles:"+rect+"TextFrames:"+txtFrame+" Link:"+link;
                    alert("Finished. " + report, scriptName); 
                } 
                
                function PreCheck() { 
                    if (app.documents.length == 0) ErrorExit("Please open a document and try again.", true); 
                    doc = app.activeDocument; 
                    if (doc.converted) ErrorExit("The current document has been modified by being converted from older version of InDesign. Please save the document and try again.", true); 
                    if (!doc.saved) ErrorExit("The current document has not been saved since it was created. Please save the document and try again.", true); 
                    Main(); 
                } 
                
                function ErrorExit(error, icon) { 
                    alert(error, scriptName, icon); 
                    exit(); 
                } 
                

                 

                 

                The link is considered as rectangle even i use itemlink, link, graphic and allgraphics.

                 

                The problem is in line 23.

                 

                Thanks,

                Karthi

                • 5. Re: Pasteboard items information
                  Kasyan Servetsky Level 5

                  Hi Karthi,

                   

                  I suggest to check allGraphics property to count how many images the object contains. (in your original post you mentioned images).

                  You can’t check a property after the object has been removed because it doesn’t exist any more (obj.isValid returns false).

                  I tested this in CS3 and it works for me, but it removes all the objects (also on pages) in CC 2014. If this happens in you version of InDesign, you can try another approach. Check out this article.

                  var scriptName = "Delete objects on pasteboard",
                  doc;
                  
                  PreCheck();
                  
                  function Main() {
                      var obj, howManyImages,
                      objs = doc.pageItems.everyItem().getElements(),
                      countTotal = countTextFrame = countRectangle = countImage = 0;
                      
                      for (var i = objs.length - 1; i >= 0; i--) {
                          obj = objs[i];
                  
                          if (obj.parent instanceof Spread) {
                              if (obj.locked) {
                                  obj.locked = false;
                              }
                          
                              try {
                                  howManyImages = obj.allGraphics.length;
                                  obj.remove();
                                  countTotal++;
                                  countImage += howManyImages;
                                  
                                  if (obj instanceof TextFrame) {
                                      countTextFrame++;
                                  } 
                              
                                  if (obj instanceof Rectangle) {
                                      countRectangle++;
                                  }
                              }
                              catch(err) {
                                  $.writeln(err.message + ", line: " + err.line);
                              }
                          }
                      }
                  
                      var report = (countTotal == 0) ? "Nothing has been removed." :
                                          "In total " + countTotal + " object" + ((countTotal == 1) ? " was" : "s were") + " removed.\nIncluding:\n\n" + 
                                          countTextFrame + " text frame" + ((countTextFrame == 1) ? "" : "s") + "\n" + 
                                          countRectangle + " rectangle" + ((countRectangle == 1) ? "" : "s") + "\n" + 
                                          countImage + " image" + ((countImage == 1) ? "" : "s");
                  
                      alert(report, scriptName);
                  }
                  
                  function PreCheck() {
                      if (app.documents.length == 0) ErrorExit("Please open a document and try again.", true);
                      doc = app.activeDocument;
                      if (doc.converted) ErrorExit("The current document has been modified by being converted from older version of InDesign. Please save the document and try again.", true);
                      if (!doc.saved) ErrorExit("The current document has not been saved since it was created. Please save the document and try again.", true);
                      Main();
                  }
                  
                  function ErrorExit(error, icon) {
                      alert(error, scriptName, icon);
                      exit();
                  }
                  

                   

                  Regards,

                  Kasyan

                  • 6. Re: Pasteboard items information
                    tpk1982 Level 4

                    Hi Kasyan,

                     

                    Thanks for revised script. Yes it is deleting all the page items in CS6.

                     

                    I changed Line 14 to

                     

                    if(obj.parentPage == null) { 
                    

                     

                     

                    Now it is deleting only pasteboard item . Is it correct or it have any side effect?

                     

                    Thanks,

                    Karthi

                    • 7. Re: Pasteboard items information
                      Kasyan Servetsky Level 5

                      I made a quick test and with your adjustment it works for me as expected.

                      Note that one object may contain several images – e.g. two grouped images – in such a case, you’ll get: in total 1 object including  two images.

                       

                      Kasyan

                      • 8. Re: Pasteboard items information
                        tpk1982 Level 4

                        Hi Kasyan,

                         

                        Yes you are correct. But my main objective is getting image count and deleted image names. I will develop those in further. Beside this i need to unlock all layers first as per your advice.

                         

                        TERRIBLE THANKS FOR YOUR KIND HELP.

                         

                        Regards,

                        Karthi

                        • 9. Re: Pasteboard items information
                          Kasyan Servetsky Level 5

                          Alternatively you can loop through the links first getting the list of deleted images, like so:

                          Main();
                          
                          function Main() {
                              var doc = app.activeDocument,
                              links = doc.links,
                              link, image, rectangle;
                              
                              for (var i = links.length - 1; i >= 0; i--) {
                                  link = links[i];
                                  image = link.parent;
                                  rectangle = image.parent;
                                  
                                  if (rectangle.itemLayer.locked) {
                                      rectangle.itemLayer.locked = false; // unlock layer if it's locked
                                  }
                          
                                  if (rectangle.parentPage == null) {
                                      $.writeln("Removing image: " + link.name);
                                      rectangle.remove();
                                  }
                                  else {
                                      $.writeln(link.name + " is on page. Don't touch it.");
                                  }
                              }
                          }
                          

                          In the example above I show you how to unlock a locked layer before removing an object it's located on.

                          • 10. Re: Pasteboard items information
                            tpk1982 Level 4

                            Hi Kasyan,

                             

                            Genius you are . Here is the final script which i am going to use. It will unlock layers and get image names as well.

                             

                            var scriptName = "Delete objects on pasteboard",  
                            doc;  
                              
                            PreCheck();  
                              
                            function Main() {  
                                var obj, howManyImages,  
                                objs = doc.pageItems.everyItem().getElements(), 
                                countTotal = countTextFrame = countRectangle = countImage = 0;  
                                var lnkname=[];
                                
                                 var layers=doc.layers;
                                for(var i=0;i<layers.length;i++){
                                if (layers[i].locked){
                                   layers[i].locked=false;
                                 }
                               }
                                
                                var links = doc.links;  
                                for(var j = 0; j < links.length; j++){  
                                if(links[j].parent.parent.parentPage == null){
                                    lnkname.push(links[j].name);
                                }
                                }  
                            
                            
                                  
                                for (var i = objs.length - 1; i >= 0; i--) {  
                                    obj = objs[i];  
                                    //lnkname.push(doc.allGraphics[i].itemLink.name);
                            
                            
                            //var doc = app.activeDocument;
                                   if(obj.parentPage == null) {  
                                        if (obj.locked) {  
                                            obj.locked = false;  
                                        }  
                                      
                                        try {  
                                            howManyImages = obj.allGraphics.length;                                  
                                            obj.remove();  
                                            countTotal++;  
                                            countImage += howManyImages;
                            
                            
                                        if (obj instanceof TextFrame) {  
                                                countTextFrame++;  
                                            }   
                                          
                                            if (obj instanceof Rectangle) {  
                                                countRectangle++;  
                                            }  
                                        }  
                                        catch(err) {  
                                            $.writeln(err.message + ", line: " + err.line);  
                                        }  
                                    }  
                                }  
                              
                                var report = (countTotal == 0) ? "Nothing has been removed." :  
                                                    "In total " + countTotal + " object" + ((countTotal == 1) ? " was" : "s were") + " removed.\nIncluding:\n\n" +   
                                                    countTextFrame + " Text Frame" + ((countTextFrame == 1) ? "" : "s") + "\n" +   
                                                    Number(countRectangle-countImage) + " Rectangle" + ((countRectangle == 1) ? "" : "s") + "\n" +   
                                                    countImage + " Image" + ((countImage == 1) ? "" : "s" + "\nImage Name - \n"+lnkname.join("\n"));  
                              
                                alert(report, scriptName);  
                            }  
                              
                            function PreCheck() {  
                                if (app.documents.length == 0) ErrorExit("Please open a document and try again.", true);  
                                doc = app.activeDocument;  
                                if (doc.converted) ErrorExit("The current document has been modified by being converted from older version of InDesign. Please save the document and try again.", true);  
                                if (!doc.saved) ErrorExit("The current document has not been saved since it was created. Please save the document and try again.", true);  
                                Main();  
                            }  
                              
                            function ErrorExit(error, icon) {  
                                alert(error, scriptName, icon);  
                                exit();  
                            }  
                            

                             

                             

                            Thanks again for your involvement in this issue.

                             

                            Regards,

                            Karthi