Skip to main content
Participant
July 22, 2024
Answered

How to Write a Script to Create a Text Layer with Two Different Fonts in Photoshop JSX

  • July 22, 2024
  • 1 reply
  • 903 views

Hi everyone,

I'm working on a Photoshop script using JavaScript (JSX) and I need to create a text layer that contains two different fonts within the same text layer. Specifically, I want to have one part of the text in one font and another part in a different font.

For example, I want to create a text layer with the content "Hello World", where "Hello" is in Arial-Bold and "World" is in Times New Roman.

Could someone please provide an example of how to achieve this? Any guidance or code snippets would be greatly appreciated.

Thank you!

This topic has been closed for replies.
Correct answer Lumigraphics

Doing this is a **MAJOR** pain with Extendscript. It requires Action Manager code or you always get the same type settings for the entire layer.

Below is a demo script, you would need to modify it to select the part of your text you want to format differently. I have it splitting the string on a return but you would want to use a space.

This script is provided as-is, you are on your own with it!!!!

 

/*
Utility Pack Scripts created by David M. Converse ©2018-24

This script demos formatting a text layer

Last modified 7/22/2024

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#target photoshop

layerUpdate();

function setFormatting(start, end, fontName, fontStyle, fontSize){
    var idsetd = app.charIDToTypeID('setd');
    var action = new ActionDescriptor();
    var idnull = app.charIDToTypeID('null');
    var reference = new ActionReference();
    var idTxLr = app.charIDToTypeID('TxLr');
    var idOrdn = app.charIDToTypeID('Ordn');
    var idTrgt = app.charIDToTypeID('Trgt');
    reference.putEnumerated(idTxLr, idOrdn, idTrgt);
    action.putReference(idnull, reference);
    var idT = app.charIDToTypeID('T   ');
    var textAction = new ActionDescriptor();
    var idTxtt = app.charIDToTypeID('Txtt');
    var actionList = new ActionList();
    var textRange = new ActionDescriptor();
    var idFrom = app.charIDToTypeID('From');
    textRange.putInteger(idFrom, start);
    textRange.putInteger(idT, end);
    var idTxtS = app.charIDToTypeID('TxtS');
    var formatting = new ActionDescriptor();
    var idFntN = app.charIDToTypeID('FntN');
    formatting.putString(idFntN, fontName);
    var idFntS = app.charIDToTypeID('FntS');
    formatting.putString(idFntS, fontStyle);
    var idSz = app.charIDToTypeID('Sz  ');
    var idPnt = app.charIDToTypeID('#Pnt');
    formatting.putUnitDouble(idSz, idPnt, fontSize);
    textRange.putObject(idTxtS, idTxtS, formatting);
    actionList.putObject(idTxtt, textRange);
    textAction.putList(idTxtt, actionList);
    action.putObject(idT, idTxLr, textAction);
    app.executeAction(idsetd, action, DialogModes.NO);
	}

function layerUpdate(){
    if(documents.length > 0){
        //declare demo variables
        var text1 = "test";
        var text2 = "test replacement";
        var oldtSize = 48;
        var tSize = 24;
        var tFont = "Calibri";
        var tStyle = "bold";
        var tLead = 24;
        var boxH = 200;
        var boxW = 400;
        var tColor = new SolidColor;
        tColor.rgb.hexValue = "FF0000";
        //end demo variables
        var originalDialogMode = app.displayDialogs;
        app.displayDialogs = DialogModes.ERROR;
        var originalRulerUnits = preferences.rulerUnits;
        var j = 0;
        try{
            var docRef = activeDocument;
            preferences.rulerUnits = Units.POINTS;
            var m = 0;
            for(var i = 0; i < docRef.artLayers.length; i++){
                var LayerRef = docRef.artLayers[i];
                if(LayerRef.kind == LayerKind.TEXT){
                    var TextRef = LayerRef.textItem;
                    TextRef.textComposer = TextComposer.ADOBESINGLELINE;
                    var layerText = TextRef.contents;
                    var newText = layerText.replace(text1, text2);
                    if(newText != layerText){
                        j = i;
                        TextRef.contents = newText;
                        if(TextRef.size == oldtSize){
                            TextRef.size = tSize;
                            TextRef.font = tFont;
                            TextRef.useAutoLeading = false;
                            TextRef.leading = tLead;
                            TextRef.color = tColor;
                            var l = TextRef.contents.split(/\r/);
                            docRef.activeLayer = LayerRef;
                            setFormatting(0, l[0].length, tFont, tStyle, oldtSize);
                            preferences.rulerUnits = Units.PIXELS;
                            if(TextRef.kind == TextType.PARAGRAPHTEXT){
                                TextRef.height = boxH;
                                TextRef.width = boxW;
                                }
                            preferences.rulerUnits = Units.POINTS;
                            break;
                            }
                        }
                    }
                }
            }
        catch(e){
            alert(e + '  ' + e.line);
            preferences.rulerUnits = originalRulerUnits;
            app.displayDialogs = originalDialogMode;
            return;
            }
        preferences.rulerUnits = originalRulerUnits;
        app.displayDialogs = originalDialogMode;
        }
    else{
        alert('You must have a document open to run this script.');
        return;
        }
    }

1 reply

LumigraphicsCorrect answer
Legend
July 22, 2024

Doing this is a **MAJOR** pain with Extendscript. It requires Action Manager code or you always get the same type settings for the entire layer.

Below is a demo script, you would need to modify it to select the part of your text you want to format differently. I have it splitting the string on a return but you would want to use a space.

This script is provided as-is, you are on your own with it!!!!

 

/*
Utility Pack Scripts created by David M. Converse ©2018-24

This script demos formatting a text layer

Last modified 7/22/2024

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#target photoshop

layerUpdate();

function setFormatting(start, end, fontName, fontStyle, fontSize){
    var idsetd = app.charIDToTypeID('setd');
    var action = new ActionDescriptor();
    var idnull = app.charIDToTypeID('null');
    var reference = new ActionReference();
    var idTxLr = app.charIDToTypeID('TxLr');
    var idOrdn = app.charIDToTypeID('Ordn');
    var idTrgt = app.charIDToTypeID('Trgt');
    reference.putEnumerated(idTxLr, idOrdn, idTrgt);
    action.putReference(idnull, reference);
    var idT = app.charIDToTypeID('T   ');
    var textAction = new ActionDescriptor();
    var idTxtt = app.charIDToTypeID('Txtt');
    var actionList = new ActionList();
    var textRange = new ActionDescriptor();
    var idFrom = app.charIDToTypeID('From');
    textRange.putInteger(idFrom, start);
    textRange.putInteger(idT, end);
    var idTxtS = app.charIDToTypeID('TxtS');
    var formatting = new ActionDescriptor();
    var idFntN = app.charIDToTypeID('FntN');
    formatting.putString(idFntN, fontName);
    var idFntS = app.charIDToTypeID('FntS');
    formatting.putString(idFntS, fontStyle);
    var idSz = app.charIDToTypeID('Sz  ');
    var idPnt = app.charIDToTypeID('#Pnt');
    formatting.putUnitDouble(idSz, idPnt, fontSize);
    textRange.putObject(idTxtS, idTxtS, formatting);
    actionList.putObject(idTxtt, textRange);
    textAction.putList(idTxtt, actionList);
    action.putObject(idT, idTxLr, textAction);
    app.executeAction(idsetd, action, DialogModes.NO);
	}

function layerUpdate(){
    if(documents.length > 0){
        //declare demo variables
        var text1 = "test";
        var text2 = "test replacement";
        var oldtSize = 48;
        var tSize = 24;
        var tFont = "Calibri";
        var tStyle = "bold";
        var tLead = 24;
        var boxH = 200;
        var boxW = 400;
        var tColor = new SolidColor;
        tColor.rgb.hexValue = "FF0000";
        //end demo variables
        var originalDialogMode = app.displayDialogs;
        app.displayDialogs = DialogModes.ERROR;
        var originalRulerUnits = preferences.rulerUnits;
        var j = 0;
        try{
            var docRef = activeDocument;
            preferences.rulerUnits = Units.POINTS;
            var m = 0;
            for(var i = 0; i < docRef.artLayers.length; i++){
                var LayerRef = docRef.artLayers[i];
                if(LayerRef.kind == LayerKind.TEXT){
                    var TextRef = LayerRef.textItem;
                    TextRef.textComposer = TextComposer.ADOBESINGLELINE;
                    var layerText = TextRef.contents;
                    var newText = layerText.replace(text1, text2);
                    if(newText != layerText){
                        j = i;
                        TextRef.contents = newText;
                        if(TextRef.size == oldtSize){
                            TextRef.size = tSize;
                            TextRef.font = tFont;
                            TextRef.useAutoLeading = false;
                            TextRef.leading = tLead;
                            TextRef.color = tColor;
                            var l = TextRef.contents.split(/\r/);
                            docRef.activeLayer = LayerRef;
                            setFormatting(0, l[0].length, tFont, tStyle, oldtSize);
                            preferences.rulerUnits = Units.PIXELS;
                            if(TextRef.kind == TextType.PARAGRAPHTEXT){
                                TextRef.height = boxH;
                                TextRef.width = boxW;
                                }
                            preferences.rulerUnits = Units.POINTS;
                            break;
                            }
                        }
                    }
                }
            }
        catch(e){
            alert(e + '  ' + e.line);
            preferences.rulerUnits = originalRulerUnits;
            app.displayDialogs = originalDialogMode;
            return;
            }
        preferences.rulerUnits = originalRulerUnits;
        app.displayDialogs = originalDialogMode;
        }
    else{
        alert('You must have a document open to run this script.');
        return;
        }
    }
Participant
July 23, 2024

Thank You! it worked perfectly!