Skip to main content
New Participant
September 28, 2010
Answered

Align TextFrame to page margins with JS

  • September 28, 2010
  • 2 replies
  • 2746 views

If i have the above page and a textframe in the center what is the javascript code to align / fit the textframe to the page margins ?

This topic has been closed for replies.
Correct answer tomaxxi

Hey!

You can do it like this:

var myPage = app.activeWindow.activePage;
var myMargins = myPage.marginPreferences;
if(app.selection.length){
    app.selection[0].geometricBounds = [myPage.bounds[0]+myMargins.top,myPage.bounds[1]+myMargins.left,myPage.bounds[2]-myMargins.bottom,myPage.bounds[3]-myMargins.right];
}else{
    myPage.textFrames[0].geometricBounds = [myPage.bounds[0]+myMargins.top,myPage.bounds[1]+myMargins.left,myPage.bounds[2]-myMargins.bottom,myPage.bounds[3]-myMargins.right];
}

HTH

--

tomaxxi

http://indisnip.wordpress.com/

2 replies

tomaxxi
tomaxxiCorrect answer
Inspiring
September 28, 2010

Hey!

You can do it like this:

var myPage = app.activeWindow.activePage;
var myMargins = myPage.marginPreferences;
if(app.selection.length){
    app.selection[0].geometricBounds = [myPage.bounds[0]+myMargins.top,myPage.bounds[1]+myMargins.left,myPage.bounds[2]-myMargins.bottom,myPage.bounds[3]-myMargins.right];
}else{
    myPage.textFrames[0].geometricBounds = [myPage.bounds[0]+myMargins.top,myPage.bounds[1]+myMargins.left,myPage.bounds[2]-myMargins.bottom,myPage.bounds[3]-myMargins.right];
}

HTH

--

tomaxxi

http://indisnip.wordpress.com/

dublove
Brainiac
November 19, 2024

@tomaxxi 

How to make an image fit into margin and column width, scaled equally by width

Kasyan Servetsky
Brainiac
September 28, 2010

var doc = app.activeDocument;
var page = doc.pages[0];
var textFrame = page.textFrames[0];
textFrame.geometricBounds = myGetBounds(doc, page);

function myGetBounds(myDocument, myPage){
     var myPageWidth = myDocument.documentPreferences.pageWidth;
     var myPageHeight = myDocument.documentPreferences.pageHeight
     if(myPage.side == PageSideOptions.leftHand){
          var myX2 = myPage.marginPreferences.left;
          var myX1 = myPage.marginPreferences.right;
     }
     else{
          var myX1 = myPage.marginPreferences.left;
          var myX2 = myPage.marginPreferences.right;
     }
     var myY1 = myPage.marginPreferences.top;
     var myX2 = myPageWidth - myX2;
     var myY2 = myPageHeight - myPage.marginPreferences.bottom;
     return [myY1, myX1, myY2, myX2];
}

Known Participant
August 3, 2017

Hi I know this is from 2010! But I need to apply your code to every pages.

How to do that?

Thank you!