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

My code cannot retrieve the font size of the text layer.

Participant ,
Mar 20, 2025 Mar 20, 2025

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);

TOPICS
Actions and scripting
556
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

Community Expert , Apr 06, 2025 Apr 06, 2025

Edit: This would level out differently sized letters, but still …

Screenshot 2025-04-06 at 15.54.15.pngexpand imageScreenshot 2025-04-06 at 15.54.19.pngexpand image

// 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 
...
Translate
Participant ,
Mar 21, 2025 Mar 21, 2025

When I create a new document, drag the text over, and replace the text, it still gets resized to 12 point.

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
Community Expert ,
Mar 23, 2025 Mar 23, 2025

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. 

Screenshot 2025-03-23 at 17.53.01.pngexpand imageScreenshot 2025-03-23 at 17.55.18.pngexpand imageScreenshot 2025-03-23 at 17.55.28.pngexpand image

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
Participant ,
Mar 23, 2025 Mar 23, 2025

But after clicking "Reset Character," the text immediately shrinks.

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
Community Expert ,
Apr 06, 2025 Apr 06, 2025

Edit: This would level out differently sized letters, but still …

Screenshot 2025-04-06 at 15.54.15.pngexpand imageScreenshot 2025-04-06 at 15.54.19.pngexpand image

// 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)}
};
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
Participant ,
Apr 06, 2025 Apr 06, 2025
LATEST

Thank you so much, you are truly a lifesaver. 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
Enthusiast ,
Apr 06, 2025 Apr 06, 2025

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

Schermata 2025-04-06 alle 17.32.53.pngexpand image

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