Skip to main content
IanBortz
Participant
February 8, 2023
Question

Changing the numbers in the text to a different font via script in photohsop

  • February 8, 2023
  • 1 reply
  • 185 views

Hi, usgin chatgpt im trying to make a photoshop script that scans all the text layer in my psd and only changes the font of numbers, so far im faling to change the font only the carachters its changing the whole text layer, i can post some examples but in all of them i can read the layers and get the index of the number carachter i want to change but im faling to find the right function to change only this carachters to a specified font. can anyone help?

This topic has been closed for replies.

1 reply

Legend
February 8, 2023

Changing just part of a line is extremely difficult, it requires custom scripting.

This sample script separates a text layer at a return character and formats. You would have to customize it to select and send only number characters which may not be easy. I do not have time to modify it but the script is Apache-licensed so modify it as much as you want.

 

/*
Utility PS Scripts created by David M. Converse ©2018-21

This script replaces text and sets styling

Last modified 6/2/2021

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){ //format text per input
    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(){
    //sample values
        var size1 = 96
        var size2 = 42;
        var font1 = 'Calibri';
        var style1 = 'bold';
        var texth = 220;
        var textw = 460;
    
    if(documents.length > 0){
        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;
                    //can iterate through multiple find/replace pairs
                    var newText = layerText.replace('old', 'new');
                    newText = newText.replace('old-1', 'new-1');
                    newText = newText.replace('old-2', 'new-2');
                    if(newText != layerText){
                        j = i;
                        TextRef.contents = newText;
                        if(TextRef.size == size1){
                            TextRef.size = size2;
                            TextRef.font = font1;
                            TextRef.useAutoLeading = false;
                            TextRef.leading = size2;
                            var l = TextRef.contents.split(/\r/);
                            docRef.activeLayer = LayerRef;
                            setFormatting(0, l[0].length, font1, style1, size1); //send to formatting function
                            preferences.rulerUnits = Units.PIXELS;
                            if(TextRef.kind == TextType.PARAGRAPHTEXT){
                                TextRef.height = texth;
                                TextRef.width = textw;
                                }
                            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;
        }
    }
IanBortz
IanBortzAuthor
Participant
February 8, 2023

Thank you very very much, i will try to customize the code!