My code cannot retrieve the font size of the text layer.
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);
Explore related tutorials & articles
Copy link to clipboard
Copied
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"
Copy link to clipboard
Copied
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);
}
}
Copy link to clipboard
Copied
Error: General Photoshop error occurred. This functionality may not be available in this version of Photoshop.
- <no additional information available> 15
Copy link to clipboard
Copied
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)
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
You need to use points with fonts. And make sure PPI settings are the same between files.
Copy link to clipboard
Copied
However, the font sizes are different in each file, and when replacing text in bulk, all of them are being shrunk.
Copy link to clipboard
Copied
This isn't enough information to go on. What do you mean "shrunk." Is the font size changing?
Copy link to clipboard
Copied
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"
Copy link to clipboard
Copied
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) {}
};
Copy link to clipboard
Copied
It can be replaced, but after replacing, the text will still be resized to a font size of 12
Copy link to clipboard
Copied
Sorry, it seemed to work in a previous test but now it misbehaves as you describe.
Copy link to clipboard
Copied
Thank you for your reply, I will continue to look for a solution.
Copy link to clipboard
Copied
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');
Copy link to clipboard
Copied
Thank you for your response. After replacing the text, it still gets reduced to 12-point font.
Copy link to clipboard
Copied
In Preferences->Type do you have "Set default font size automatically" checked?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Could this be a compatibility bug with the Photoshop software?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.


-
- 1
- 2