Skip to main content
ajabon grinsmith
Community Expert
Community Expert
February 22, 2024
Answered

How to get postscript font name for text with paragraph or character styles applied

  • February 22, 2024
  • 3 replies
  • 2229 views

Hi there, ,
As the title says, I want to get the postscript name of the font applied to a text layer in ExtendScript.
I was able to achieve this by searching the forum logs if I am specifying the font directly.
But if a paragraph style or character style is applied, I get nothing.
I am not bright in AM code.
Please help me.

This topic has been closed for replies.
Correct answer c.pfaffenbichler

Now, we have a test document that is similar to the actual test data. Created in version 2023(24.7.2).
If we could get eight font names from this entire document, we would be satisfied.

Until the client pointed it out to me, I had not imagined that there would be cases where different fonts would be used from layers within a group or from the middle of a single text layer. It can actually happen..

thanks.


I am afraid there may be a more general bug. 

Even if I copy a word from a Type Layer (set up with a Paragraph Style) and paste it into a new Type Layer in a new document the font the Paragraph Style-Font appears to be disregarded and I get Myriad Pro-Regular. 

 

As I generally don’t do type-heavy work in Photoshop I hadn’t noticed so far. 

 

Maybe UXP Scripting can access the font properties in a »better« way than ExtendScript. 

3 replies

ajabon grinsmith
Community Expert
Community Expert
February 26, 2024

Would it be possible to change the approach and get the PostScript names of the fonts defined for all paragraph and character styles in the document?


That is, even if those are not actually applied to the text layer.

Legend
February 22, 2024

The DOM is apparently buggy.

With this code you get different results depending on whether a paragraph style is applied. With a style, the DOM code returns an incorrect result. Without a style, the correct font is returned.

var a = app.activeDocument.activeLayer.textItem.font;
var b = app.fonts.getByName(a);
Window.alert(a + ' ' + b.postScriptName);

 

 

ajabon grinsmith
Community Expert
Community Expert
February 26, 2024

@Lumigraphics 

Thank you. I could not get the ".font" property on the text layer where the first character was not overwritten and I got an error.
Aside from that, I have confirmed that the phenomenon you mentioned is definitely the case.

c.pfaffenbichler
Community Expert
Community Expert
February 22, 2024
// thanks to mike hale and paul riggott;
// 2015, use it at your own risk;
if (app.documents.length > 0) {
var myDocument = app.activeDocument;
var theFonts = selectedLayersFonts ();
alert ("the result:"+"\n"+theFonts.join("\n"))
};
////////////////////////////////////
function selectedLayersFonts () {
var theFonts = new Array;
////////////////////////////////////
var selectedLayers = new Array; 
var ref = new ActionReference(); 
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
var desc = executeActionGet(ref); 
if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){ 
 desc = desc.getList( stringIDToTypeID( 'targetLayers' )); 
  var c = desc.count 
  var selectedLayers = new Array(); 
  for(var i=0;i<c;i++){ 
	try{ 
	   activeDocument.backgroundLayer; 
	   selectedLayers.push(  desc.getReference( i ).getIndex() ); 
	}catch(e){ 
	   selectedLayers.push(  desc.getReference( i ).getIndex()+1 ); 
	} 
  } 
}else{ 
 var ref = new ActionReference(); 
 ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "ItmI" )); 
 ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
 try{
	activeDocument.backgroundLayer;
	selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))-1);
 }catch(e){
	selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" )));
 }
};
////////////////////////////////////
var theNumber = selectedLayers.length;
////////////////////////////////////
for (var m = theNumber; m >= 0; m--) {
try {
var ref = new ActionReference();
ref.putIndex( charIDToTypeID( "Lyr " ), selectedLayers[m]);
var layerDesc = executeActionGet(ref);
var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));
var isBackground = layerDesc.getBoolean(stringIDToTypeID("background"));
var theName = layerDesc.getString(stringIDToTypeID('name'));
// if not layer group collect values;
if (layerSet != "layerSectionEnd" && layerSet != "layerSectionStart" && isBackground != true) {
var hasText = layerDesc.hasKey(stringIDToTypeID("textKey"));
if (hasText == true) {
var textDesc = layerDesc.getObjectValue(stringIDToTypeID('textKey'));
//checkDesc2 (textDesc)
var paragraphRangeList = textDesc.getList(stringIDToTypeID('paragraphStyleRange'));
var kernRange = textDesc.getList(stringIDToTypeID('kerningRange'));
var rangeList = textDesc.getList(stringIDToTypeID('textStyleRange'));
for (var o = 0; o < rangeList.count; o++) {
var styleDesc = rangeList.getObjectValue(o).getObjectValue(stringIDToTypeID('textStyle'));
//checkDesc2 (styleDesc)
var theSize = styleDesc.getUnitDoubleValue(stringIDToTypeID('size'));
// check for default font;
if (styleDesc.hasKey(stringIDToTypeID('fontPostScriptName')) == true) {var aFont = styleDesc.getString(stringIDToTypeID('fontPostScriptName'))}
else {
	var theDefault = styleDesc.getObjectValue(stringIDToTypeID('baseParentStyle'));
	var aFont = theDefault.getString(stringIDToTypeID('fontPostScriptName'));
	};
theFonts.push([aFont, theSize])
}
}
}
} catch (e) {};
}
return theFonts
};
ajabon grinsmith
Community Expert
Community Expert
February 26, 2024

@c.pfaffenbichler 

Thanks, I was on holiday and delayed confirmation.


I was able to get the postscript name of the selected layers.
However, the fonts defined in paragraph styles and/ or character styles were ignored.


I have confirmed this with versions of Photoshop 2021 through 2024.
Did this code work well in earlier versions?
Is ExtendScript no longer preferred to make it work in current versions..

ajabon grinsmith
Community Expert
Community Expert
February 26, 2024

I have tried to start past OS backups and run them in CS6, CC2017 and got nothing as well.
That's a shame.