Skip to main content
Known Participant
July 16, 2021
Answered

Photoshop script to change the font for Superscript(In glyph)

  • July 16, 2021
  • 2 replies
  • 6378 views

Hi Everyone,

Could anyone please help to change the font for superscript text(in glyph). For example, I want to change the superscript text  which is in "SF pro display" font and change to "Helvetica Neue". Is it possible with action manager code? Script Listener plugin was not working for photoshop 2021 and above in my mac. So please any action manager code available for this request please share.

 

This topic has been closed for replies.
Correct answer r-bin

Thanks r-bin!

 

I have modified the code as per my requirement and facing some issue is below:

 

var otbaseline = styles[i].getEnumerationValue(stringIDToTypeID("otbaseline"))

If I use the above line in the code for "SF Pro Display" font and got the below error 

 

 So I removed the line and change the code, now it change the font to superscript text but it convert the normal number to superscript. see the below modified code.

try { app.activeDocument.suspendHistory("Replace Superscript Font", "replace_superscript_font(activeDocument)"); } catch(e) { alert(e); }

alert("done");
function replace_superscript_font(target)
    {
    try {
        var layers = target.layers;

        for (var i = 0; i < layers.length; i++) 
            {
            if (layers[i].typename == "LayerSet") 
                {
                callee(layers[i]);
                }
            else if (layers[i].kind == LayerKind.TEXT)
                {
                    replace_styles(layers[i].id) 
                }
            }
        }
    catch (e) { throw(e); }        
    }

    


    
    //////////////////////////////////////////////////////
    function replace_styles(id) 
        {
        try {
                var fontPostScriptName = "HelveticaNeue-Bold";

            var dig1_reg = new RegExp("\\d+");
            var dig2_reg = new RegExp("[\u0030\u0031\u0032\u0033\u0034\u0035\u0036\u0037\u0038\u0039]");// same unicode values for Superscript and numbers in SF Pro Display Font
            var r = new ActionReference(); 
            r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("textKey"));
            r.putIdentifier(stringIDToTypeID("layer"), id); 
            var textKey = executeActionGet(r).getObjectValue(stringIDToTypeID("textKey"));
    
            var text = textKey.getString(stringIDToTypeID("textKey"));
            var list = textKey.getList(stringIDToTypeID("textStyleRange"));

            var styles = new Array();
            
            for (var i = 0; i < list.count; i++)
                {
                var d = list.getObjectValue(i);

                var from = d.getInteger(stringIDToTypeID("from"));

                var to = d.getInteger(stringIDToTypeID("to"));
            
                var style = d.getObjectValue(stringIDToTypeID("textStyle"));
    
                for (var x = from; x < to; x++) styles.push(style);

                }
    
            var new_text = "";
    
            for (var i = 0; i < text.length; i++)
                {
                var c = text[i];
    
                // change chars with only orig_font_name
                // uncomment if necessary 
                //var font = styles[i].getString(stringIDToTypeID("fontName"));  
                //if (font != orig_font_name) { new_text += c; continue; }
    
                var dig1 = dig1_reg.test(c);
                var dig2 = dig2_reg.test(c);
    
                if (dig1 || dig2)
                    {

                  // var otbaseline = styles[i].getEnumerationValue(stringIDToTypeID("otbaseline")); 
                   if (stringIDToTypeID("superScript")) c = conver_to_superscript(c);
    
                    styles[i].putString(stringIDToTypeID("fontPostScriptName"), fontPostScriptName);
    
                    // uncomment if necessary 
                    //styles[i].putString(stringIDToTypeID("fontName"),           fontName);
                    //styles[i].putString(stringIDToTypeID("fontStyleName"),      fontStyleName);
                    }
    
                new_text += c;
                }
    
            var new_list = new ActionList(); 
    
            styles.push(new ActionDescriptor());
    
            var from = 0;
    
            for (var i = 0; i < styles.length-1; i++)
                {
                if (!styles[i].isEqual(styles[i+1]))
                    {
                    var d = new ActionDescriptor(); 
                    d.putInteger(stringIDToTypeID("from"), from); 
                    d.putInteger(stringIDToTypeID("to"), i+1); 
                    d.putObject(stringIDToTypeID("textStyle"), stringIDToTypeID("textStyle"), styles[i]); 
    
                    new_list.putObject(stringIDToTypeID("textStyleRange"), d);
    
                    from = i+1;
                    }
                }
    
            textKey.putString(stringIDToTypeID("textKey"), new_text); 
    
            textKey.putList(stringIDToTypeID("textStyleRange"), new_list); 
    
            var d = new ActionDescriptor(); 
            var r = new ActionReference(); 
            r.putIdentifier(stringIDToTypeID("layer"), id); 
            d.putReference(stringIDToTypeID("null"), r); 
            d.putObject(stringIDToTypeID("to"), stringIDToTypeID("textLayer"), textKey); 
    
            executeAction(stringIDToTypeID("set"), d, DialogModes.NO); 
            }
        catch(e) { alert(e); }
        }
    
    
    //////////////////////////////////////////////////////
    function conver_to_superscript(c) 
        {
        try {
            switch (c)
                {
                case "1": c = "\u00B9"; break;
                case "2": c = "\u00B2"; break;
                case "3": c = "\u00B3"; break;
                case "0": c = "\u2070"; break;
                case "4": c = "\u2074"; break;
                case "5": c = "\u2075"; break;
                case "6": c = "\u2076"; break;
                case "7": c = "\u2077"; break;
                case "8": c = "\u2078"; break;
                case "9": c = "\u2079"; break;
                }
    
            return c;
            }
        catch(e) { throw(e); }
        }
    
    

 Sample and output file is attached.

 

Thanks


Your text is strange. There are duplicate ranges in the textStyleRange.
How did you get it, typed it with your hands?
 
Made two corrections in the code.
The first takes into account the presence of duplicate ranges.
The second is the absence of the otbaseline parameter.
 
By the way, it is the presence of otbaseline that makes ordinary number characters look like superscript in the SFProDisplay font. This is not the case in other fonts. I do not know what this parameter is.
 
Checking for the presence of this font was disabled (commented out) in the script. That is, all fonts will be subject to changes.

2 replies

Legend
July 22, 2021

So if I get this right...

you need to find superscript text in one font, then change it to a different font. Correct?

Because this is REALLY confusing, I'm not even sure wht you want to do.

Rocky@Author
Known Participant
July 23, 2021

Yes from starting itself I have pointed out the same thing. I want to find and change the superscript font(SF Pro Display) to different superscript font(Helvetica Neue). Im using SF pro display font, in the SF pro font numbers, superscript and subscript have same unicode value. For reference see the below thread: photoshop script to differentiate the same unicode characters

Kukurykus
Legend
July 23, 2021

Before moving to another request isn't an answer given in this thread a correct to mark it so?

Rocky@Author
Known Participant
July 18, 2021

From the below code, I can able to change the font,  but my requirement is to change the font name only and not the style. Is it possible to change the font name alone directly via javascript or it can possible through action manager code.

var doc = app.activeDocument;
replaceFont(doc);

function replaceFont(target)
{


    var layers = target.layers;
    for (var i = 0; i < layers.length; i++) {

        if (layers[i].typename == "LayerSet") {

            replaceFont(layers[i]);

        } else if (layers[i].typename == "ArtLayer" && layers[i].kind == LayerKind.TEXT) {
          //  alert(layers[i].textItem.fontStyle)
            if (layers[i].textItem.font.indexOf("SFProDisplay")>-1) {

                layers[i].textItem.font = "HelveticaNeue"

            }
        };
    };
};

  

Legend
July 18, 2021

Try like this

 

var doc = app.activeDocument;
replaceFont(doc);

function replaceFont(target)
{
    var layers = target.layers;
    for (var i = 0; i < layers.length; i++) {

        if (layers[i].typename == "LayerSet") {
            replaceFont(layers[i]);

        } else if (layers[i].typename == "ArtLayer" && layers[i].kind == LayerKind.TEXT) {
            if (layers[i].textItem.font.indexOf("SFProDisplay")>-1) {
                set_style(layers[i].id);
            }
        };
    };
};


function set_style(layer_id)
    {
    try {
        var d = new ActionDescriptor();
        var r = new ActionReference();
        r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("textStyle"));
        r.putIdentifier(stringIDToTypeID("textLayer"), layer_id);
        d.putReference(stringIDToTypeID("null"), r);
        var d1 = new ActionDescriptor();
        //d1.putInteger(stringIDToTypeID("textOverrideFeatureName"), 808465457); // don't know what it is, you can skip 
        //d1.putInteger(stringIDToTypeID("typeStyleOperationType"), 3);          // don't know what it is, you can skip 
        d1.putString(stringIDToTypeID("fontPostScriptName"), "HelveticaNeue");
        //d1.putString(stringIDToTypeID("fontName"), "Helvetica Neue");
        //d1.putString(stringIDToTypeID("fontStyleName"), "Regular");
        d1.putEnumerated(stringIDToTypeID("baseline"), stringIDToTypeID("baseline"), stringIDToTypeID("superScript"));
        d.putObject(stringIDToTypeID("to"), stringIDToTypeID("textStyle"), d1);
        executeAction(stringIDToTypeID("set"), d, DialogModes.NO);
        }
    catch (e) { throw(e); }
    }
Rocky@Author
Known Participant
July 18, 2021

Thanks r-bin!

 

Is it possible to change the superscript( in glyph text) font via photoshop action manager code?