在 indesign 脚本中获取当前文本框宽度

get current text frame width in indesign script

下面的代码获取我的文档中的第一个文本框宽度。如何在 Indesign 脚本中获取活动文本框宽度?图片显示了我的问题。

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;
}

这更多是关于如何检索所选内容的父文本框的问题。您通常可以通过文本选择的 parentTextFrames 属性 来完成此操作。您还可以选择包括检查是否选择了文本框本身而不是文本框内的某些文本。因此,以下代码应该适用于您的场景:

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);