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

Trouble setting font to textItem font in text layer

Community Expert ,
Oct 13, 2022 Oct 13, 2022

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"

 

TOPICS
Actions and scripting

Views

48

Likes

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 1 Correct answer

Community Expert , Oct 13, 2022 Oct 13, 2022

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"

 

Likes

Translate

Translate
Community Expert ,
Oct 13, 2022 Oct 13, 2022

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

 

Likes

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 ,
Oct 13, 2022 Oct 13, 2022

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"

 

Best regards

Likes

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 ,
Oct 13, 2022 Oct 13, 2022

Copy link to clipboard

Copied

Thank you again, Charu. I owe you big time! 

Likes

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 ,
Oct 13, 2022 Oct 13, 2022

Copy link to clipboard

Copied

@brianp311 – Feedback requested: What was wrong or lacking in my reply?

Likes

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 ,
Oct 13, 2022 Oct 13, 2022

Copy link to clipboard

Copied

Hi @Stephen_A_Marsh 

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.

Likes

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 ,
Oct 13, 2022 Oct 13, 2022

Copy link to clipboard

Copied

LATEST

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

Likes

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