Highlighted

Indesign scripting to get all textframes of a page in order

New Here ,
May 05, 2020

Copy link to clipboard

Copied

I am trying to get all the text frames for a single page in order but I was always getting them in random order when using the

myDoc.pages[1] //assuming the 1st index page
>> [object Page]
myDoc.pages[eachPage].textFrames //get all the text frames on that page. >> [object TextFrames] //able to get the textframes but they are not in the order if they are linked textframes

Is there a way to get them in order if they are linked text frames? (not sure how they will come up if they are unlinked)

If not at least how to know the first text frame on the page so I can get the other frames with. `.nextTextFrame`.

 

cross-reference : https://stackoverflow.com/questions/61624405/indesign-scripting-to-get-all-textframes-of-a-page-in-o...

Topics

Scripting

Views

455

Likes

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

Indesign scripting to get all textframes of a page in order

New Here ,
May 05, 2020

Copy link to clipboard

Copied

I am trying to get all the text frames for a single page in order but I was always getting them in random order when using the

myDoc.pages[1] //assuming the 1st index page
>> [object Page]
myDoc.pages[eachPage].textFrames //get all the text frames on that page. >> [object TextFrames] //able to get the textframes but they are not in the order if they are linked textframes

Is there a way to get them in order if they are linked text frames? (not sure how they will come up if they are unlinked)

If not at least how to know the first text frame on the page so I can get the other frames with. `.nextTextFrame`.

 

cross-reference : https://stackoverflow.com/questions/61624405/indesign-scripting-to-get-all-textframes-of-a-page-in-o...

Topics

Scripting

Views

456

Likes

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
May 05, 2020 0
Contributor ,
May 05, 2020

Copy link to clipboard

Copied

Not sure what you mean by "first." If you mean the one in the top left corner, you'd have to plow through the geometric bounds of each text frame and find the one with the lowest x1 and y1 values. Could there be a circumstance where the lowest y1 is not the same box as thhe lowest x1?

If you mean, the first box placed on the page, look at textFrame.id. The id number is assigned to each box as it's created and it doesn't change as other boxes are added or removed. The lowest id number will be the first one created.

Besides nextTextFrame, you can also look at previousTextFrame to see if there is another box threaded on the upstream side.

Hope this helps,

Bob

Likes

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
Reply
Loading...
May 05, 2020 0
New Here ,
May 05, 2020

Copy link to clipboard

Copied

The "first" what I meant is the text frame that's the starting of the chain of the text frames when the text frames are linked. thanks for highlighting the `text frame id` i will try it out, I would even like to know if there is any other alternative for the same.

Likes

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
Reply
Loading...
May 05, 2020 0
Adobe Community Professional ,
May 05, 2020

Copy link to clipboard

Copied

You could look for a frame that has a Next but no Previous, and create a new array to return. Something like this (not sure my logic is 100% accurate, but you should get the idea). Of course, this wouldn't work if there were two sets of threaded frames on a page. You'd also have to add some logic if you don't want threaded frames that jump to a different page. 

 

var getThreadedFramesInOrder = function(thepage) {
    var threadedFrames = [];
    var allTfs = thepage.textFrames;
    var theTargetTF = undefined;
    for (var i = 0; i allTfs.length;i++) {
         if (allTfs[i].nextTextFrame.isValid && !allTfs[i].previousTextFrame.isValid) {
            theTargetTF = allTfs[i];
            threadedFrames.push(theTargetTF);
            while (theTargetTF.nextTextFrame.isValid) {
                threadedFrames.push(theTargetTF.nextTextFrame);
                theTargetTF = theTargetTF.nextTextFrame;
            }
            break;    
         }
    }
    return threadedFrames;
}

 

 

 

 

  

Likes

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
Reply
Loading...
May 05, 2020 0
Adobe Community Professional ,
May 06, 2020

Copy link to clipboard

Copied

Hi Sundeep,

are there also text frames that are not part of the page?

 

FWIW: You could work with the textContainers array of a story.

As a start you need one single text frame of the story. It's equal which one, if it is part of the story you want to follow.

 

Let's say you selected a text frame on page 2 like in the example below:

 

SomeThreadedTextFramesSelected-1.PNG

 

Then you run the script below that would write just three of the six visible text frames to an array. In the order they appear in the story and with an indication what this order is, from 0 to 2:

 

 

 

var page = app.selection[0].parentPage;
var story = app.selection[0].parentStory;

// Prepare a result array:
var resultArray = [];

// Contains all text frames and text paths of the story:
var textContainersArray =story.textContainers;

// Loop that array:

for( var n=0; n<textContainersArray.length; n++ )
{
	var currentContainer = textContainersArray[n] ;
	
	// If you encounter a text path, inspect the parent of the text path:
	if( currentContainer.constructor.name == "TextPath" )
	{ currentContainer = currentContainer.parent };
	
	// If parent page is null the container is not on the page, it's on the pasteboard.
	// Per definition we are looking on objects only on the page, so ignore it:
	if( currentContainer.parentPage == null ){ continue };
	
	// Also test for the page itself. Could be that a text frame is on a different page in the document:
	if( currentContainer.parentPage != page ){ continue };
	
	// All tests passed? Store the text container or the element that holds the text path in the result array:
	resultArray[ resultArray.length++ ] = currentContainer ;
	
};

// Do something with the result:
for( var n=0; n<resultArray.length; n++ )
{
	resultArray[n].properties =
	{
		fillColor : "Yellow" ,
		strokeColor : "Magenta" ,
		strokeWeight : "5 Pt" 
	};
	
	var newTextFrame = page.textFrames.add();
	
	newTextFrame.properties =
	{
		contents : n.toString() ,
		
		geometricBounds : resultArray[n].geometricBounds ,
		
		fillColor : "None" ,
		strokeWeight : 0 ,
		strokeColor : "None" ,
		
		parentStory :
		{
			pointSize : 40 ,
			justification : Justification.CENTER_ALIGN
		}
	};

	newTextFrame.textFramePreferences.properties =
	{
		autoSizingReferencePoint : AutoSizingReferenceEnum.CENTER_POINT ,
		autoSizingType : AutoSizingTypeEnum.HEIGHT_AND_WIDTH ,
		firstBaselineOffset : FirstBaseline.CAP_HEIGHT ,
		insetSpacing : 0 ,
		verticalJustification : VerticalJustification.CENTER_ALIGN
	};
	
};

 

 

 

The result would be this:

 

SomeThreadedTextFramesSelected-2.PNG

 

Why do only three frames qualify?

The second frame of the story is on the pasteboard, the fourth and the fifth are also not in the array, they are on the wrong page. only the first one in the story, the second third one and the last one in the story, the selected one, qualify.

 

Regards,
Uwe Laubender

( ACP )

 

EDITED

Likes

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
Reply
Loading...
May 06, 2020 0