5 Replies Latest reply on Dec 15, 2014 10:34 AM by xbytor2

    Calculate text width


      I need to get a width of text (which includes few letters or even one letter). Is it possible to make this via scripting? Let's say, I have created a layer with text and have a reference to textItem.


        • 1. Re: Calculate text width
          xbytor2 Level 4

          Doesn't TextItem.width.as("px") give you what you need?

          • 2. Re: Calculate text width
            DK22 Level 1

            Sadly no.

            It gives me size of paragraph box:


            which is always bigger than text/letter width.

            • 3. Re: Re: Calculate text width
              xbytor2 Level 4

              If you only have text layer, do the following:


              1) Magic Wand the background. I usually call it at 0,0


              cTID = function(s) { return app.charIDToTypeID(s); };
              sTID = function(s) { return app.stringIDToTypeID(s); };
              function magicWand(doc, x, y, tol, aa, cntg) {
                  var desc = new ActionDescriptor();
                  // Selection
                  var ref = new ActionReference();
                  ref.putProperty(cTID("Chnl"), cTID("fsel"));
                  desc.putReference(cTID("null"), ref);
                  // Point
                  var pdesc = new ActionDescriptor();
                  pdesc.putUnitDouble(cTID("Hrzn"), cTID("#Pxl"), x);
                  pdesc.putUnitDouble(cTID("Vrtc"), cTID("#Pxl"), y);
                  desc.putObject(cTID("T  "), cTID("Pnt "), pdesc);
                  // Tolerance
                  if (tol != undefined) {
                    desc.putInteger(cTID("Tlrn"), tol);
                  // Anti-alias
                  desc.putBoolean(cTID("AntA"), !!aa);
                  // Contiguous
                  desc.putBoolean(cTID("Cntg"), !!cntg);
                  executeAction(cTID("setd"), desc, DialogModes.NO);


              2) Invert the selection: doc.selection.invert()

              3) Get the bounds of the selection (doc.selection.bounds) and compute the width of the text in in pixels.

              4) To get the intra-character spacing, look at textItem.tracking and convert it to pixels.

                  Also, it might make the math a bit easier if you temporarily convert to 72ppi when working with the tracking value.

              5) The tracking value in px plus the character width in px should give you what you want.

              6) Delete the selection


              If you do have other layers, you will need to hide them for the magic wand to work correctly and restore them and restore them afterwards.

              The simplest what to do that is to iterate through doc.layers and hide each layer except the text layer. At the end of the script, do however

              many undos are required to get back to your initial state.

              • 4. Re: Calculate text width
                DK22 Level 1

                Thanks bytor2, but I'm not sure if it will give the correct result. I think it will give me this as a result:


                Anyway, I found some way to get a correct width:


                // text must be converted to PARAGRAPHTEXT

                textItemRef.kind = TextType.PARAGRAPHTEXT

                // save default justify if needed

                textItemRef.justification = Justification.RIGHTJUSTIFIED

                x1 = artLayerRef.bounds[0].as("px")

                textItemRef.justification = Justification.LEFTJUSTIFIED

                x2 = artLayerRef.bounds[0].as("px")

                width = textItemRef.width.as("px") - (x1 - x2)

                // restore default justify if needed

                • 5. Re: Calculate text width
                  xbytor2 Level 4

                  Looks like a reasonable solution. And much simpler than mine