5 Replies Latest reply on Apr 4, 2013 3:54 AM by oxyde68

    Remove specific elements

    oxyde68

      Hi everybody ,

       

      I'm trying a little script in Javascript for InDesign.

      Before exporting my final PDF from InDesign, i want delete all TextFrame empty and Graphic item with link to a specific image ( blank_image.png ).

       

      In the following script, i select only TextFrame ( test if content is empty, and hide/lock the element in the layer panel ) and Images ( test if Link is equal to blanc_image.png and hide/lock the element in the layer panel ) ... But it does not work very well

       

       

      var MyDoc = app.activeDocument;
      var Test = MyDoc.allPageItems;
      
      for(a=0;a<Test.length;a++)
      { var ParentOf = Test[a].parent;
          var NameOf = Test[a].constructor.name;
          
      switch(NameOf)
      { 
          case "TextFrame" : 
          
          var TxtValeur = Test[a].contents;    
              if(TxtValeur == "" || TxtValeur.length == 0)
              { 
                  Test[a].locked = true;
                Test[a].visible = false;
      // i want here lock and hide the element in the layer panel ( before delete this ) to check there is no problem
                  }
          break;
          
              case "Image" : 
          
              if(Test[a].itemLink.name == "blank_image.png")
              {  
                  Test[a].locked = true;
                Test[a].visible = false;
      // i want here lock and hide the element in the layer panel ( before delete this ) to check there is no problem
                  
                  }
                  break;
                  
          }
          
          }
      
      

       

       

      I have read the Indesign CS6 Scripting guide, but i don't understand how item and Pageitem functions work ... anyone have some example for me ?

      Thanks a lot for your attention.

       

      Have a good day

        • 1. Re: Remove specific elements
          Vamitul Level 4

          can you tell us what doesn't work? what error?

          • 2. Re: Remove specific elements
            [Jongware] Most Valuable Participant

            I think I know what the problem is with this code.

             

            You can lock and hide a *regular* page item, but not an image. An Image is something *inside* a regular page item; it's something you select with the *white* arrow, not the usual black selection arrow. If you want to hide and/or lock the image, you have to do so with its *parent* -- which is the object 'in' which the image appears.

             

            You already retrieve the parent of all of your items near the top of the loop, but you only have to do so for the "Image" case.

             

            (Post-Thought Edit) Hmm ... are you sure that 'allPageItems' include "Image" objects as well? After all, as I said above, an 'image' is *not* a regular 'page item'.

            1 person found this helpful
            • 3. Re: Remove specific elements
              oxyde68 Level 1

              There are no error while script is working, but the layer don't lock and don't hide at end.

              And some graphic and text frame aren't scanned by the script. Only one is find.

              How specifically lock the item when conditions are validated ?

               

              How to use the item function ?

              What is the correct structure to use page item ?

               

               

              app.activeDocument.pages.pageitems(0).items(0) 
              

              ???

               

              Thanks

              • 4. Re: Remove specific elements
                oxyde68 Level 1

                Thanks Jongware for your help, when i try

                 

                Test[a].parent.locked = true;
                

                 

                All work good here

                 

                But now, some TextFrame in my document have Tag XML, so they are not empty.

                When the sript is run, the file is in the final part of execution, we no longer need XML Tag on the empty elements.

                 

                I search a part of code for untag TextFrame who are empty.

                Some TextFrame have been tagged several times and appear like this :


                [NAME] [/NAME]
                [NAME] [/NAME]
                
                

                 

                Anyone can help me ?

                I find a part of code for search with GREP and the code work when i do Find/Remplace in my indesign, but not directly on the script.

                 

                Message was edited by: oxyde68

                • 5. Re: Remove specific elements
                  oxyde68 Level 1

                  After few tests, script work good with graphics but not with text.

                  When i test if textframe is empty, all the textframe don't be affected because some have XML tags inside.

                   

                  I think there are some ways to explore :

                   

                  - Untag text frame and test if content is empty or equal to "   " ( = some spaces ) and delete it.

                  => When i untag textFrame, some style and text move, it's not a good solution.

                  I think test if length is less 5 : untag it.

                   

                  - Do a search ( like search/replace in indesign app ) with GREP ( search(/^[\s\r\n]+$/))and if is true, delete it.

                  I find this method on some forums, when i use the formula in indesign with Search/Replace GREP it work, but when i use the same formula in the script it find nothing ...

                   

                  - Get the exact code ( with invisible characters and tag ) for test if the content is the same

                  But how to get the code of invisible characters ?

                   

                  Thanks