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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Is photoshop script to differentiate the same unicode characters a continuation of this thread?
Copy link to clipboard
Copied
Hi
Copy link to clipboard
Copied
?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Before moving to another request isn't an answer given in this thread a correct to mark it so?
Copy link to clipboard
Copied
I think people have written enough code for you. It's time you started doing your own work, instead of expecting people to do it for you.
Copy link to clipboard
Copied
A message to the helpers in this thread. Sorry the top of my head just came off and steam came out of my ears - from the original poster's appalling (to me) rudeness in starting a new thread without any reference to the huge amount of work already put into this one. I should not and do not seek to suggest anyone stop helping if that's what they want to do.
Copy link to clipboard
Copied
You have gotten way off into the weeds. Unicode value is irrelevant here. You are dealing with styling information.