Copy link to clipboard
Copied
Considering the following snippet, the existing layer's font is something other than Myriad Pro and Arial. When I replace the contents of its textItem and set the font, it switches to Myriad Pro. Any thoughts on what I'm doing wrong? It won't change for many other fonts besides Arial. Thanks.
$.writeln("Contents--" + lay.textItem.contents); //Currently "Goodbye World"
lay.textItem.contents = "Hello World";
var fnt = app.fonts.getByName("Arial").name;
lay.textItem.font = fnt;
$.writeln(lay.textItem.contents); //"Hello World"
$.writeln(lay.textItem.font); //"Myriad Pro"
HI @brianp311 ,
To set the font, you need to use the postScriptName property, try following
var lay = app.activeDocument.layers[0];
$.writeln("Contents--" + lay.textItem.contents); //Currently "Goodbye World"
lay.textItem.contents = "Hello World";
var fnt = app.fonts.getByName("ArialMT").postScriptName;
lay.textItem.font = fnt;
$.writeln(lay.textItem.contents); //"Hello World"
$.writeln(lay.textItem.font); //"Myriad Pro"
Copy link to clipboard
Copied
Try:
ArialMT
// https://raw.githubusercontent.com/frontendbeast/list-fonts/master/List%20Fonts.jsx
var refPSD = new ActionReference();
function arrayUnique(a){
var temp = []
i = a.length;
// ExtendScript has no indexOf function
while(i--) {
var found = false,
n = temp.length;
while (n--) {
if(a[i] === temp[n]) {
found = true;
}
}
if(!found) {
temp.push(a[i]);
}
}
return temp;
}
function findFonts() {
refPSD.putEnumerated( charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
// Get layers from PSD
var countLayers = executeActionGet(refPSD).getInteger(charIDToTypeID('NmbL'))+1,
fonts = [];
// Loop through each layer
while(countLayers--) {
var refLayer = new ActionReference(),
descLayer,
layerStyles,
countStyles;
refLayer.putIndex( charIDToTypeID( 'Lyr ' ), countLayers );
// Catch error when no backgroundLayer is present
try {
descLayer = executeActionGet(refLayer);
} catch (e) {
continue;
}
// Only proceed if text layer
if(!descLayer.hasKey(stringIDToTypeID( 'textKey' ))) continue;
// Get list of styles (in case of multiple fonts in a text layer)
layerStyles = descLayer.getObjectValue(stringIDToTypeID('textKey')).getList(stringIDToTypeID('textStyleRange'));
countStyles = layerStyles.count;
// Loop through each style and get the font name
while(countStyles--) {
try {
var fontName = layerStyles.getObjectValue(countStyles).getObjectValue(stringIDToTypeID('textStyle')).getString(stringIDToTypeID('fontPostScriptName'));
fonts.push(fontName);
} catch (e) {
continue;
}
}
}
// Return a unique and sorted array of font names
return arrayUnique(fonts).sort();
}
if (documents.length) {
var fontsFound = findFonts();
alert(fontsFound.length +' fonts found \n'+fontsFound.join('\n'));
} else {
alert('No fonts found \nOpen a PSD before running this script',);
}
/* https://stackoverflow.com/questions/10634302/how-to-get-the-font-style-from-the-psd-file-using-ps-script */
for (var i = app.activeDocument.layers.length -1; i >= 0; i--)
{
var tempLayer = app.activeDocument.artLayers[i]
if (tempLayer.kind == 'LayerKind.TEXT')
{
var textContents = tempLayer.textItem.contents;
var fontSize = tempLayer.textItem.size;
var fontFace = tempLayer.textItem.font;
alert (fontFace)
}
}
/*
// For ESTK or VS Code console only - don't change to an alert() !!!
for (i = 0; i < app.fonts.length; i++) {
//$.writeln(app.fonts[i].name);
$.writeln(app.fonts[i].postScriptName);
}
*/
Copy link to clipboard
Copied
HI @brianp311 ,
To set the font, you need to use the postScriptName property, try following
var lay = app.activeDocument.layers[0];
$.writeln("Contents--" + lay.textItem.contents); //Currently "Goodbye World"
lay.textItem.contents = "Hello World";
var fnt = app.fonts.getByName("ArialMT").postScriptName;
lay.textItem.font = fnt;
$.writeln(lay.textItem.contents); //"Hello World"
$.writeln(lay.textItem.font); //"Myriad Pro"
Copy link to clipboard
Copied
Thank you again, Charu. I owe you big time!
Copy link to clipboard
Copied
@brianp311 – Feedback requested: What was wrong or lacking in my reply?
Copy link to clipboard
Copied
As I mentioned, my code was not working for many fonts, not just Arial. Also, I was not trying to get the font of a given layer (at least, I assume that's what your code was doing); I was trying to set it given a random value of a font family name derived from CSV not shown in my script. I just boiled my problem down to the specific issue I was having, and Charu's solution proved correct. Thanks for the help.
Copy link to clipboard
Copied
Thank you for the feedback Brian, I didn't understand the extent of the issue, hopefully, the scripts I posted may help somebody else to find the "internal" font name (postScriptName).