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

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

Community Expert ,
Feb 21, 2024 Feb 21, 2024

Copy link to clipboard

Copied

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.

TOPICS
Actions and scripting

Views

697

Translate

Translate

Report

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 2 Correct answers

LEGEND , Feb 22, 2024 Feb 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);

 

 Screenshot 2024-02-22 at 12.37.50 PM.pngScreenshot 2024-02-22 at 12.38.23 PM.png

Votes

Translate

Translate
Community Expert , Feb 27, 2024 Feb 27, 2024

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. 

Votes

Translate

Translate
Adobe
Community Expert ,
Feb 21, 2024 Feb 21, 2024

Copy link to clipboard

Copied

// 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
};

Votes

Translate

Translate

Report

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 ,
Feb 25, 2024 Feb 25, 2024

Copy link to clipboard

Copied

@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..

Votes

Translate

Translate

Report

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 ,
Feb 25, 2024 Feb 25, 2024

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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 ,
Feb 25, 2024 Feb 25, 2024

Copy link to clipboard

Copied

I did some tests but possibly not extensively enough. 

Please provide a sample file (with just the Type Layers). 

Votes

Translate

Translate

Report

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 ,
Feb 26, 2024 Feb 26, 2024

Copy link to clipboard

Copied

Okay, wait for half a day, please.

Votes

Translate

Translate

Report

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 ,
Feb 26, 2024 Feb 26, 2024

Copy link to clipboard

Copied

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.

スクリーンショット 2024-02-27 9.57.46.png

Votes

Translate

Translate

Report

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 ,
Feb 27, 2024 Feb 27, 2024

Copy link to clipboard

Copied

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. 

Votes

Translate

Translate

Report

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 ,
Feb 27, 2024 Feb 27, 2024

Copy link to clipboard

Copied

Thanks for the verification. It seems that in the future, if I am lucky enough to achieve my goal, I will soon be faced with unknown problems.
I'm ready to give up, I need to learn UXP as soon as possible.

Votes

Translate

Translate

Report

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 ,
Feb 27, 2024 Feb 27, 2024

Copy link to clipboard

Copied

An inconvenient work-around might be saving a pdf-copy of the file, then getting the fonts-list from some other application (Acrobat, Illustrator, …). 

 

@Davide_Barranca , please forgive another importunity, but do you know if UXP Scripting can retrieve the fonts (including those from applied Paragraph Styles) more reliably than ExtendScript code? 

Votes

Translate

Translate

Report

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 ,
Feb 29, 2024 Feb 29, 2024

Copy link to clipboard

Copied

quote

@Davide_Barranca , please forgive another importunity, but do you know if UXP Scripting can retrieve the fonts (including those from applied Paragraph Styles) more reliably than ExtendScript code? 

 

By @c.pfaffenbichler

 

Hiya,

it might be worth looking at the Layer.textItem.characterStyle.font property, docs here:
https://developer.adobe.com/photoshop/uxp/2022/ps_reference/classes/characterstyle/

Text stuff additions in the UXP DOM were my job when I worked with the PS team, so if something's missing or bugged, I am mostly to blame 😁
Just to let you know, despite the number of new features compared to JSX, targeting individual chars within a text block is unavailable due to the awfully complex descriptors involved vs. the amount of time allocated for the porting.

 

 

Davide Barranca - PS developer and author
www.ps-scripting.com

Votes

Translate

Translate

Report

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 ,
Feb 29, 2024 Feb 29, 2024

Copy link to clipboard

Copied

Thank you! 

I am not up to speed with UXP Scripting, so I’ll have to save the link for some other time. 

 

If I understand correctly the case the OP of this thread has (multiple fonts in one Type Layer with at least one based on Paragraph Styles) will not work out ideally? 

Votes

Translate

Translate

Report

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
LEGEND ,
Feb 29, 2024 Feb 29, 2024

Copy link to clipboard

Copied

This is one of the problems with ExtendScript, is that its difficult to work with mixed attributes in the same text layer. There are some clunky workarounds with AM code and specifying character ranges but nothing easy to use.

Votes

Translate

Translate

Report

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 ,
Feb 29, 2024 Feb 29, 2024

Copy link to clipboard

Copied

LATEST

>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.

 

Is there a way about my additional requirements? 

In ExtendScript if possible.

Votes

Translate

Translate

Report

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
LEGEND ,
Feb 22, 2024 Feb 22, 2024

Copy link to clipboard

Copied

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

 

 Screenshot 2024-02-22 at 12.37.50 PM.pngScreenshot 2024-02-22 at 12.38.23 PM.png

Votes

Translate

Translate

Report

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 ,
Feb 25, 2024 Feb 25, 2024

Copy link to clipboard

Copied

@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.

Votes

Translate

Translate

Report

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 ,
Feb 25, 2024 Feb 25, 2024

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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