Skip navigation
Currently Being Moderated

Deleting anchored frames

Feb 7, 2012 3:22 AM

Hello all,

 

What happens with a linked list of objects when I use the Delete method on one of these objects ? The FrameMaker Scripting Guide does not say much about this, nor does the FDK reference guide. I have tried the following but it does not work as I expected:

 

doc = app.ActiveDoc;

theGraph = doc.FirstGraphicInDoc;

n = 0;

 

while ( theGraph.ObjectValid() ) {

          tobedeleted = false;

          if ( theGraph.constructor.name == "AFrame" ) {

    theObject = theGraph.FirstGraphicInFrame;

    if ( theObject.ObjectValid() )

      if ( theObject.constructor.name == "TextFrame" )

        tobedeleted = true;

    }

    if ( tobedeleted ) {

      nextGraph = theGraph.NextGraphicInDoc;

      theGraph.Delete();

      n++

      theGraph = nextGraph;

    } else

        theGraph = theGraph.NextGraphicInDoc;

}

alert ( n );

 

The document has 18 of the specified anchored frames (with embedded text frames), but the above code only deletes 5. If I just walk through the list and skip deleting the objects, the count goes up to 18, as expected. So there must be some crazy side effect of deleting an object from a linked list. But what exactly ?

 

How can I walk through a linked list and delete some objects from them without disrupting the linked list ?

 

Jang

 
Replies
  • Currently Being Moderated
    Feb 7, 2012 4:51 AM   in reply to 4everJang

    Hi Jang,

     

    You might find this tutorial useful:

     

    http://frameexpert.com/tutorials/loopsandlinkedlists.htm

     

    The code is FrameScript, but the concepts are the same in ExtendScript and the FDK. Please let me know if you have any questions or comments.

     

    Rick

     
    |
    Mark as:
  • Currently Being Moderated
    Feb 7, 2012 6:21 AM   in reply to 4everJang

    I'll have to take some time to try your code and try to understand what is going on. In the meantime, another approach is to loop through the anchored frames save the objects that you want to delete in an array. Then, you can loop through the array and delete each object. It may be a bit less efficient, but each loop can be very simple.

     

    var doc = app.ActiveDoc;
    var graphic = doc.FirstGraphicInDoc;
    var textFrame;
    var toDelete = [ ];

     

    while(graphic.ObjectValid()) {
      if (graphic.constructor.name === "AFrame") {
        textFrame = graphic.FirstGraphicInFrame;
        if (textFrame.ObjectValid()) {
          if (textFrame.constructor.name === "TextFrame") {
            toDelete.push(graphic); // Store the frame in the array.
          }
        }
      }
      graphic = graphic.NextGraphicInDoc;
    }

     

    // Loop through the array of anchored frames and delete each one.

    for (var i = 0; i < toDelete.length; i += 1) {
      toDelete[i].Delete();
    }

     
    |
    Mark as:
  • Currently Being Moderated
    Feb 8, 2012 8:53 AM   in reply to 4everJang

    Hi Jang,

     

    I orginally developed this for deleting certain elements in a structured document, since these are in a tree and not a linked list and this is the only way to do it reliably.

     

    Rick

     
    |
    Mark as:
  • Currently Being Moderated
    Feb 8, 2012 11:30 AM   in reply to 4everJang

    I agree, FrameSLT is excellent. I am sure you have seen my blog post on FrameSLT:

     

    http://frameautomation.com/2010/03/15/frameslt-a-great-framemaker-plug in/

     

    Rick

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points