Pages And Margins

fabiantheblind edited this page May 4, 2016 · 3 revisions


This script fits a page to a text frame

    var doc = app.documents.add();
    var page = doc.pages.item(0);
    var tf = page.textFrames.add({
        geometricBounds:[12.7,12.7,100,210],
        contents: "Hi God, it's me - Jesus."
        });
       
        tf.paragraphs.item(0).pointSize = 42;
        tf.fit(FitOptions.FRAME_TO_CONTENT);
        
       
    
        doc.documentPreferences.pageWidth = tf.geometricBounds[3] + 12.7;
        doc.documentPreferences.pageHeight = tf.geometricBounds[2] + 12.7;
        
        var new_pw = doc.documentPreferences.pageWidth;
        var new_ph = doc.documentPreferences.pageHeight;
        
        tf.geometricBounds = [12.7,12.7,new_ph - 12.7,new_pw - 12.7];   

The next script shows how many fun you can have with pageItems

    var doc;
    if(app.documents.length > 0){
        doc = app.activeDocument;
        }else{
        doc = app.documents.add();     
    }
    
    /**
     *  A document has always a page 
     *  so we don't need to create one  
     */
    var page = doc.pages.item(0);
    // could also be 
    // var page = doc.pages.lastItem();
    // now lets do some stuff on that page
    
    var gb = [10,10,50,50]; // some bounds
        page.ovals.add({geometricBounds:gb}); // add an oval
        // increase x values
        gb[1]+=10;
        gb[3]+=10;
        page.rectangles.add({geometricBounds:gb}); // add an rectangle
        // increase x values again
        gb[1]+=10;
        gb[3]+=10;
        page.textFrames.add({geometricBounds:gb}); // add an textFrame
    
        // a page has not only ovals and so on
        // it also has pageItems
        // loop them
    for(var i = 0; i < page.pageItems.length; i++){
            // now get the y value
        var y = page.pageItems[i].geometricBounds[1];
            // and move them
            page.pageItems[i].move([ 10 + (10*i),y]);    
        }
    
        // now duplicate the page
        page.duplicate();

###margins
Margins are the grid for your layout.

    var doc = app.documents.add({
    
        });
    var page = doc.pages.item(0);
    page.marginPreferences.properties = {
        top : 30,
        left: 30,
        right: 30,
        bottom:30
        };

Have a look at the tons of different options and properties you can set. This script also creates a new layer and connects several textFrames.

    /**
     *  Lets build a doc
     */
    main();
    function main(){
        
    // add a doc
    var doc = app.documents.add(); 
    // lets store some data in an object for better handling
    meta = {
            "pw":200,
            "ph":200,
            "top":20,
            "left":20,
            "bottom":20,
            "right":20,
            "gutter":5,
            "textColumnCount":5
            };
    
    // have a look at the build function below
    doc_build(doc, true,false,meta);
    
    // now lets make something with those pages
    var count = 0; // counter
    var range = 10; // until
    var frames = []; // to store the textFrames
    // now loop it
    while(count < range){
        // the first page is always there
        // so we have to check if we are at index 0
        var page;
        if(count != 0 ){
        page = doc.pages.add(); // if not add a page
        }else{
        page = doc.pages[0]; // we are at index 0 take the existing page
        }
        // apply a master spread
        page.appliedMaster = doc.masterSpreads.item(0);
        // add a textFrame and push it into an array
        // for better handling
        frames.push(page.textFrames.add());
        // now get the margins from the page
        // the get bounds function is part of the
        // Scripting Tutorial Hello World from the SDK
        frames[count].geometricBounds = myGetBounds(doc, page);
        // once again we have to check if we are at index 0
        // because the textFrame 0 cant have a previous text frame
        // if we are not at 0 connect the actual textFrame with the previous  
        if(count != 0){
            frames[count].previousTextFrame = frames[count-1];
            }
        // now we change the gutter on every second text frame
        // just for the fun of it.
        // get the gutter from the page 
        // and give the textFrame the same margins
        if((count%2) == 0){
            frames[count].textFramePreferences.properties = {
            textColumnCount : page.marginPreferences.columnCount/2,
            textColumnGutter : page.marginPreferences.columnGutter
            };
         }
         //increment the count for the next loop
        count++;
        }
    // add some placeholder text
    frames[0].contents = TextFrameContents.placeholderText;
    // The End
    return 0;
    };
    
    
    /**
     *  Lets build a document
     *  with lots of preferences
     */
    function doc_build(doc, makeColumns,facingPages,meta){
        doc.layers.item(0).name = "the standard layer";    
        doc.layers.add({name:"a new layer"});
            if(facingPages){
            doc.documentPreferences.facingPages = true;
        }else{
            doc.documentPreferences.facingPages = false;
        }
    
        // set some view parameters
        doc.viewPreferences.properties = {
            horizontalMeasurementUnits: MeasurementUnits.MILLIMETERS,
            verticalMeasurementUnits:MeasurementUnits.MILLIMETERS
        };
        // set the page size and bleed box
        doc.documentPreferences.properties = {
            pageWidth : meta.pw,
            pageHeight : meta.ph,
            documentBleedBottomOffset : 3,
            documentBleedTopOffset : 3,
            documentBleedInsideOrLeftOffset : 3,
            documentBleedOutsideOrRightOffset : 3
        };
        
        // the baseline settings
        doc.gridPreferences.properties = {
         baselineStart : meta.top
        }
        
        // how big are the steps
        doc.gridPreferences.baselineDivision = "15pt";
    
    // lets create some master spreads
    var master_spread1 = doc.masterSpreads.item(0).pages.item(0);// edit the master spreads
     
        master_spread1.marginPreferences.properties = {
            right:meta.right,
            top:meta.top ,
            left:meta.left ,
            bottom:meta.bottom + 10,
            columnGutter:meta.gutter*2,
        };  
    
    var master_spread2 = doc.masterSpreads.item(0).pages.item(1);//edit the other master spread
    
        master_spread2.marginPreferences.properties = {
            right:meta.right,
            top:meta.top,
            left:meta.left,
            bottom:meta.bottom,
            columnGutter:meta.gutter
        };  
    
        if(makeColumns){
            master_spread1.marginPreferences.columnCount = meta.textColumnCount*2;
            master_spread2.marginPreferences.columnCount = meta.textColumnCount;
        }
    
    
    }// end make doc
    
    // part of
    //ImprovedHelloWorld.jsx from InDesign scripting SDK
    //An InDesign CS6 JavaScript
    
    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];
    }
Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.