1 Reply Latest reply on Feb 9, 2012 1:12 PM by gappster

    pageItems.ItemByName() deletes frame


      I am working on script that does the following:


      1. Reads some xml and gets two bits of data: componentName and componentText

      2. Iterates through the current InDesign document looking for text frames whose 'name' is equal to the 'componentName'

      3. If the script finds a frame on any of the pages that matches the 'componentName' and adds the text to text frame

      4. if the script does not find a qualifying frame it will add the frame to first page of the document.


      In one of my functions I have the following:



      pageItemByName = docReference.pages.item(q).pageItems.itemByName(theComponentName);



      'q' is the current page of the document and 'theComponentName' is the name of the frame that I am looking for.





      The above command ends up deleting the first frame that is created.


      For instance if I have four components: head, subhead, body, caption the script will do the following:


      1. Read the first component, look through the three page document that does _not_ contain any text frames

      2. Not find any frames with the 'name' of 'head' on any of the pages

      3. Create the frame on the first page with the 'name' of 'head' and place the text into the frame

      4. Read the second component, look at the first page using the command above, delete the 'head' frame, create a new frame called 'subhead' and put the correct text into the frame

      5. The script will finish its work and will have created all four frames, deleted the first and left the other three on the first page.


      This will also happen if I create the four frame before running the script and the four frames are _not_ on the same page.


      If the frames are on the same page everything works as expected.


      I thought at first I was passing some reference or variable into the code but at this point I'm a bit stumped


      Any thoughts appreciated.






        • 1. Re: pageItems.ItemByName() deletes frame
          gappster Level 1

          Here's what I did to resolve this:


          This, as noted above, was the original call:



          pageItemByName = docReference.pages.item(q).pageItems.itemByName(theComponentName);



          The idea here was that I could be lazy and just ask for the 'componentName' and get back the first reference on the page and use that reference. Clearly that wasn't working for me.


          I went a different route and asked for the pageItems, counted them and asked each for its name.


          Like this:



          for( var m = 0; m < pageItemsListCount; m++){


                                                  var pageItemsReference = pageItemsList[m];

                                                  var pageItemsReferenceName = pageItemsReference.name;


            do some stuff




          With a bit more testing of the returned frame this resolved the problem for me. I'm still not sure why the first try failed.