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

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

Contributor ,
Jul 16, 2021 Jul 16, 2021

Copy link to clipboard

Copied

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.

 

TOPICS
Actions and scripting

Views

4.1K

Translate

Translate

Report

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

correct answers 1 Correct answer

People's Champ , Jul 25, 2021 Jul 25, 2021
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
...

Votes

Translate

Translate
Adobe
Contributor ,
Jul 18, 2021 Jul 18, 2021

Copy link to clipboard

Copied

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"

            }
        };
    };
};

  

Votes

Translate

Translate

Report

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
People's Champ ,
Jul 18, 2021 Jul 18, 2021

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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
Contributor ,
Jul 18, 2021 Jul 18, 2021

Copy link to clipboard

Copied

Thanks r-bin!

 

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

 

Votes

Translate

Translate

Report

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
People's Champ ,
Jul 18, 2021 Jul 18, 2021

Copy link to clipboard

Copied

Unfortunately, I am not familiar with the terminology used in fonts, as well as with fonts in general. I don't understand what "to change the superscript (in glyph text)" is.
 
Doesn't this script put the font in "superscript" mode???
 
Try to record your actions in Action and call it for each found layer, having previously selected it.

Votes

Translate

Translate

Report

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
Contributor ,
Jul 19, 2021 Jul 19, 2021

Copy link to clipboard

Copied

FYI, the psd files containing SF Pro fonts. I have script to extract the content from the psd files to excel. The SF Pro font containing same unicode for superscript ( ie glyph character) and numbers. So the superscript was not reflected in excel in the extracted content. It will treat as number. 

 

My requirements is simple to retain the superscript in the extracted content of excel. For that only I need the code to change the font to superscript and then extract the content. Is it possible?

Votes

Translate

Translate

Report

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
People's Champ ,
Jul 19, 2021 Jul 19, 2021

Copy link to clipboard

Copied

I didn’t understand (and I’ll hardly understand).
From what I understood (or so I think I understood), interesting to see how your script (is it a photoshop script?) extracts text to an Excel file. I can still imagine forming a csv text file. But there are no fonts in there as far as I know.

Votes

Translate

Translate

Report

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
Contributor ,
Jul 20, 2021 Jul 20, 2021

Copy link to clipboard

Copied

sorry for the confusion. I will explain my requirement in simple.

 

Screenshot 2021-07-20 at 10.07.29 PM.png

I want to find and change the superscript glyph character(0-9) with one unicode value of one font to another via action code. Is it possible?

Votes

Translate

Translate

Report

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
People's Champ ,
Jul 20, 2021 Jul 20, 2021

Copy link to clipboard

Copied

Maybe I finally figured out what you want : )

 

var font_postscript_name = "HelveticaNeue"; // use the exact PostScriptFontName 

var glyphs = "\u00B2\u00B3\u00B9\u2070\u2074\u2075\u2076\u2077\u2078\u2079";
var glyphs_length = glyphs.length;

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)
                {
                process(layers[i].id);
                }
            }
        }
    catch (e) { throw(e); }        
    }

function process(id)
    {
    try {
        var r = new ActionReference(); 
        r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("textKey"));
        r.putIdentifier(stringIDToTypeID("layer"), id); 
        var text = executeActionGet(r).getObjectValue(stringIDToTypeID("textKey")).getString(stringIDToTypeID("textKey"));

        for (var i = 0; i < text.length; i++)
            {
            var c = text.charCodeAt(i);
            for (var n = 0; n < glyphs_length; n++) if (glyphs.charCodeAt(n) == c) break; 

            if (n < glyphs_length)
                {
                set_text_style(id, i, 1, mdf); 
                }
            }        
        }
    catch (e) { throw(e); }        
    }

function mdf(d) 
    {
    try { 
        d.putString(stringIDToTypeID("fontPostScriptName"), font_postscript_name);
        d.erase(stringIDToTypeID("fontName"));
        }
    catch (e) { throw (e); }
    }

function set_text_style(id, from, len, style_modifier) 
    { 
    try { 
        var r = new ActionReference(); 
        r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("textKey"));
        r.putIdentifier(stringIDToTypeID("layer"), id); 
        var textKey = executeActionGet(r).getObjectValue(stringIDToTypeID("textKey"));

        var list = textKey.getList(stringIDToTypeID("textStyleRange"));
        var new_list = new ActionList(); 

        var styles = new Array();           

        var x = 0;

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

            var x0 = d.getInteger(stringIDToTypeID("from"));
            var x1 = d.getInteger(stringIDToTypeID("to"));

            var st = d.getObjectValue(stringIDToTypeID("textStyle"));

            for (var n = 0; n < x1-x0; n++)
                {
                if (x >= from && x < from+len) 
                    {
                    var d = new ActionDescriptor(); 
                    d.fromStream(st.toStream());
                    style_modifier(d);          
                    styles.push(d);
                    }
                else
                    {
                    styles.push(st);
                    }

                ++x;
                }
            }

        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.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) { throw(e); }
    } 

Votes

Translate

Translate

Report

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
Contributor ,
Jul 22, 2021 Jul 22, 2021

Copy link to clipboard

Copied

Thanks R-bin,

 

Its really awesome the code is working very well. But the code will only work for the same unicode of find and replace fonts. It will not work for different unicodes.

Votes

Translate

Translate

Report

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
People's Champ ,
Jul 22, 2021 Jul 22, 2021

Copy link to clipboard

Copied

Again, I did not understand you.
Unicode is a coding standard. What are "different unicodes"?
Give an example, it is better in a psd file, and not so that I myself have to invent the conditions of the problem without fully understanding it.

Votes

Translate

Translate

Report

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
Community Expert ,
Jul 22, 2021 Jul 22, 2021

Copy link to clipboard

Copied

Maybe it is time for you to explain what you want to achieve. 

Do you want to create some dialog/interface/… or how exactly do you want to handle »different unicodes«? 

Votes

Translate

Translate

Report

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
Contributor ,
Jul 22, 2021 Jul 22, 2021

Copy link to clipboard

Copied

For Example, Im using SF pro Display font. Please refer the below screenshot
The numeric 1 and superscript 1 have same unicode value,  I want to change the "Superscript 1 SF pro Display font" to "Superscript 1 helvetica neue font". Is it possible?

MicrosoftTeams-image (4).png

MicrosoftTeams-image (3).png

Votes

Translate

Translate

Report

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
People's Champ ,
Jul 22, 2021 Jul 22, 2021

Copy link to clipboard

Copied

Again, the task is not clearly defined. In the above example, character "1" is not a superscript character. Add the characters "0123456789" to the glyphs string and then all numeric characters will be superscripted.

Votes

Translate

Translate

Report

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
Contributor ,
Jul 22, 2021 Jul 22, 2021

Copy link to clipboard

Copied

Sorry for the confusion again.

 

Please ignore the screenshot. I want to replace the superscripts characters(0-9) in "SF Pro Display" font to "Helvetica Neue" font. Is it Possible?

 

Votes

Translate

Translate

Report

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
People's Champ ,
Jul 22, 2021 Jul 22, 2021

Copy link to clipboard

Copied

Although, you have something incomprehensible with this font.

Need to figure it out. Wait a bit.

Votes

Translate

Translate

Report

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
People's Champ ,
Jul 22, 2021 Jul 22, 2021

Copy link to clipboard

Copied

I need your file. One text layer is the initial text. The second is modified. It is advisable to attach your fonts, because I may not have them.

Votes

Translate

Translate

Report

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
Contributor ,
Jul 22, 2021 Jul 22, 2021

Copy link to clipboard

Copied

Please find the attached sample file and fonts for your reference in the below link:

Fonts&Testingfiles.zip

Votes

Translate

Translate

Report

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
People's Champ ,
Jul 22, 2021 Jul 22, 2021

Copy link to clipboard

Copied

Did not help. Firstly, there is no initial text and modified (with the HelveticaNeue font). Unfortunately, your font is not installed on me. Win7 says the font is wrong. You have not attached the HelveticaNeue font. For similar fonts that I downloaded from the Internet, it is impossible to manually translate the text from SF Pro to Helvetica. I didn't succeed.

 

P.S. Probably I can’t help.

Votes

Translate

Translate

Report

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
LEGEND ,
Jul 22, 2021 Jul 22, 2021

Copy link to clipboard

Copied

Even my patience ends following this thread 😕

Votes

Translate

Translate

Report

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
Contributor ,
Jul 22, 2021 Jul 22, 2021

Copy link to clipboard

Copied

Sorry Im using Mac.  The font I have sent is Mac font. No issue, Could you help me how to find and replace the one unicode value to another unicode value using action code, which is helpful for me.

Votes

Translate

Translate

Report

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
Contributor ,
Jul 22, 2021 Jul 22, 2021

Copy link to clipboard

Copied

PFA...

Votes

Translate

Translate

Report

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
People's Champ ,
Jul 22, 2021 Jul 22, 2021

Copy link to clipboard

Copied

Last try. For the active layer only. Check it out.
 
//var orig_font_name = "SF Pro Display";

var fontPostScriptName = "HelveticaNeue-Bold";
//var fontName           = "Helvetica Neue";
//var fontStyleName      = "Bold";

replace_styles(activeDocument.activeLayer.id) 

//////////////////////////////////////////////////////
function replace_styles(id) 
    {
    try {
        var dig1_reg = new RegExp("\\d");
        var dig2_reg = new RegExp("[\u00B2\u00B3\u00B9\u2070\u2074\u2075\u2076\u2077\u2078\u2079]");

        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 (dig1 && otbaseline == 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); }
    }

 
P.S. Everything is so complicated there that there are a million cases when it will not work.
 
upd. edited 1 time

Votes

Translate

Translate

Report

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
Contributor ,
Jul 22, 2021 Jul 22, 2021

Copy link to clipboard

Copied

Thanks r-bin!

 

You are really amazing thanks for your great support. shall I use this code to apply for all text layer in the file?

Votes

Translate

Translate

Report

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
People's Champ ,
Jul 22, 2021 Jul 22, 2021

Copy link to clipboard

Copied

For all layers, use the loop from the previous code.
Replace process(layers[i].id) there with replace_styles(layers[i].id)

Votes

Translate

Translate

Report

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