Skip to main content
New Participant
April 13, 2023
Question

Insert anchored frame below every "Figure" element

  • April 13, 2023
  • 2 replies
  • 529 views

Good Morning,

I am trying to write a script that inserts an anchored frame in front of each element specified as "Caption". Below are my attempts. Could you help me? or advise something? Thank you in advance.

var doc = app.ActiveDoc;
var mainFlow = doc.MainFlowInDoc;
FindElements(mainFlow);


//~ function FindElements(flow) {
//~ var elements = flow.FirstTextFrameInFlow.everyItem().getElements();
//~ for (var i = 0; i < elements.length; i++) {
//~ var currentElement = elements[i];


function FindElements(flow) {

var currentElement = flow.HighestLevelElement;
var nextElement = currentElement.HighestLevelElement;
while (currentElement.ObjectValid()) {

FindFigures(currentElement);

currentElement = nextElement;
nextElement = currentElement.HighestLevelElement;
if (nextElement === currentElement) {
break;
}
}
}

function FindFigures(element){

var currentFigure = element.ElementDef;
var nextFigure = currentFigure.NextElementDefInDoc;
while (currentFigure.ObjectValid() && currentFigure.Name = "Figure");

NewAnchoredAFrame(currentFigure);

currentFigure = nextFigure;
nextFigure = currentFigure.NextElementDefInDoc;
if (nextFigure === currentFigure) {
//~ break;
}
}


function NewAnchoredAFrame(figure) {

var newFrame = figure.textContainers.add({
anchoredObjectSettings: {
anchoredPosition: AnchorPosition.ABOVE_LINE,
horizontalAlignment: HorizontalAlignment.CENTER_ALIGN,
anchoredPositionV: VerticalAlignment.TOP_ALIGN
}})
}

This topic has been closed for replies.

2 replies

Participating Frequently
July 21, 2023

Hi, Kasandra
The steps to insert an anchored frame in a structured document are:

1. Create an insertion point for the Picture element.
2. Insert a Picture element.
3. An anchored frame is created automatically.

Try the script below with a document open.

var doc = app.ActiveDoc;
var pictureEdef = doc.GetNamedElementDef ("Picture");
var elements = doc.MainFlowInDoc.GetText (Constants.FTI_ElementBegin);
var parent, child, elemLoc, pictureEle, aFrame, insetObj, insetFilePath;

for (var i=0;i<elements.length;i++){
	if (elements[i].obj.ElementDef.Name == "Figure"){
		// Create an insertion point for the Picture element
		parent = elements[i].obj;	//Figure element
		child = parent.FirstChildElement;	//Caption element
		elemLoc = new ElementLoc (parent, child, 0);
		// Insert a Picture element
		// An anchored frame should be created automatically at this point
		pictureEle = pictureEdef.NewElementInHierarchy (elemLoc);
		
		// Anchored frame settings
		aFrame = pictureEle.Object;
		aFrame.AnchorType = Constants.FV_ANCHOR_BELOW;
		aFrame.Alignment = Constants.FV_ALIGN_CENTER;
		aFrame.AFrameIsFloating = false;
		
		// The code below places an image in an anchored frame
		var insetObj = doc.NewGraphicObject (Constants.FO_Inset, aFrame);
		// Please rewrite the file path as necessary
		var insetFilePath = new File (doc.Name).parent.fsName+"\\test.ai";
		insetObj.InsetFile = insetFilePath;
		insetObj.Pen = Constants.FV_FILL_CLEAR;
		
		// Set the size of the anchor frame (unit: pt)
		aFrame.Width = insetObj.Width + 12 * 65536;
		aFrame.Height = insetObj.Height + 12 * 65536;
		
		insetObj.LocX = 6 * 65536;
		insetObj.LocY = 6 * 65536;
	}
}
Inspiring
April 13, 2023

Hello,

I guess you work in structured.
According to your code, you are trying to insert an Anchored Frame in a 'Figure' element but without positioning the anchor.

I think you should go through each paragraph (if is like my book I have much more element than paragrph), if in the paragraph, your element named is "Caption" then position it at the beginning of it. And at that position, you can insert an Anchored Frame.

 

Here a beginning of a code for loop the paragraph/element but I never inserted AnchoredFrame so I can't help you with that part.

// ------------ init ------------ 
var vDoc = app.ActiveDoc;
var vPgf = vDoc.MainFlowInDoc.FirstTextFrameInFlow.FirstPgf;
var textRange = new TextRange();
var eRange, vElement, textItems;
// ---- Loop throw all paragraph
while(vPgf.ObjectValid() == true){
  textRange.beg.obj = vPgf;
  textRange.beg.offset = 0;
  textRange.end.obj = vPgf;
  textRange.end.offset = Constants.FTI_PgfEnd;
  textItems = vDoc.GetTextForRange(textRange, Constants.FTI_String);
  vDoc.TextSelection = textRange;
// ------------ Positionning on paragraph not the element ------------
  eRange = vDoc.ElementSelection;
  vElement = eRange.beg.child;
// ------------ Check if element that your "wrap" have is name == "Caption" ------------
  if (vElement.ElementDef.Name == "PARA") {
    // -------- Positionning at the beginning of the element(if you chose "0" you will be just before your element --------
    textRange.beg.obj = vPgf;
    textRange.beg.offset = 1;
    textRange.end.obj = vPgf;
    textRange.end.offset = 1;
    vDoc.TextSelection = textRange;

// ------- ADD Anchored Frame ------
    
  }
  vPgf = vPgf.NextPgfInFlow;
}

 

Inspiring
April 13, 2023

Opps, change "PARA" by Caption, I used PARA to test on my files

New Participant
April 17, 2023

Thank you very much 🙂