Skip to content
fabiantheblind edited this page May 4, 2016 · 3 revisions

###textFrames

textFrames are the thing you will make extensive use of. So inspect them and their properties carefully. The script below loads the content of the selected file into a text frame. It also builds a document with a different size than the preset.

    var txtFile = File.openDialog("Choose your file","*.*",false);
    var content;
    if(txtFile != null){
        txtFile.open('r');
        content = txtFile.read();

    var doc = app.documents.add();
    var docPref = doc.documentPreferences;
        docPref.pageWidth = 150;
        docPref.pageHeight = 150;
    var pw = docPref.pageWidth;
    var ph = docPref.pageHeight;
        /*
        You could create a document like this
        app.documents.add({
        documentPreferences:{
            pageWidth:150,pageHeight:150
            }
        });
        */    
    var page = doc.pages.item(0);

    var tf = page.textFrames.add({
            geometricBounds:[10,10,ph - 10, pw - 10],
            contents : content
            });
     }

###paragraphs, lines, words and characters

    /**
     *  Lets work with text
     *  This is what we came here for? didn't we?
     *  A text frame holds
     *  paragraphs
     *  lines
     *  words
     *  characters
     *
     *  also
     *  
     *  a line has characters
     *  a word has characters
     *  a line has words
     *  
     *  you see yo can access them in many different ways
     *  like this:
     */
    
    // create doc, get page, add text frame
    var doc = app.documents.add();
    var page = doc.pages[0];
    var docpref = doc.documentPreferences;
    var pw = docpref.pageWidth;
    var ph = docpref.pageHeight;
    var tf = page.textFrames.add({geometricBounds:[12.7,12.7,ph-12.7,pw-12.7]});
    
    // add some placeholder text
    tf.contents = TextFrameContents.PLACEHOLDER_TEXT;
    
    // loop thru the lines
    for(var i = 0; i< tf.lines.length; i++){
            tf.lines[i].fillTint = (100 - ((100/tf.lines.length ) * i));
            // if i is 0 change the characters in it
           if(i == 0){
                for(var j = 0; j < tf.lines[i].characters.length;j++){
                     tf.lines[i].characters[j].pointSize = 5 + (0.5*j);
                    }
               }
        }

###Story
A story holds text from connected textFrames. That means that a story can have several text frames it is in. You can also manipulate the:

  • paragraphs
  • lines
  • words
  • characters

Like in a textFrame. But that applies to all the content of all textFrames in the story.

    /**
     *  The story is a collection of all text
     */
    
    main(); // everything in here
    function main (){
    
    // make a document
    var pw = 150;
    var ph = 100;
    var doc = app.documents.add({
                documentPreferences:{
                    pageHeight:ph,
                    pageWidth:pw
                }
        });
    
    var page = doc.pages[0]; // get the page
    
    // make some text frames
    tf1 = page.textFrames.add({geometricBounds:[10,10,ph-10,(pw/2)- 10]});
    tf1.label = "Jimbo";// give them a label
    // you can label nearly everything
    
    tf2 = page.textFrames.add({geometricBounds:[10,(pw/2)+ 10,ph-10,pw-10]});
    tf2.label = "Milhouse";// another label
    
    tf2.previousTextFrame = tf1; // connect the textFrames
    // fill them with text
    tf1.contents = TextFrameContents.placeholderText;
    // now inspect them
    var first_story = doc.stories[0];
        first_story.label = "Ay caramba!";
        
        // build the output text
    var list = [];
           // first textFrame
           list.push("These are the contents:\n")
           list.push("Content of ");
           list.push(tf1.constructor.name);
           list.push(" called '");
           list.push(tf1.label);
           list.push("':\n")
           list.push(tf1.contents);
           list.push("\n\n");
           
           // second textFrame
           list.push("Content of ");
           list.push(tf2.constructor.name);
           list.push(" called '");
           list.push(tf2.label);
           list.push("':\n");
           list.push(tf2.contents);
           list.push("\n\n");
           
           // the story
           list.push("Content of 'doc.stories[0]' called '");
           list.push(first_story.label);
           list.push("':\n" );
           list.push(first_story.contents);
    
    // alert it
    alert(list.join(""));
    
    return 0;
    }

###Overflow
How to detect overflow on text frames?

    /*
    this script centers a character on a page
    and sizes him up until he fills the text frame
    written by @fabiantheblind
    */
    
    // create a doc with a size of 200 w and h
    var doc = app.documents.add({
            documentPreferences:{
                    pageWidth : 200,
                    pageHeight: 200
                }
            });
    /*    
    create a text frame with a content and the 
    text frame option set to 
    Center
    */
    
    var tf = doc.pages.item(0).textFrames.add({
            geometricBounds:[25,25,175,175],
            contents: "X",
            textFramePreferences:{
                verticalJustification: VerticalJustification.CENTER_ALIGN
                }
            });
        
        
    var firstPar = tf.paragraphs.item(0); // get the first paragraph
    // set some properties
    firstPar.properties = {
                    pointSize: 10,
                    justification : Justification.CENTER_ALIGN,
                    hyphenation : false
                    };
    
    /*
    
    now lets get to the gist
    how to size up a character?
    a text frame has a property overflows
    so we scale up with a while loop until it overflows
    than we scale down until the overflow is gone
    */
    
    var ptsz = firstPar.pointSize; // get the actual point size
    
    // a while loop 
    // if the tf is not overflowing scale up
    while(tf.overflows == false){
            
        firstPar.pointSize = ptsz;
            ptsz++;
            }
    
    // now he has overflow so scale down again until the overflow is gone
    while(tf.overflows == true){
             ptsz--;
            firstPar.pointSize = ptsz;
            }
    
        tf.fit(FitOptions.FRAME_TO_CONTENT); // now fit the box onto the character
    
    // we are done.
    // x marks the spot
Clone this wiki locally