Skip to main content
May 30, 2019
Answered

Applying superscript to textItem

  • May 30, 2019
  • 1 reply
  • 1710 views

Hi,

I have developed a script that reads a csv file, and does stuff with the data it gets. So far working good. One issue I seem to be having though is trying to translate superscript tags into actual supserscript in photoshop.

Here is what I'm trying to illustrate:

String of text: "$10.00"

I would want the ".00" to be superscript. I don't really see any way to do this by looking at the documentation found here (https://www.adobe.com/content/dam/acom/en/devnet/photoshop/pdfs/photoshop-cc-javascript-ref-2019.pdf )

Can someone point me in the right direction on how I can achieve this? Here's some sample code that I am using to test:

layer.textItem.contents = "$10<sup>.00</sup>"

Obviously this wont work, but I am obviously trying to superscript the .00, I have dived as deep as I could into each property available on textItem. I would be open to splitting the string off, aplying one content, then applying another content with superscript. Which is what I was planning on doing, but can't quite seem to figure out how to get this style applied programmatically like this.

One thing I did try, is I applied a superscript to the layer's text to begin with, then before I mutate it, I alert(layer.textItem.content) and there's no special tag or anything, really hope there is a way to do this?

This topic has been closed for replies.
Correct answer pixxxelschubser

SuperMerlin​ was faster

// original code written by r-bin

// adapted for actuel requirements by pixxxelschubser

// activeLayer in demo example must be textlayer  !!

//activeDocument.activeLayer.textItem.contents = "$10.00";

var s = activeDocument.activeLayer.textItem.size;

var c = activeDocument.activeLayer.textItem.color;

var f = activeDocument.activeLayer.textItem.font;

var idx = activeDocument.activeLayer.textItem.contents.indexOf (".");

set_text_style (idx, 3, s, c, f);

function set_text_style(from, len, size, color, font)

{

try {

    var d = new ActionDescriptor();

    var r = new ActionReference();

    r.putEnumerated(stringIDToTypeID("textLayer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));

    d.putReference(stringIDToTypeID("null"), r);

    var d1 = new ActionDescriptor();

    var list1 = new ActionList();

    var d2 = new ActionDescriptor();

    d2.putInteger(stringIDToTypeID("from"), from);

    d2.putInteger(stringIDToTypeID("to"), from+len);

    var d3 = new ActionDescriptor();

    d3.putUnitDouble(stringIDToTypeID("size"), stringIDToTypeID("pointsUnit"), size);

    d3.putString(stringIDToTypeID("fontPostScriptName"), font);

    d3.putEnumerated(stringIDToTypeID("baseline"), stringIDToTypeID("baseline"), stringIDToTypeID("superScript"));

    var d4 = new ActionDescriptor();

    d4.putDouble(stringIDToTypeID("red"),   color.rgb.red);

    d4.putDouble(stringIDToTypeID("green"), color.rgb.green);

    d4.putDouble(stringIDToTypeID("blue"),  color.rgb.blue);

    d3.putObject(stringIDToTypeID("color"), stringIDToTypeID("RGBColor"), d4);

    d2.putObject(stringIDToTypeID("textStyle"), stringIDToTypeID("textStyle"), d3);

    list1.putObject(stringIDToTypeID("textStyleRange"), d2);

    d1.putList(stringIDToTypeID("textStyleRange"), list1);

    d.putObject(stringIDToTypeID("to"), stringIDToTypeID("textLayer"), d1);

    executeAction(stringIDToTypeID("set"), d, DialogModes.NO);

    }

catch (e) { throw(e); }

}

If that works for you

have fun

1 reply

SuperMerlin
Inspiring
May 31, 2019
pixxxelschubser
Community Expert
pixxxelschubserCommunity ExpertCorrect answer
Community Expert
May 31, 2019

SuperMerlin​ was faster

// original code written by r-bin

// adapted for actuel requirements by pixxxelschubser

// activeLayer in demo example must be textlayer  !!

//activeDocument.activeLayer.textItem.contents = "$10.00";

var s = activeDocument.activeLayer.textItem.size;

var c = activeDocument.activeLayer.textItem.color;

var f = activeDocument.activeLayer.textItem.font;

var idx = activeDocument.activeLayer.textItem.contents.indexOf (".");

set_text_style (idx, 3, s, c, f);

function set_text_style(from, len, size, color, font)

{

try {

    var d = new ActionDescriptor();

    var r = new ActionReference();

    r.putEnumerated(stringIDToTypeID("textLayer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));

    d.putReference(stringIDToTypeID("null"), r);

    var d1 = new ActionDescriptor();

    var list1 = new ActionList();

    var d2 = new ActionDescriptor();

    d2.putInteger(stringIDToTypeID("from"), from);

    d2.putInteger(stringIDToTypeID("to"), from+len);

    var d3 = new ActionDescriptor();

    d3.putUnitDouble(stringIDToTypeID("size"), stringIDToTypeID("pointsUnit"), size);

    d3.putString(stringIDToTypeID("fontPostScriptName"), font);

    d3.putEnumerated(stringIDToTypeID("baseline"), stringIDToTypeID("baseline"), stringIDToTypeID("superScript"));

    var d4 = new ActionDescriptor();

    d4.putDouble(stringIDToTypeID("red"),   color.rgb.red);

    d4.putDouble(stringIDToTypeID("green"), color.rgb.green);

    d4.putDouble(stringIDToTypeID("blue"),  color.rgb.blue);

    d3.putObject(stringIDToTypeID("color"), stringIDToTypeID("RGBColor"), d4);

    d2.putObject(stringIDToTypeID("textStyle"), stringIDToTypeID("textStyle"), d3);

    list1.putObject(stringIDToTypeID("textStyleRange"), d2);

    d1.putList(stringIDToTypeID("textStyleRange"), list1);

    d.putObject(stringIDToTypeID("to"), stringIDToTypeID("textLayer"), d1);

    executeAction(stringIDToTypeID("set"), d, DialogModes.NO);

    }

catch (e) { throw(e); }

}

If that works for you

have fun

Chuck Uebele
Community Expert
Community Expert
October 24, 2022

Hi @Chuck Uebele, Ya that's right. The position of the superscript will definitely vary. So wanted to understand if we can achieve it with substr() method to identify the "number 1" and make it a superscript. 


Yea, I think we can adapt the script to find the "1".