Listing graphics in order of appearance

Jonathan asked on the FrameScript list on yahoo groups about getting a list of graphic files in a document in order of appearance. I decided to answer it here so I can discuss it in some detail and because yahoo groups doesn’t format code listings very well. There are a couple of possibilities to consider. First, if you don’t have any graphics in tables, you can do something simple like this:

// Set a variable for the active document.
Set oDoc = ActiveDoc;

// Loop through all of the anchored frames in the main text flow.
Get TextList InObject(oDoc.MainFlowInDoc) FrameAnchor NewVar(tTextList);
Loop While(i <= tTextList.Count) LoopVar(i) Init(1) Incr(1)
  // Get each anchored frame.
  Set oAFrame = tTextList[i].TextData;
  // Get the information on the imported graphic(s) inside the text frame here.
  // ...
EndLoop

If you have anchored frames inside of tables, then things get a little more complicated. Here is a basic loop for processing all of the paragraphs in document order, including those in tables:

// Set a variable for the active document.
Set oDoc = ActiveDoc;

// Set a variable for the first paragraph in the main text flow.
Set oPgf = oDoc.MainFlowInDoc.FirstPgfInFlow;
// Loop through the paragraphs in the main text flow.
Loop While(oPgf)
  
  // Process any tables in the paragraph.
  Get TextList InObject(oPgf) TblAnchor NewVar(tTextList);
  Loop While(i <= tTextList.Count) LoopVar(i) Init(1) Incr(1)
    Set iResult = ProcessTable{tTextList[i].TextData};
  EndLoop
  Set oPgf = oPgf.NextPgfInFlow;
EndLoop

Function ProcessTable oTbl
//
Local oCell(0), oPgf(0);

If oTbl.TblTitlePosition = TblTitleAbove
  Set oPgf = oTbl.FirstPgf;
  Loop While(oPgf)
    
    Set oPgf = oPgf.NextPgfInFlow;
  EndLoop
EndIf
Set oCell = oTbl.FirstRowInTbl.FirstCellInRow;
Loop While(oCell)
  Set oPgf = oCell.FirstPgf;
  Loop While(oPgf)
  
    Set oPgf = oPgf.NextPgfInFlow;
  EndLoop
  Set oCell = oCell.NextCellInTbl;
EndLoop
If oTbl.TblTitlePosition = TblTitleBelow
  Set oPgf = oTbl.FirstPgf;
  Loop While(oPgf)
    
    Set oPgf = oPgf.NextPgfInFlow;
  EndLoop
EndIf
//
EndFunc //--------------------------------------------------------------------

This is a general purpose script for processing paragraphs in document order. Lines 8, 24, 32, and 40 are where you do something with each paragraph. In this case, we can call a function on each of these lines to get any anchored frames that are in each paragraph. The function would be similar to the first code example, except that it would get any anchored frames in each paragraph instead of the entire main flow.

Function ProcessParagraph oPgf
//
// Loop through all of the anchored frames in the paragraph.
Get TextList InObject(oPgf) FrameAnchor NewVar(tTextList);
Loop While(i <= tTextList.Count) LoopVar(i) Init(1) Incr(1)
  // Get each anchored frame.
  Set oAFrame = tTextList[i].TextData;
  // Get the information on the imported graphic(s) inside the text frame here.
  // ...
EndLoop
//
EndFunc //--------------------------------------------------------------------

There are some unfinished pieces here, like how to query each anchored frame for imported graphic information. Also, you will need a way to collect the graphic data. But this will get you started. Questions and comments are welcome. If there is enough discussion generated, we can go further with this.

Leave a Reply

Your email address will not be published. Required fields are marked *