Skip to main content
AG_Ps_100
Inspiring
August 14, 2018
Answered

Resize text to the size of its textbox

  • August 14, 2018
  • 2 replies
  • 2152 views

Hello everyone,

I wanted a script that resizes text to the size of its textbox.

example, before:

after (I added a layer with what i'm looking for):

> I want the text to keep its aspect ratio, in the example above the text being larger above the text box which i like

> Sometimes I'll use language that isn't English. (Hebrew)

Thank you.

This topic has been closed for replies.
Correct answer r-bin

Try

var old_units = app.preferences.rulerUnits;

app.preferences.rulerUnits = Units.POINTS;

app.activeDocument.suspendHistory("Resize Text", "fn()")

app.preferences.rulerUnits = old_units;

function fn()

    {

    var w0 = activeDocument.activeLayer.bounds[2].value - activeDocument.activeLayer.bounds[0].value;

    var r = new ActionReference(); 

    r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("textKey"));     

    r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum")); 

    try { 

        var xx = 1; 

        try { xx = executeActionGet(r).getObjectValue(stringIDToTypeID("textKey")).getObjectValue(stringIDToTypeID("transform")).getDouble(stringIDToTypeID("xx")); } catch(e) {} 

        var b = executeActionGet(r).getObjectValue(stringIDToTypeID("textKey")).getList(stringIDToTypeID("textShape")).getObjectValue(0).getObjectValue(stringIDToTypeID("bounds"))

        var w = b.getDouble(stringIDToTypeID("right")) - b.getDouble(stringIDToTypeID("left")); 

        var w1 = w*xx; 

        } 

    catch(e) { alert("not a paragraph text"); return false; } 

    var d = new ActionDescriptor();

    var r = new ActionReference();

    r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("char"));

    r.putEnumerated(stringIDToTypeID("textLayer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));

    d.putReference(stringIDToTypeID("null"), r);

    d.putEnumerated(stringIDToTypeID("to"), stringIDToTypeID("char"), stringIDToTypeID("point"));

    executeAction(stringIDToTypeID("set"), d, DialogModes.NO);

    //activeDocument.activeLayer.textItem.size *= (w1/w0);

    transform(w1/w0*100, activeDocument.activeLayer.bounds[0].value, activeDocument.activeLayer.bounds[3].value);

    return true;

    }

function transform(pcn, x, y)

    {

    try

        {

        var d1 = new ActionDescriptor();

        var r1 = new ActionReference();

        r1.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );

        d1.putReference( charIDToTypeID( "null" ), r1 );

        d1.putEnumerated( charIDToTypeID( "FTcs" ), charIDToTypeID( "QCSt" ), charIDToTypeID( "Qcsi" ) );

        var d2 = new ActionDescriptor();

        d2.putUnitDouble( charIDToTypeID( "Hrzn" ), charIDToTypeID( "#Pnt" ), x );

        d2.putUnitDouble( charIDToTypeID( "Vrtc" ), charIDToTypeID( "#Pnt" ), y );

        d1.putObject( charIDToTypeID( "Pstn" ), charIDToTypeID( "Pnt " ), d2 );

        d1.putUnitDouble( charIDToTypeID( "Wdth" ), charIDToTypeID( "#Prc" ), pcn );

        d1.putUnitDouble( charIDToTypeID( "Hght" ), charIDToTypeID( "#Prc" ), pcn );

        executeAction( charIDToTypeID( "Trnf" ), d1, DialogModes.NO );

        }

    catch (e) { alert(e); }

    }

2 replies

akmia51
Inspiring
June 21, 2022
// Abdul karim mia
// https://abdulkarimmia.com
var currentLayer = activeDocument.activeLayer;
if (currentLayer.visible && currentLayer.kind == LayerKind.TEXT && currentLayer.textItem.contents && currentLayer.textItem.kind == TextType.PARAGRAPHTEXT) {

  app.preferences.rulerUnits = Units.POINTS;
  var doc = activeDocument;
  var alay = doc.activeLayer;
  var cnt = alay.textItem.contents;
  var txs = Number(alay.textItem.size)


  for (s = txs; s > 0; s--) {
    doc.activeLayer.textItem.size = s;
    doc.activeLayer.textItem.kind = TextType.POINTTEXT;

    if (cnt.length == doc.activeLayer.textItem.contents.length) {
      Undo();
      doc.activeLayer.textItem.size = s + 1;
      doc.activeLayer.textItem.kind = TextType.POINTTEXT;
      if (cnt.length == doc.activeLayer.textItem.contents.length) { Undo(); break; } else { Undo(); break; }


    } else { Undo(); }
    s--
  }




  function Undo() {
    // Select
    var desc1 = new ActionDescriptor();
    var ref1 = new ActionReference();
    ref1.putEnumerated(charIDToTypeID('HstS'), charIDToTypeID('Ordn'), charIDToTypeID('Prvs'));
    desc1.putReference(charIDToTypeID('null'), ref1);
    executeAction(charIDToTypeID('slct'), desc1, DialogModes.NO);

  };

}
Abdul karim mia
r-binCorrect answer
Legend
August 14, 2018

Try

var old_units = app.preferences.rulerUnits;

app.preferences.rulerUnits = Units.POINTS;

app.activeDocument.suspendHistory("Resize Text", "fn()")

app.preferences.rulerUnits = old_units;

function fn()

    {

    var w0 = activeDocument.activeLayer.bounds[2].value - activeDocument.activeLayer.bounds[0].value;

    var r = new ActionReference(); 

    r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("textKey"));     

    r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum")); 

    try { 

        var xx = 1; 

        try { xx = executeActionGet(r).getObjectValue(stringIDToTypeID("textKey")).getObjectValue(stringIDToTypeID("transform")).getDouble(stringIDToTypeID("xx")); } catch(e) {} 

        var b = executeActionGet(r).getObjectValue(stringIDToTypeID("textKey")).getList(stringIDToTypeID("textShape")).getObjectValue(0).getObjectValue(stringIDToTypeID("bounds"))

        var w = b.getDouble(stringIDToTypeID("right")) - b.getDouble(stringIDToTypeID("left")); 

        var w1 = w*xx; 

        } 

    catch(e) { alert("not a paragraph text"); return false; } 

    var d = new ActionDescriptor();

    var r = new ActionReference();

    r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("char"));

    r.putEnumerated(stringIDToTypeID("textLayer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));

    d.putReference(stringIDToTypeID("null"), r);

    d.putEnumerated(stringIDToTypeID("to"), stringIDToTypeID("char"), stringIDToTypeID("point"));

    executeAction(stringIDToTypeID("set"), d, DialogModes.NO);

    //activeDocument.activeLayer.textItem.size *= (w1/w0);

    transform(w1/w0*100, activeDocument.activeLayer.bounds[0].value, activeDocument.activeLayer.bounds[3].value);

    return true;

    }

function transform(pcn, x, y)

    {

    try

        {

        var d1 = new ActionDescriptor();

        var r1 = new ActionReference();

        r1.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );

        d1.putReference( charIDToTypeID( "null" ), r1 );

        d1.putEnumerated( charIDToTypeID( "FTcs" ), charIDToTypeID( "QCSt" ), charIDToTypeID( "Qcsi" ) );

        var d2 = new ActionDescriptor();

        d2.putUnitDouble( charIDToTypeID( "Hrzn" ), charIDToTypeID( "#Pnt" ), x );

        d2.putUnitDouble( charIDToTypeID( "Vrtc" ), charIDToTypeID( "#Pnt" ), y );

        d1.putObject( charIDToTypeID( "Pstn" ), charIDToTypeID( "Pnt " ), d2 );

        d1.putUnitDouble( charIDToTypeID( "Wdth" ), charIDToTypeID( "#Prc" ), pcn );

        d1.putUnitDouble( charIDToTypeID( "Hght" ), charIDToTypeID( "#Prc" ), pcn );

        executeAction( charIDToTypeID( "Trnf" ), d1, DialogModes.NO );

        }

    catch (e) { alert(e); }

    }

AG_Ps_100
AG_Ps_100Author
Inspiring
August 20, 2018

r-bin it works

it's just not in the same position it was before, any idea on how to preserve the location?

Legend
August 20, 2018

Have you tried using

activeDocument.activeLayer.textItem.size *= (w1/w0);

instead of

transform(w1/w0*100, activeDocument.activeLayer.bounds[0].value, activeDocument.activeLayer.bounds[3].value);

?

Which position is violated?