• How to use scripting to select anchored frame.

# How to use scripting to select anchored frame.

New Here ,
Feb 14, 2022 Feb 14, 2022

Copied

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).

TOPICS
Scripting

Views

131

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Adobe Community Professional , Feb 15, 2022 Feb 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?...

Likes

7 Replies 7
Mentor ,
Feb 15, 2022 Feb 15, 2022

Copied

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

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Feb 15, 2022 Feb 15, 2022

Copied

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;
}
oTFrm = oTFrm.NextTextFrameInFlow;
DisplayObj (oDoc, oTFrm);            // display this text frame
}
} //--- end main ------------------------------------------------------------

main ();

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
New Here ,
Feb 16, 2022 Feb 16, 2022

Copied

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.

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Feb 16, 2022 Feb 16, 2022

Copied

Klaus, try deselecting the current anchored frame before going on to the next one.

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Feb 19, 2022 Feb 19, 2022

Copied

Thanks Rick for the tip, however

Only in a newly set up document 10 of 10 existing AFrames are found. In other documents it is 14 of >100, 0 of 3, 0 of 4, 20 of 64...

And: These figures can be reproduced even after closing the files, FM and then redoing the tests.

Very mysterious...

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Feb 19, 2022 Feb 19, 2022

Copied

What happens if you do it like this?

function getAFrames (doc) {

var frames, graphic;

frames = [];

graphic = doc.FirstGraphicInDoc;
while (graphic.ObjectValid () === 1) {
if (graphic.constructor.name === "AFrame") {
frames.push (graphic);
}
graphic = graphic.NextGraphicInDoc;
}

return frames;
}

Note that this will not necessarily get them in document order and it will get any that are on master or reference pages, as well as body pages.

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Feb 21, 2022 Feb 21, 2022

Copied

LATEST

Dear Rick,

This is the method I use when the user is not 'involved' that is, if she will not be irritated by jumping around in the document. However, the method needed if the user wants to follow the frames visually is by using the Find method. I have used also this method with success, although you some time ago cosidered this as 'not reliable'.

Now I wanted to find an alternative to the Find method (modified to my first idea).

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

//@target framemaker

function main () { // <><><><><><><><><><><><><><><><><><><><><><><><><><><><
var oDoc, oFlow, oTFrm, oAFrm, oaAFrm = [], nFrm = 0, nTF = 0;

oDoc=app.ActiveDoc;
if (!oDoc.ObjectValid()) {
alert ("A document must be active");
return;
}
oFlow= oDoc.MainFlowInDoc;
oTFrm= oFlow.FirstTextFrameInFlow;
while (oTFrm.ObjectValid()) {
$.writeln ("nTextFrame= ", nTF); oAFrm= oTFrm.FirstAFrame;$.bp(true);
while (oAFrm.ObjectValid()) {
oaAFrm.push(oAFrm);
nFrm += 1;
\$.writeln ("\t", nFrm, " ID:", oAFrm.id);
oAFrm = oAFrm.NextAFrame;
}
oTFrm = oTFrm.NextTextFrameInFlow;
nTF += 1;
}
} //--- end main ------------------------------------------------------------

main ();

But even if giving the script time (see the break point), after a certain time it falls out of sync (see comments). I have shortened the output (...):

nTextFrame= 0
nTextFrame= 2    page 2 only frame ...
nTextFrame= 3
nTextFrame= 4
nTextFrame= 5
1 ID:386025124
nTextFrame= 6
2 ID:386023581
3 ID:386023596
nTextFrame= 7
nTextFrame= 8
nTextFrame= 9
nTextFrame= 10
nTextFrame= 11
4 ID:386023685
5 ID:386023702
nTextFrame= 12
...
nTextFrame= 18
6 ID:386024544
nTextFrame= 19
7 ID:386024568
nTextFrame= 20
8 ID:386024678
9 ID:386024692
nTextFrame= 21
nTextFrame= 22        << items missing
nTextFrame= 23
nTextFrame= 24
nTextFrame= 25
nTextFrame= 26        << items missing
nTextFrame= 27        << items missing
nTextFrame= 28        << items missing
nTextFrame= 29        << items missing
nTextFrame= 30
...
nTextFrame= 35        << items missing
...
nTextFrame= 43
nTextFrame= 44        << items missing
nTextFrame= 45
nTextFrame= 46
nTextFrame= 47        << items missing
...
nTextFrame= 53        << item missing
nTextFrame= 54        << items in table missing
nTextFrame= 55Table continued, all items found...
10 ID:386023480
11 ID:386023846
12 ID:386023851
13 ID:386023856
14 ID:386023861
15 ID:386023866
16 ID:386023871
17 ID:386023876
18 ID:386023881
19 ID:386023886
20 ID:386023891
nTextFrame= 56        << item missing
...
nTextFrame= 75
21 ID:386025486
22 ID:386025484

I get these incorrect results with each run of the script, also after restart of FM.

I can not image why the text frames do not contain reliable information (FirstAFrame) - this method is really unsafe.

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Resources
Fixed Issues