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.
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"));
////////////modification 1
for (var x = from; x < to; x++) styles[x] = 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)
{
////////////modification 2
var otbaseline = undefined;
if (styles[i].hasKey(stringIDToTypeID("otbaseline"))) otbaseline = styles[i].getEnumerationValue(stringIDToTypeID("otbaseline"));
///////////////////////////////////////////////
if (dig1 && otbaseline == stringIDToTypeID("superScript")) c = convert_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) { throw(e); }
}
//////////////////////////////////////////////////////
function convert_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.
if (stringIDToTypeID("superScript")) c = conver_to_superscript(c);
This is the "new" code you typed. You can't do that. The condition is always true.