Change text font by (java)script
Copy link to clipboard
Copied
Actually, I want a custom panel with buttons for my preferred fonts, so I don't have to scroll through the whole endless list each time I want one of the max. 10 fonts I usually use.
I think that the best solution would be a custom panel with buttons for each font, so I could just select any text and click on the button for the font I want.
Now, I can get up to the point where it comes to the script itself. I've done only vbscript, but none in Photoshop. Can anyone give me the two or five lines of javascript to assign a font to a preselected text? I thought it might be something like this, but it doesn't work:
var selRef = app.activeDocument.selection;
selRef.font = "Arial";
selRef = null;
Is it normal that I don't get any hints on the syntax when I type code in the Extendscript Toolkit editor? It's a big pain in the a... to find anything for a beginner.
Thanks for your help!
Explore related tutorials & articles
Copy link to clipboard
Copied
One way of doing it is to create a text layer, set the font etc, then remove the layer leaving the font set.
IE:
if(documents.length){//make sure a document is open
var doc = activeDocument;
var fontSize = 40;
var fontName = "BrowalliaNew-Italic"; // NB: must be postscript name of font!
var newTextLayer = doc.artLayers.add();
newTextLayer.kind = LayerKind.TEXT;
newTextLayer.textItem.kind = TextType.POINTTEXT
newTextLayer.textItem.font = fontName;
newTextLayer.textItem.size = fontSize;
newTextLayer.textItem.contents = "Remove me";
doc.activeLayer.remove();
}
Copy link to clipboard
Copied
Thanks Paul, for the detailed example. I'll try to use that syntax in my own script (I just want it to change the font of an already selected text).
Also thanks to Michael. I'll keep you posted, but I think this should work.
Copy link to clipboard
Copied
Ok, it works if I select the text layer and then click my panel button, but not if I select a part of the text. Any way to cover that situation too?
Copy link to clipboard
Copied
The reason it doesn't work is you are setting a property named font to a selection object.
activeDocument.activeLayer.textItem.font = 'MyriadPro-Regular';
As Paul noted you need to use the postscript name of the font, not the name Photoshop displays in the GUI.

