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
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"
}
};
};
};
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); }
}
Copy link to clipboard
Copied
Thanks r-bin!
Is it possible to change the superscript( in glyph text) font via photoshop action manager code?
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
Copy link to clipboard
Copied
sorry for the confusion. I will explain my requirement in simple.
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?
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); }
}
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.
Copy link to clipboard
Copied
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«?
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?
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.
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?
Copy link to clipboard
Copied
Although, you have something incomprehensible with this font.
Need to figure it out. Wait a bit.
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.
Copy link to clipboard
Copied
Please find the attached sample file and fonts for your reference in the below link:
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.
Copy link to clipboard
Copied
Even my patience ends following this thread 😕
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.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
//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); }
}
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?
Copy link to clipboard
Copied