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

Change text font by (java)script

Participant ,
Dec 15, 2009 Dec 15, 2009

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!

TOPICS
Actions and scripting
5.3K
Translate
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
Adobe
Valorous Hero ,
Dec 15, 2009 Dec 15, 2009

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();
}

Translate
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
Participant ,
Dec 16, 2009 Dec 16, 2009

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.

Translate
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
Participant ,
Dec 16, 2009 Dec 16, 2009
LATEST

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?

Translate
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
Guru ,
Dec 15, 2009 Dec 15, 2009

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.

Translate
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