Copy link to clipboard
Copied
I have two text frames on a page. When the top one is overset, I want it to resize the height so all the content is visible meanwhile reducing the height of the second text frame on the same page. If the top one is not overset and the textframe height is larger than the content, I want to resize it so the second text frame increases in size etc.
How do I accomplish this using a script? (cannot use plugins, must be a script)
Copy link to clipboard
Copied
You can do it like this with javascript:
function resizeTwoTextFrames(myPage, spaceBetweenTextFrames){ // resizes the top two textframes of a page
if (myPage.textFrames.length>2){
alert("Page must contain at least two textframes");
return;
}
var myTextFrames = myPage.textFrames.everyItem().getElements().sort(function(a,b){return a.geometricBounds[0]>b.geometricBounds[1]}); // sorts every textframe on the page by the top margin
var topTextFrame = myTextFrames[0];
var lowerTextFrame = myTextFrames[1];
topTextFrame.fit(FitOptions.FRAME_TO_CONTENT);
lowerTextFrame.geometricBounds = [topTextFrame.geometricBounds[2]+spaceBetweenTextFrames,lowerTextFrame.geometricBounds[1],lowerTextFrame.geometricBounds[2],lowerTextFrame.geometricBounds[3]]
}
resizeTwoTextFrames(app.activeWindow.activePage, 0);
You might want to change the method of selecting the two textframes in question. This will take the top two textframes on the page.
Copy link to clipboard
Copied
Typo?
sort(function(a,b){return a.geometricBounds[0]>b.geometricBounds[1]})
Copy link to clipboard
Copied
John Hawkinson wrote:
Typo?
sort(function(a,b){return a.geometricBounds[0]>b.geometricBounds[1]})
![]()
Yes, it's a typo. This is what it's supposed to be:
sort(function(a,b){return a.geometricBounds[0]>b.geometricBounds[0]})
Here's the whole script with the typo corrected
function resizeTwoTextFrames(myPage, spaceBetweenTextFrames){ // resizes the top two textframes of a page
if (myPage.textFrames.length>2){
alert("Page must contain at least two textframes");
return;
}
var myTextFrames = myPage.textFrames.everyItem().getElements().sort(function(a,b){return a.geometricBounds[0]>b.geometricBounds[0]}); // sorts every textframe on the page by the top margin
var topTextFrame = myTextFrames[0];
var lowerTextFrame = myTextFrames[1];
topTextFrame.fit(FitOptions.FRAME_TO_CONTENT);
lowerTextFrame.geometricBounds = [topTextFrame.geometricBounds[2]+spaceBetweenTextFrames,lowerTextFrame.geometricBounds[1],lowerTextFrame.geometricBounds[2],lowerTextFrame.geometricBounds[3]]
}
resizeTwoTextFrames(app.activeWindow.activePage, 0);
Copy link to clipboard
Copied
Another typo?
length>2
Should be
length<2
in the if-statement…
Uwe
Copy link to clipboard
Copied
Very good! That was not a typo, it was a test to see if you were paying attention. ![]()
Copy link to clipboard
Copied
Thanks for the reply. However this is what I want:
When there's two text frames on a page one after the other, I want to delete the first textframe and resize the other textframe so it takes up the whole page. How do I do this with a script? I tried modifying the geometric bounds (secondTextFrame.GeometricBounds[0] = 0 but it doesn't move up)
Copy link to clipboard
Copied
secondTextFrame.GeometricBounds[0] = 0
This construction fetches secondTextFrame.geometricBounds (an array), keeps it hanging out there, and changes the value of the first element, and then throws the array away. You must use a temporary variable:
var b = secondTextFrame.geometricBounds;
b[0] = 0;
secondTextFrame.geometricBounds = b;
(well, ok, I guess you could do it on one line without the temp. var)
That said, it seems like you should use the bounds of the current page, or something, just in case somebody moves the zero point.
Copy link to clipboard
Copied
Thanks, how do I set it to the geometric bounds of the page margin? I tried textFrame.GeometricBounds = page.Bounds but that set it above the visible area etc.
Copy link to clipboard
Copied
"visible area"? What does that mean?
What do you want?
Are you saying you would like to stay within the page margins?
Please be specific in your questions.
If you want the margin, you'll have to adjust the bounds array
yourself based on the values of the margins which you can look
up in the Page's marginPreferences.
Copy link to clipboard
Copied
TextFrame secondTextFrame;
var a = secondTextFrame.GeometricBounds;
Page someRandomPage= doc.Pages[4];
var b = someRandomPage.GeometricBounds;
a = b;
secondTextFrame.GeometricBounds = a;
The above code fixes the problem BUT depending on the document, there may not be a page 4 (there maybe only 2 pages etc). Instead of getting the geometric bounds from a random page, how do I get it from the page margins?
Copy link to clipboard
Copied
Why would you use page 4? I just don't understand what's going on here.
If you want to pick a random page, you could pick page zero.
But a better choice is probably to use the page the textFrame is actually on.
You know, b = secondTextFrame.parentPage.bounds;
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more