2 Replies Latest reply on Aug 18, 2011 7:02 AM by Danny Noonan

    [JS][CS5]My Script Won't Delete Pasteboard Items In CS5

    Danny Noonan Level 1

      Hello All,

      We've used this script in CS4 for years but now I'm trying it in CS5 and it deletes everything, not just the items on the pasteboard. I know there were some changes with pageItems in CS5 and I suspect it has something to do with the problem, but I don't see it. Any help would be greatly appreciated as usual. Also, I've pulled this out of a script Dave Saunders created for us. I would like to use it in another script I am currently working on. I know it's not pretty like it was when Dave created it, but I've tested this in CS4 and it works, if you can clean it up at all too, that would be awesome.

       

      var myDoc = app.activeDocument;
      
      
      deleteAllPasteboardItems(myDoc);
      
      function deleteAllPasteboardItems(myDoc) {
                var myPIs = myDoc.pageItems.everyItem().getElements();
                for (var j = myPIs.length - 1; j >= 0; j--) {
                     var page = findPage(myPIs[j]);
                     if (page === null) {
                          // must be on Pasteboard -- can't be overset
                          try
                          {
                               myPIs[j].locked=false;
                               myPIs[j].remove();
                          }
                         catch (err)
                          {
                          }
                     }
                }
           }
      
      
      
      
           function findPage(theObj) {
                if (theObj.hasOwnProperty("baseline")) {
                     theObj = theObj.parentTextFrames[0];
                }
                while (theObj != null) {
                     var whatIsIt = theObj.constructor;
                     switch (whatIsIt) {
                          case Page : return theObj;
                          case Character : theObj = theObj.parentTextFrames[0]; break;
                          case Cell : theObj = theObj.insertionPoints[0].parentTextFrames[0]; break;
                          case Note : ; case Footnote : theObj = theObj.storyOffset; break;
                          case Application : return null;
                     }
                     if (theObj == null) return null;
                     try
                     {
                          theObj = theObj.parent;
                     }
                     catch (ignored)
                     {
                     }
                }
                return theObj
           } // end findPage
      
       
      

       

      Thanks again,

       

      Danny

        • 1. Re: [JS][CS5]My Script Won't Delete Pasteboard Items In CS5
          Marc Autret Level 4

          Hi Danny,

           

          In ID CS5 any top-level page item belongs to a spread even if it is located on a page, so your findPage function does not work anymore. Fortunately the DOM now offers a parentPage property, which returns null if the object has no parent page (=does not hit any page).

           

          The following snippet should do the job:

           

          // ===============================================
          // Remove Pasteboard Items / InDesign CS5+
          // ===============================================
          
          removePasteboardItems(app.documents.length && app.activeDocument);
          
          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();
                  }
              catch(_){}
          }
          

           

          @+

          Marc

          • 2. Re: [JS][CS5]My Script Won't Delete Pasteboard Items In CS5
            Danny Noonan Level 1

            Very nice. Thanks for the explaination and the script, it does exactly what I need it to. The parentPage property makes it much easier than it used to be!

             

            Thanks again Marc,

             

            Danny