Search code examples
javascriptadobe-indesign

get current text frame width in indesign script


the below code gets the first text frame width in my doc. How can I get the active text frame Width in Indesign script? The image shows my problem.

function freamWidth() {

    var frameRef = app.documents[0].textFrames[0];
    var gBounds = frameRef.geometricBounds;
    var y0 = gBounds[0];
    var x0 = gBounds[1];
    var y1 = gBounds[2];
    var x1 = gBounds[3];
    //do calculations
    var frameWid = x1 - x0;
 //   var frameHgt = y1 - y0;
    return frameWid;
}

enter image description here


Solution

  • This is more a question of how to retreive the parent text frame of a selection. You can usually do this via the parentTextFrames property of a text selection. You could optionally also include a check if the text frame itself is selected instead of some text within the text frame. So the following code should work for your scenario:

    var tf;
    var sel = app.selection[0];
    
    if(sel.hasOwnProperty('baseline')) {
      tf = sel.parentTextFrames[0];
    } else if (sel instanceof TextFrame) {
      tf = sel;
    }
    
    var tfWidth = tf.geometricBounds[3] - tf.geometricBounds[1];
    
    alert("The text frame width is " +  tfWidth);