Copy link to clipboard
Copied
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.
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
Copy link to clipboard
Copied
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); }
}
Copy link to clipboard
Copied
r-bin it works
it's just not in the same position it was before, any idea on how to preserve the location?
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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 |
Copy link to clipboard
Copied
i get this message:
Copy link to clipboard
Copied
After running the script, the text is no longer a paragraph.
Rollback on the history before the script runs.
Copy link to clipboard
Copied
alright thank you!
Copy link to clipboard
Copied
// 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);
};
}