How to get and set the font?

If I try to get the font of the current text layer through the .psjs file, the code I wrote below is incorrect for some reason. How can I set the font of the current text layer to: DOUYINSANSBOLD-GB?

const { app } = require("photoshop");
async function getSelectedTextLayerFont() {
    try {
        let font = app.activeDocument.activeLayer.textItem.font;
        await app.showAlert(`Font: ${font}`);
    } catch (error) {
        await app.showAlert(`Error: ${error.message}`);
    }
}
getSelectedTextLayerFont();

Try this out. There are some edge cases you’ll need to address in a complete solution but this should get you headed in the right direction.

const { app } = require("photoshop");

//check for an active document
const doc = app.activeDocument;
if (!doc) {
    app.showAlert("No active documents.");
    return;
}

// get the **first** selected layer
const layer = doc.activeLayers[0];

// update the layer to use the target font
targetFontPostScriptName = "ArialMT";
try {
    layer.textItem.characterStyle.font = targetFontPostScriptName; //
} catch (error) {
    app.showAlert(error);
}

// If you are unsure of the PostScript name of the font you want to use
// here is a little helper function that will show all fonts whose name includes
// the string argument you supply to the function.

/**
 * Log the PostScript names of any fonts whose name includes `s`.
 * @param s String to search for inside of a font name.
 */
function logMatchingFonts(searchString) {
    const matches = app.fonts
        .filter((font) => font.name.toLowerCase().includes(searchString.toLowerCase()))
        .map((font) => font.postScriptName);

    app.showAlert(`Found ${matches.length} matches:\n\n${matches.join("\n")}`);
}

// logMatchingFonts("arial");
2 Likes

Thank you for guiding me in the right direction, thank you.

1 Like