Skip to main content
Participant
February 15, 2022
Answered

How to use scripting to select anchored frame.

  • February 15, 2022
  • 1 reply
  • 1046 views

I am a novice in frame maker.I'm trying to teach myself to script. I have a basic understanding of programming syntax and a passing familiarity with basic JavaScript.I want to know how to select a blank Anchored Frame.

 

var doc=app.ActiveDoc;

var flow=doc.MainFlowInDoc;

var textFM=flow.FirstTextFrameInFlow;

var aFM=textFM.FirstAFrame;

 

Or explain how to use scripting to insert a file into an anchor frame (there are many anchored frame in the file, which need to be traversed and inserted into other files or pictures in order).

This topic has been closed for replies.
Correct answer K.Daube

May I chime in here concerning the first question ... (FM-15)

Walking through the anchored frames of various documents with the script below shows a strange behaviour:

In a new document with newly inserted AFrames all are found/reported.

In existing documents - even MIF washed - in some document all AFrames are found/reported, but in some documents only few AFrames are found/reported (After restarting FM and/or the script always the same AFrames).

What may be the cause of such erratic behaviour?

 

 

 

/* E:\_support+consultancy\!_FM-community\GetFirstEmptyFrame1.jsx ==== UTF-8
History       2022-02-15
*/ ; // =====================================================================

//@target framemaker

function DisplayObj (oDoc, obj) { // ----------------------------------------
    obj.GraphicIsSelected = true;
    oTLbeg = new TextLoc(obj,0);
    oTLend = new TextLoc(obj,Constants.FV_OBJ_END_OFFSET);
    oTR    = new TextRange(oTLbeg,oTLend);
    oDoc.TextSelection = oTR;
    oDoc.ScrollToText(oTR);
} //--- end DisplayObj ------------------------------------------------------

function main () { // <><><><><><><><><><><><><><><><><><><><><><><><><><><><
var oDoc, oFlow, oTFrm, oAFrm;

  oDoc=app.ActiveDoc;
  if (!oDoc.ObjectValid()) {
    alert ("A document must be active");
    return;
  }
  oFlow= oDoc.MainFlowInDoc;
  oTFrm= oFlow.FirstTextFrameInFlow;
  while (oTFrm.ObjectValid()) {
    oAFrm= oTFrm.FirstAFrame;
    while (oAFrm.ObjectValid()) {
      DisplayObj (oDoc, oAFrm);          // display this anchored frame
      oAFrm = oAFrm.NextAFrame;
alert ("Aframe found");
    }
    oTFrm = oTFrm.NextTextFrameInFlow;
    DisplayObj (oDoc, oTFrm);            // display this text frame
  }
} //--- end main ------------------------------------------------------------

main ();

 

 

 

1 reply

Legend
February 15, 2022

Hi,

 

Inserting a file into an anchored frame with ExtendScript follows a similar pattern as the same action in the UI. Basically, you have to select the frame with code (unless it is already selected), then invoke the code version of the File > Import command.

 

To select a frame, you need the frame object. Your code is correct to get the first frame in the main flow, as long as it is in the first text frame of the flow. To select it with code, you just need one more line:

 

var doc=app.ActiveDoc;
var flow=doc.MainFlowInDoc;
var textFM=flow.FirstTextFrameInFlow;
var aFM=textFM.FirstAFrame;
aFM.GraphicIsSelected = true;

 

After that, you can do a very simple import with something like this:

 

ImportGraphic(doc, "C:\\path\\to\\graphic.png");
function ImportGraphic(doc, path)
{
    var index;

    var props = GetImportDefaultParams();

    index = GetPropIndex(props, Constants.FS_HowToImport);
    props[index].propVal.ival = Constants.FV_DoByRef;

    var tr = doc.TextSelection;

    var returnp = new PropVals();
    var file = doc.Import(tr, path, props, returnp);
}

 

But, there are lots of things to consider here...

 

- There are many different import parameters you can set to control how the import happens. Hopefully this code sample gives you a place to start and you can look up the rest of the options.

- If you are traversing many different frames, you will need to develop the logic for that. There is the NextAFrame property to step through frames within a flow, but you will have to figure out the logic to know what frame you have selected. An AFrame is just an AFrame like any other, without some other identifying factor. So, this will depend on your specific use case.

 

...and other things. There are many complications involved and this is as much as I can provide right now. I hope this helps.

Russ

 

 

 

 

 

K.Daube
Community Expert
K.DaubeCommunity ExpertCorrect answer
Community Expert
February 15, 2022

May I chime in here concerning the first question ... (FM-15)

Walking through the anchored frames of various documents with the script below shows a strange behaviour:

In a new document with newly inserted AFrames all are found/reported.

In existing documents - even MIF washed - in some document all AFrames are found/reported, but in some documents only few AFrames are found/reported (After restarting FM and/or the script always the same AFrames).

What may be the cause of such erratic behaviour?

 

 

 

/* E:\_support+consultancy\!_FM-community\GetFirstEmptyFrame1.jsx ==== UTF-8
History       2022-02-15
*/ ; // =====================================================================

//@target framemaker

function DisplayObj (oDoc, obj) { // ----------------------------------------
    obj.GraphicIsSelected = true;
    oTLbeg = new TextLoc(obj,0);
    oTLend = new TextLoc(obj,Constants.FV_OBJ_END_OFFSET);
    oTR    = new TextRange(oTLbeg,oTLend);
    oDoc.TextSelection = oTR;
    oDoc.ScrollToText(oTR);
} //--- end DisplayObj ------------------------------------------------------

function main () { // <><><><><><><><><><><><><><><><><><><><><><><><><><><><
var oDoc, oFlow, oTFrm, oAFrm;

  oDoc=app.ActiveDoc;
  if (!oDoc.ObjectValid()) {
    alert ("A document must be active");
    return;
  }
  oFlow= oDoc.MainFlowInDoc;
  oTFrm= oFlow.FirstTextFrameInFlow;
  while (oTFrm.ObjectValid()) {
    oAFrm= oTFrm.FirstAFrame;
    while (oAFrm.ObjectValid()) {
      DisplayObj (oDoc, oAFrm);          // display this anchored frame
      oAFrm = oAFrm.NextAFrame;
alert ("Aframe found");
    }
    oTFrm = oTFrm.NextTextFrameInFlow;
    DisplayObj (oDoc, oTFrm);            // display this text frame
  }
} //--- end main ------------------------------------------------------------

main ();

 

 

 

Participant
February 16, 2022

1. Just traverse and select the anchored frame document, and the imported file will appear in the first or second anchored frame.

2. The window scrolling code above can solve the problem of import. But who can explain why.