Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Resize text to the size of its textbox

Engaged ,
Aug 14, 2018 Aug 14, 2018

Hello everyone,

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

example, before:

1.JPG

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

2.JPG

> 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.

TOPICS
Actions and scripting
2.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

People's Champ , Aug 14, 2018 Aug 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"), str

...
Translate
Adobe
People's Champ ,
Aug 14, 2018 Aug 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); }

    }

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Aug 20, 2018 Aug 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Aug 20, 2018 Aug 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Aug 20, 2018 Aug 20, 2018

Use

var pos = activeDocument.activeLayer.textItem.position;

 

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

activeDocument.activeLayer.textItem.position = pos

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Aug 20, 2018 Aug 20, 2018

i get this message:

3.JPG

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Aug 20, 2018 Aug 20, 2018

After running the script, the text is no longer a paragraph.
Rollback on the history before the script runs.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Aug 20, 2018 Aug 20, 2018

alright thank you!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 21, 2022 Jun 21, 2022
LATEST
// 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines