Skip to main content
Retsied
Known Participant
February 20, 2019
Answered

Script for changing font of one layer, only certain fonts working

  • February 20, 2019
  • 2 replies
  • 2460 views

I have a script in which I select the top layer of the psd file, which is a text layer, then change the font and finally resize it to a certain px width. Below is the code. The problem, is that only certain fonts seem to work. Some either don't do anything, remove the text entirely, or scale the text to a different size than specified in the script. It seems random. Several of the fonts I am attempting to apply were downloaded online (1001fonts, Dafonts, etc), but I haven't found any correlation as to which ones work/don't work.

I recently moved all downloaded fonts in FontBook from User (~/Library/Fonts/) to Computer (/Library/Fonts/), thinking the desired font would need to be accessible to all users, but this didn't work.

Any suggestions? Thanks!

#target photoshop 

function main(){ 

if(!documents.length) return; 

var startRulerUnits = app.preferences.rulerUnits; 

app.preferences.rulerUnits = Units.PIXELS; 

app.activeDocument.activeLayer = app.activeDocument.layers[0];

app.activeDocument.activeLayer.textItem.font = "Helvetica";

var doc = activeDocument; 

var res= doc.resolution; 

var LB = activeDocument.activeLayer.bounds; 

var Width= LB[2].value - LB[0].value; 

var onePix = 100/Width; 

var newSize = onePix * 1000; 

doc.activeLayer.resize( newSize , newSize, AnchorPosition.MIDDLECENTER);  

app.preferences.rulerUnits = startRulerUnits; 

main(); 

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

    Scaled because I always want it to be a given proportion to document size, regardless of font.

    My attempt to change fonts:

    app.activeDocument.activeLayer.textItem.font = "font name";


    What is "font name" exactly?

    Again: Do you use the correct postScriptName of the font?

    2 replies

    Legend
    February 20, 2019

    You should probably make sure its a text layer, and then make sure you are using the correct font name. An incorrect font name won't work and a non-text ArtLayer won't work. You also want to be sure to use the correct text box type.

            for(var i = 0; i < docRef.artLayers.length; i++){
                var LayerRef = docRef.artLayers;
                if(LayerRef.kind == LayerKind.TEXT){
                    var TextRef = LayerRef.textItem;
                    TextRef.textComposer = TextComposer.ADOBESINGLELINE;
    c.pfaffenbichler
    Community Expert
    Community Expert
    February 20, 2019
    scale the text to a different size than specified in the script.

    Do all the files have the same resolution?

    And why did you post on the General Forum instead of the Scripting Forum?

    Retsied
    RetsiedAuthor
    Known Participant
    February 20, 2019

    Single file only. Sorry if this is in the wrong location.

    c.pfaffenbichler
    Community Expert
    Community Expert
    February 20, 2019

    Why do you scale the Layer?

    The Scripting Forum:

    Photoshop Scripting