Copy link to clipboard
Copied
My code cannot retrieve the font size of the text layer.
#target photoshop
alert(app.activeDocument.activeLayer.textItem.size);
#target photoshop
// Get the active document
var doc = app.activeDocument;
// Get the active layer
var selectedLayer = doc.activeLayer;
// Check if the active layer is a text layer
if (selectedLayer.kind == LayerKind.TEXT) {
// If it's a text layer, read the font size and alert it
alert("Current font size is: " + selectedLayer.textItem.size);
} else {
// If it's not a text layer, alert the user
alert("The active layer is not a text layer!");
}
Error 8800: General Photoshop error occurred. This functionality may not be available in this version of Photoshop.
- <no additional information available> Line: 12 -> alert("Current font size is: " + selectedLayer.textItem.size);
1 Correct answer
Edit: This would level out differently sized letters, but still …
// 2025, use it at your own risk;
changeText ("Hxxxxx12");
////// stuff //////
function changeText (theText) {
try {
var ref = new ActionReference();
ref.putProperty(stringIDToTypeID("property"), stringIDToTypeID('textKey'));
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var layerDesc = executeActionGet(ref);
var textDesc = layerDesc.getObjectValue(stringIDToTypeID('textKey'));
var
...
Explore related tutorials & articles
Copy link to clipboard
Copied
When I create a new document, drag the text over, and replace the text, it still gets resized to 12 point.
Copy link to clipboard
Copied
Even if I feed the textKey Object back to the ActionDescriptor the size changes – which does not happen with a completely new type layer in a new document.
There seems to be something »special« about the Layer/File …
Edit: Resetting the Characters does naturally remove the color etc., but leaves the size unchanged and subsequently the Script seems to work as intended on the Layer.
Copy link to clipboard
Copied
But after clicking "Reset Character," the text immediately shrinks.
Copy link to clipboard
Copied
Edit: This would level out differently sized letters, but still …
// 2025, use it at your own risk;
changeText ("Hxxxxx12");
////// stuff //////
function changeText (theText) {
try {
var ref = new ActionReference();
ref.putProperty(stringIDToTypeID("property"), stringIDToTypeID('textKey'));
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var layerDesc = executeActionGet(ref);
var textDesc = layerDesc.getObjectValue(stringIDToTypeID('textKey'));
var textStyleRange = textDesc.getList(stringIDToTypeID('textStyleRange'));
////////////////////////////////////
var textStyleRangeX = new ActionList();
for (var o = 0; o < textStyleRange.count; o++) {
var thisListItem = textStyleRange.getObjectValue(o);
if (thisListItem.getObjectValue(thisListItem.getKey(2)).hasKey(stringIDToTypeID("size")) == false && thisListItem.getObjectValue(thisListItem.getKey(2)).hasKey(stringIDToTypeID("baseParentStyle")) == true) {
if (thisListItem.getObjectValue(thisListItem.getKey(2)).getObjectValue(stringIDToTypeID("baseParentStyle")).hasKey(stringIDToTypeID("impliedFontSize"))) {
var theSize = thisListItem.getObjectValue(thisListItem.getKey(2)).getObjectValue(stringIDToTypeID("baseParentStyle")).getUnitDoubleValue(stringIDToTypeID("impliedFontSize"));
} else {}
} else {};
textStyleRangeX.putObject(stringIDToTypeID( "textStyleRange" ), thisListItem);
};
////////////////////////////////////
var desc179 = new ActionDescriptor();
var ref1 = new ActionReference();
ref1.putEnumerated( stringIDToTypeID( "textLayer" ), stringIDToTypeID( "ordinal" ), stringIDToTypeID( "targetEnum" ) );
desc179.putReference( stringIDToTypeID( "null" ), ref1 );
////////////////////////////////////
var desc180 = new ActionDescriptor();
desc180.putList( stringIDToTypeID( "textStyleRange" ), textStyleRangeX );
desc179.putObject( stringIDToTypeID( "to" ), stringIDToTypeID( "textLayer" ), desc180 );
////////////////////////////////////
textDesc.putString( stringIDToTypeID( "textKey" ), theText );
desc179.putObject( stringIDToTypeID( "to" ), stringIDToTypeID( "textKey" ), textDesc );
executeAction( stringIDToTypeID( "set" ), desc179, DialogModes.NO );
if (theSize) {activeDocument.activeLayer.textItem.size = theSize};
} catch (e) {alert (e)}
};
Copy link to clipboard
Copied
Thank you so much, you are truly a lifesaver. Thank you
Copy link to clipboard
Copied
I tried this piece of code and on my photoshop on mac m1 it works fine.
var doc = app.activeDocument;
// Get the active layer
var selectedLayer = doc.activeLayer;
// Check if the active layer is a text layer
if (selectedLayer.kind == LayerKind.TEXT) {
// If it's a text layer, read the font size and alert it
alert("Current font size is: " + selectedLayer.textItem.size);
} else {
// If it's not a text layer, alert the user
alert("The active layer is not a text layer!");
}
this comes out


-
- 1
- 2