3 Replies Latest reply on Oct 15, 2009 8:37 AM by billy5266

    Help with PageItems Please

    billy5266

      Hello masters of this forum...  I have a question.

       

      Here's what I want to do:  I have links (graphics) on my indesign pages... I would like to remove some of these links based on the name... but also remove the box where the graphic is... because if I remove just the graphic the box still there... so I decided to make a loop to all the page elements and see if inside the element has some graphics... check the name of the link and then decide if I have to remove the element... something like...

       

      For page 2:

       

      for elemento = 1 to app.activeDocument.Pages(2).PageItems.count

           if app.activeDocument.Pages(2).PageItems(elemento).allGraphics.count <> 0 then

                for grafico = 1 to app.activeDocument.Pages(2).PageItems(elemento).allGraphics.count

                     if app.activeDocument.Pages(2).PageItems(elemento).allGraphics(grafico).itemlink.name = "test.tif" then

                          app.activeDocument.Pages(2).PageItems(elemento).delete

                     end if

                next

           end if

      next

       

      THE PROBLEM IS... when removing the box, it will also delete the graphic inside the box (2 elements, which is good... because I will kill the box and the graphic), but... because I'm reading an array of pageItems when I get reference to the graphic item I will get an error because the graphic (the item) has already been removed when removing the box... is there a way to check if a PageItem exist before whe make reference to it?? thanks for your help!

       

      Guillermo.

       

      PD.  I can use ON ERROR RESUME NEXT... but I think that's not a professional way to do it or should I??

        • 1. Re: Help with PageItems Please
          [Jongware] Most Valuable Participant

          You are correct, using an error handler is ... bad style. And for a long script, it might make things run slower.

           

          Your problem is one that pops up when doing a number of actions in InDesign. The program updates the PageItems and Graphs counts on each edit, and that means the for ..next loops are messed up. There is, fortunately, a standard work-around this: loop backwards instead!

           

          for elemento = app.activeDocument.Pages(2).PageItems.count to 1 step -1

           

          (I think I got the VB syntax right.) In effect, the page items are still updated when you delete one, but it doesn't matter anymore as the only ones that wil be effected are already processed.

           

          [Note] You might want to try your other idea again -- using allGraphics directly should be much faster. You can "go" from an allGraphics array element -- a Graphic object -- to its container, simply by using the allGraphics parent property. That always points directly to the frame the image is in. Apart from any VB syntax errors, something like this ought to work:

           

          app.activeDocument.Pages(2).AllGraphics(elemento).Parent.delete

           

          Looping backwards over the AllGraphics array, of course. Just remember always to do that when you are changing stuff.

          • 2. Re: Help with PageItems Please
            billy5266 Level 1

            Thank you... very creative solutions... I will test both in my script and will post back... thank you!!!

            • 3. Re: Help with PageItems Please
              billy5266 Level 1

              The sole solution

               

              app.activeDocument.pages(pagina).allGraphics(grafica).parent.delete()

               

              work ONLY removing the parent graphic... but the error still... I tried to invert the loop the way you said in conjunction with the first one and seems to be working fine... thanks for your colaboration!!!

               

              Billy.