• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Insert anchored frame below every "Figure" element

New Here ,
Apr 13, 2023 Apr 13, 2023

Copy link to clipboard

Copied

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

TOPICS
Scripting

Views

291

Translate

Translate

Report

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

Copy link to clipboard

Copied

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;
}

 

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

Thank you very much 🙂

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

Thank you very much.
I need also this part of script responsible for insert anchored frame, element "Picture" between Figure and Caption.

So after go through each paragraph script should take first "Figure" or "Caption" element ...

Kasandra28240320c4xs_0-1681734915038.png

 

.... and I'd like to insert Anchored Frame before Caption, like this:

Kasandra28240320c4xs_1-1681734915007.png

Could anybody help me with this part?

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

LATEST

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;
	}
}

Votes

Translate

Translate

Report

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