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

Photoshop script - make text character go superscript

Community Beginner ,
Sep 02, 2020 Sep 02, 2020

Copy link to clipboard

Copied

Hi, im trying to change text with photoshop script, but i was wonder is it possible to change characters in text with superscript. Like a date for example, change layer text and last two character st to made smaller with superscript January 1 st, thanks

jakerh48534187_0-1599041829346.png

 

TOPICS
Actions and scripting

Views

1.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

Community Expert , Sep 03, 2020 Sep 03, 2020

Now a version where the coloring is removed. 

// add superscript;
// 2020, use it at your own risk;
if (app.documents.length > 0) {
//////////////////
try {
var theFonts = new Array;
var theStyleRanges = new Array;
// get font of active layer;
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
var layerDesc = executeActionGet(ref);
var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSectio
...

Votes

Translate

Translate
Adobe
Community Expert ,
Sep 02, 2020 Sep 02, 2020

Copy link to clipboard

Copied

I just adapted an older Script to recolor letters to superscript the second and third letter of the selected Type Layer. 

Maybe it can provide a starting point for you. 

Screenshot 2020-09-02 at 14.59.00.pngScreenshot 2020-09-02 at 14.59.05.png

 

 

// apply colors from array to letters of selected type layer;
// add superscript;
// 2020, use it at your own risk;
if (app.documents.length > 0) {
//////////////////
try {
var theFonts = new Array;
var theStyleRanges = new Array;
// get font of active layer;
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
var layerDesc = executeActionGet(ref);
var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));
var isBackground = layerDesc.getBoolean(stringIDToTypeID("background"));
var theName = layerDesc.getString(stringIDToTypeID('name'));
// if not layer group collect values;
if (layerSet != "layerSectionEnd" && layerSet != "layerSectionStart" && isBackground != true) {
var hasText = layerDesc.hasKey(stringIDToTypeID("textKey"));
if (hasText == true) {
var textDesc = layerDesc.getObjectValue(stringIDToTypeID('textKey'));
var theText = textDesc.getString(stringIDToTypeID('textKey'));
//var shapeList = textDesc.getList(stringIDToTypeID('textShape'));
var paragraphRangeList = textDesc.getList(stringIDToTypeID('paragraphStyleRange'));
var kernRange = textDesc.getList(stringIDToTypeID('kerningRange'));
var rangeList = textDesc.getList(stringIDToTypeID('textStyleRange'));
// process the list;
for (var o = 0; o < rangeList.count; o++) {
var thisList = rangeList.getObjectValue(o);
var theFrom = thisList.getInteger(stringIDToTypeID('from'));
var theTo = thisList.getInteger(stringIDToTypeID('to'));
var styleDesc = thisList.getObjectValue(stringIDToTypeID('textStyle'));
//checkDesc2 (styleDesc);
var aSize = styleDesc.getUnitDoubleValue(charIDToTypeID( "Sz  " ));
// check for default font;
if (styleDesc.hasKey(stringIDToTypeID('fontPostScriptName')) == true) {var aFont = styleDesc.getString(stringIDToTypeID('fontPostScriptName'))}
else {
	var theDefault = styleDesc.getObjectValue(stringIDToTypeID('baseParentStyle'));	
	var aFont = theDefault.getString(stringIDToTypeID('fontPostScriptName'));
	};
if (styleDesc.hasKey(stringIDToTypeID('impliedFontSize')) == true) {var aFont = styleDesc.getString(stringIDToTypeID('fontPostScriptName'))}
// add to array;
theFonts.push([aFont, aSize, theFrom, theTo]);
theStyleRanges.push(thisList.getObjectValue(stringIDToTypeID('textStyle')));
}
}
};
// the color array;
var theColors = [[255,0,0], [255,255,0], [0,255,0],[0,255,255], [0,0,255], [255,0,255]];
var anIndex = 0;
var theColorNumber = theColors.length;
// change text;
// =======================================================
var idPxl = charIDToTypeID( "#Pxl" );
var idTxtt = charIDToTypeID( "Txtt" );
var idFrom = charIDToTypeID( "From" );
var idT = charIDToTypeID( "T   " );
var idTxtS = charIDToTypeID( "TxtS" );
var idTxLr = charIDToTypeID( "TxLr" );
var idTxt = charIDToTypeID( "Txt " );
var idsetd = charIDToTypeID( "setd" );
var idBaseline = stringIDToTypeID( "baseline" );
    var desc6 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref1 = new ActionReference();
        ref1.putEnumerated( idTxLr, charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt"  ));
    desc6.putReference( idnull, ref1 );
        var desc7 = new ActionDescriptor();
        desc7.putString( idTxt, theText );
            var list2 = new ActionList();
//////////////////
// define each letter individually;
var theIndex = 0;
var theRef = theStyleRanges[theIndex];
var theRef2 = theFonts[theIndex];
for (var m = 0; m < theText.length; m++) {
// check for relevant style range;
		if (m == theRef2[3]) {
			theIndex++;
			var theRef = theStyleRanges[theIndex];
			var theRef2 = theFonts[theIndex];
			};
		var desc14 = new ActionDescriptor();
		desc14.putInteger( idFrom, m );
		desc14.putInteger( idT, m+1 );
		var theStyle = theRef;
		var desc21 = new ActionDescriptor();
		desc21.putDouble( charIDToTypeID( "Rd  " ), theColors[anIndex][0] );
		desc21.putDouble( charIDToTypeID( "Grn " ), theColors[anIndex][1]  );
		desc21.putDouble( charIDToTypeID( "Bl  " ), theColors[anIndex][2]  );
        theStyle.putObject( charIDToTypeID( "Clr " ), charIDToTypeID( "RGBC" ), desc21 );
// superscript;
        if (m == 1 || m == 2) {
            theStyle.putEnumerated( idBaseline, idBaseline, stringIDToTypeID( "superScript" ));
        }
        else {
            theStyle.putEnumerated( idBaseline, idBaseline, stringIDToTypeID( "normal" ));
        };
		desc14.putObject( stringIDToTypeID('textStyle'), stringIDToTypeID('textStyle'), theStyle);
		list2.putObject( charIDToTypeID( "Txtt" ),  desc14);
anIndex++;
if (theText[m].match(/\s/)!=null) {anIndex--};
if (anIndex == theColorNumber) {anIndex = 0};
		};
//////////////////
var list3 = new ActionList();
for (var n = 0; n < kernRange.count; n++) {
var thisOne = kernRange.getObjectValue(n);
$.writeln (n+"___"+thisOne.getInteger(charIDToTypeID("From"))+"___"+thisOne.getInteger(charIDToTypeID( "T   " ))+"___"+thisOne.getInteger(stringIDToTypeID("kerning")))
var desc15 = new ActionDescriptor();
desc15.putInteger( idFrom, thisOne.getInteger(charIDToTypeID("From")) );
desc15.putInteger( idT, thisOne.getInteger(charIDToTypeID( "T   " )) );
desc15.putInteger( charIDToTypeID( "Krng" ), thisOne.getInteger(stringIDToTypeID("kerning")));
list3.putObject( stringIDToTypeID( "kerningRange"),  desc15);
};
//////////////////
        desc7.putList( idTxtt, list2 );
        desc7.putList( stringIDToTypeID( "kerningRange"), list3);
    desc6.putObject( idT, idTxLr, desc7 );
executeAction( idsetd, desc6, DialogModes.NO );
}
catch (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
Community Beginner ,
Sep 02, 2020 Sep 02, 2020

Copy link to clipboard

Copied

thanks, i will try what i can do with this, its a huge help, i will post results

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 ,
Sep 03, 2020 Sep 03, 2020

Copy link to clipboard

Copied

Now a version where the coloring is removed. 

// add superscript;
// 2020, use it at your own risk;
if (app.documents.length > 0) {
//////////////////
try {
var theFonts = new Array;
var theStyleRanges = new Array;
// get font of active layer;
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
var layerDesc = executeActionGet(ref);
var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));
var isBackground = layerDesc.getBoolean(stringIDToTypeID("background"));
var theName = layerDesc.getString(stringIDToTypeID('name'));
// if not layer group collect values;
if (layerSet != "layerSectionEnd" && layerSet != "layerSectionStart" && isBackground != true) {
var hasText = layerDesc.hasKey(stringIDToTypeID("textKey"));
if (hasText == true) {
var textDesc = layerDesc.getObjectValue(stringIDToTypeID('textKey'));
var theText = textDesc.getString(stringIDToTypeID('textKey'));
var paragraphRangeList = textDesc.getList(stringIDToTypeID('paragraphStyleRange'));
var kernRange = textDesc.getList(stringIDToTypeID('kerningRange'));
var rangeList = textDesc.getList(stringIDToTypeID('textStyleRange'));
// process the list;
for (var o = 0; o < rangeList.count; o++) {
var thisList = rangeList.getObjectValue(o);
var theFrom = thisList.getInteger(stringIDToTypeID('from'));
var theTo = thisList.getInteger(stringIDToTypeID('to'));
var styleDesc = thisList.getObjectValue(stringIDToTypeID('textStyle'));
var aSize = styleDesc.getUnitDoubleValue(charIDToTypeID( "Sz  " ));
// check for default font;
if (styleDesc.hasKey(stringIDToTypeID('fontPostScriptName')) == true) {var aFont = styleDesc.getString(stringIDToTypeID('fontPostScriptName'))}
else {
	var theDefault = styleDesc.getObjectValue(stringIDToTypeID('baseParentStyle'));	
	var aFont = theDefault.getString(stringIDToTypeID('fontPostScriptName'));
	};
if (styleDesc.hasKey(stringIDToTypeID('impliedFontSize')) == true) {var aFont = styleDesc.getString(stringIDToTypeID('fontPostScriptName'))}
// add to array;
theFonts.push([aFont, aSize, theFrom, theTo]);
theStyleRanges.push(thisList.getObjectValue(stringIDToTypeID('textStyle')));
}
}
};
// change text;
// =======================================================
var idPxl = charIDToTypeID( "#Pxl" );
var idTxtt = charIDToTypeID( "Txtt" );
var idFrom = charIDToTypeID( "From" );
var idT = charIDToTypeID( "T   " );
var idTxtS = charIDToTypeID( "TxtS" );
var idTxLr = charIDToTypeID( "TxLr" );
var idTxt = charIDToTypeID( "Txt " );
var idsetd = charIDToTypeID( "setd" );
var idBaseline = stringIDToTypeID( "baseline" );
var idTextStyle = stringIDToTypeID('textStyle');
    var desc6 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref1 = new ActionReference();
        ref1.putEnumerated( idTxLr, charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt"  ));
    desc6.putReference( idnull, ref1 );
        var desc7 = new ActionDescriptor();
        desc7.putString( idTxt, theText );
            var list2 = new ActionList();
//////////////////
// define each letter individually;
var theIndex = 0;
var theRef = theStyleRanges[theIndex];
var theRef2 = theFonts[theIndex];
for (var m = 0; m < theText.length; m++) {
// check for relevant style range;
		if (m == theRef2[3]) {
			theIndex++;
			var theRef = theStyleRanges[theIndex];
			var theRef2 = theFonts[theIndex];
			};
		var desc14 = new ActionDescriptor();
		desc14.putInteger( idFrom, m );
		desc14.putInteger( idT, m+1 );
		var theStyle = theRef;
// superscript;
        if (m == 1 || m == 5) {
            theStyle.putEnumerated( idBaseline, idBaseline, stringIDToTypeID( "superScript" ));
        }
        else {
            theStyle.putEnumerated( idBaseline, idBaseline, stringIDToTypeID( "normal" ));
        };
		desc14.putObject( idTextStyle, idTextStyle, theStyle);
		list2.putObject( charIDToTypeID( "Txtt" ),  desc14);
		};
//////////////////
var list3 = new ActionList();
for (var n = 0; n < kernRange.count; n++) {
var thisOne = kernRange.getObjectValue(n);
$.writeln (n+"___"+thisOne.getInteger(charIDToTypeID("From"))+"___"+thisOne.getInteger(charIDToTypeID( "T   " ))+"___"+thisOne.getInteger(stringIDToTypeID("kerning")))
var desc15 = new ActionDescriptor();
desc15.putInteger( idFrom, thisOne.getInteger(charIDToTypeID("From")) );
desc15.putInteger( idT, thisOne.getInteger(charIDToTypeID( "T   " )) );
desc15.putInteger( charIDToTypeID( "Krng" ), thisOne.getInteger(stringIDToTypeID("kerning")));
list3.putObject( stringIDToTypeID( "kerningRange"),  desc15);
};
//////////////////
        desc7.putList( idTxtt, list2 );
        desc7.putList( stringIDToTypeID( "kerningRange"), list3);
    desc6.putObject( idT, idTxLr, desc7 );
executeAction( idsetd, desc6, DialogModes.NO );
}
catch (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
Community Beginner ,
Sep 03, 2020 Sep 03, 2020

Copy link to clipboard

Copied

LATEST

niceee, thanks bro, now it works like a charm 😄 thanks, i just changed

 

// superscript;
        if (m == 1 || m == 5) {

to

// superscript;
if ( (theText.length-1) == m || m == (theText.length-2)) {

to not superscript character number 1 and 5 because i need last two characters.

 

THANKS

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