Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
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
421
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
Adobe
Participant ,
Mar 20, 2025 Mar 20, 2025

My PSD file is very strange. When I use this code to replace the text content, the text gets smaller. How can I ensure that the text does not shrink? Thank you, everyone!

#target photoshop
app.activeDocument.activeLayer.textItem.contents = "night"

 

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 ,
Mar 20, 2025 Mar 20, 2025

Complete sample with error trapping. If your text layer has multiple text sizes, only the first will be reported.

 

#target photoshop
tSize();
function tSize(){
    try{
        if(documents.length > 0){
            var originalDialogMode = app.displayDialogs;
            app.displayDialogs = DialogModes.ERROR;
            var originalRulerUnits = preferences.rulerUnits;
            preferences.rulerUnits = Units.POINTS;
            var docRef = app.activeDocument;
            var layerRef = docRef.activeLayer;
            var textSize = 0;
            if(layerRef.kind == LayerKind.TEXT){
                var textRef = layerRef.textItem;
                testSize = textRef.size;
                Window.alert('Current font size is: ' + testSize.toString());
                }
            app.displayDialogs = originalDialogMode;
            preferences.rulerUnits = originalRulerUnits;
            }
        }
    catch(e){
        app.displayDialogs = originalDialogMode;
        preferences.rulerUnits = originalRulerUnits;
        Window.alert(e + ' ' + e.line);
        }
    }

 

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 20, 2025 Mar 20, 2025
Thank you very much for your first reply, but the following error occurred.


Error: General Photoshop error occurred. This functionality may not be available in this version of Photoshop.
- <no additional information available> 15

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 20, 2025 Mar 20, 2025

I found a solution for getting the font size on the forum, but the issue that still confuses me is that the font shrinks after replacing the text. I have been unable to solve this, so I’m asking for help from the experts here. Thank you!

//https://community.adobe.com/t5/photoshop-ecosystem-discussions/read-font-size-with-script/m-p/10697934
app.currentTool = "typeCreateOrEditTool";

try {
    var r = new ActionReference();
    var d = new ActionDescriptor();
    r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("tool"));
    r.putEnumerated(charIDToTypeID("capp"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
    var options = executeActionGet(r);
    var object = options.getObjectValue(stringIDToTypeID("currentToolOptions")).getObjectValue(stringIDToTypeID("textToolCharacterOptions")).getObjectValue(stringIDToTypeID("textStyle"));


    alert(object.getUnitDoubleValue(stringIDToTypeID("size")));
} 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 ,
Mar 20, 2025 Mar 20, 2025

Have you set your ruler units properly? And you don't need to use Action Manager code to get the font size, it can be done in the DOM.

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 20, 2025 Mar 20, 2025

Yes, I set the ruler to pixel units. There are no issues with the settings on other computers, but for some reason, on certain computers, this problem occurs. When I replace the text in the PSD file I uploaded, it shrinks. You can download the PSD file and test it yourself—just replacing the text will cause it to shrink.

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 ,
Mar 20, 2025 Mar 20, 2025

You need to use points with fonts. And make sure PPI settings are the same between files.

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 20, 2025 Mar 20, 2025

However, the font sizes are different in each file, and when replacing text in bulk, all of them are being shrunk.

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 ,
Mar 20, 2025 Mar 20, 2025

This isn't enough information to go on. What do you mean "shrunk." Is the font size changing?

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 20, 2025 Mar 20, 2025

Yes, the font size changes. When I use the following code to replace the text, the font size changes. The original font size was 161.34 points, but after using the replacement text code, the font size changes to 12.

#target photoshop
app.activeDocument.activeLayer.textItem.contents = "txt"
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 21, 2025 Mar 21, 2025

Does this work? 

changeText ("hahaha")
function changeText (theText) {
try {
var idset = stringIDToTypeID( "set" );
    var desc583 = new ActionDescriptor();
    var idnull = stringIDToTypeID( "null" );
        var ref2 = new ActionReference();
        ref2.putEnumerated( stringIDToTypeID( "textLayer" ), stringIDToTypeID( "ordinal" ), stringIDToTypeID( "targetEnum" ) );
    desc583.putReference( idnull, ref2 );
    var idto = stringIDToTypeID( "to" );
        var desc584 = new ActionDescriptor();
        var idtextKey = stringIDToTypeID( "textKey" );
        desc584.putString( idtextKey, theText );
    var idtextLayer = stringIDToTypeID( "textLayer" );
    desc583.putObject( idto, idtextLayer, desc584 );
executeAction( idset, desc583, DialogModes.NO );
} catch (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 ,
Mar 21, 2025 Mar 21, 2025

It can be replaced, but after replacing, the text will still be resized to a font size of 12

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 21, 2025 Mar 21, 2025

Sorry, it seemed to work in a previous test but now it misbehaves as you describe. 

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 21, 2025 Mar 21, 2025

Thank you for your reply, I will continue to look for a solution.

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 ,
Mar 21, 2025 Mar 21, 2025

This snippet works correctly on both Mac and Windows:

var docRef = app.activeDocument;
var layerRef = docRef.activeLayer;
var textRef = layerRef.textItem;
textRef.contents = Window.prompt('Enter text');
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 21, 2025 Mar 21, 2025

Thank you for your response. After replacing the text, it still gets reduced to 12-point font.

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 ,
Mar 21, 2025 Mar 21, 2025

In Preferences->Type do you have "Set default font size automatically" checked?

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 21, 2025 Mar 21, 2025

I have turned the "Auto Set as Default Font Size" option on and off in the preferences, and I restart every time I make a change, but it still shrinks. However, when I open the PSD file in the CS6 version, replacing the text works fine. I'm not sure what's going on.

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 ,
Mar 21, 2025 Mar 21, 2025

When I run this on an empty text layer, the font goes to Myriad Pro 9 but on a layer with existing text, the styling doesn't change. Regardless of the setting for default sizing.

I suspect a bug here.

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 21, 2025 Mar 21, 2025

Could this be a compatibility bug with the Photoshop software?

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 ,
Mar 21, 2025 Mar 21, 2025

https://community.adobe.com/t5/photoshop-ecosystem-bugs/setting-text-contents-via-script-changes-for...

 

I think its a scripting bug. Formatting changes if I fill an empty text layer via script but not if text is already there. Both Mac and Windows.

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 21, 2025 Mar 21, 2025

I wonder if there may be something particular about that Type Layer yet. 

Copying the text manually and pasting it in a new Type Layer seems to result in 12pt text, too. 

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 ,
Mar 21, 2025 Mar 21, 2025

I tried with new documents on both Mac and Windows machines, changing the default type setting as noted. Creating an empty, formatted text layer via script and then adding text, either manually or via a second script, works fine, no formatting changes. something odd is going on.

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 21, 2025 Mar 21, 2025

I manually created a new document, dragged the text over, and replaced the text via script. After replacement, it still gets resized to 12 point, but manually replacing the text does not cause it to shrink.

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