Skip to main content
mijoop21729000
New Participant
February 6, 2019
Question

Random size for each character in a text

  • February 6, 2019
  • 2 replies
  • 3657 views

I have a text layer that says "something"

and I would like each character to have random size and baseline shift, but I cannot find

how to manipulate each character in a text layer.

It would be nice to use charAt[0] to change sizes of all the characters, yet

I can't seem to find a way to do so.

Please help.

This topic has been closed for replies.

2 replies

c.pfaffenbichler
Community Expert
February 7, 2019

// 2019, use it at your own risk;

#target  photoshop

if (app.documents.length > 0) {

//////////////////

try {

var theFonts = 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 theFrom = rangeList.getObjectValue(o).getInteger(stringIDToTypeID('from'));

var theTo = rangeList.getObjectValue(o).getInteger(stringIDToTypeID('to'));

var styleDesc = rangeList.getObjectValue(o).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]);

}

}

};

// change the size and baseline shift;

var theString = theText;

var theSize = theFonts[0][1];

var theBaselineShift = 100;

var theFont = theFonts[0][0];

var theIndex = 0;

var theNext = theFonts[0][3];

// =======================================================

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 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, theString );

            var list2 = new ActionList();

// define each letter individually;

for (var m = 0; m < theString.length; m++) {

// get font;

if (theNext == m) {

theIndex++;

var theFont = theFonts[theIndex][0];

var theNext = theFonts[theIndex][3];

};

// determine siue etc;

var thisSize = theSize / 2 +  theSize * Math.random();

var thisShift = theBaselineShift * (0.5 - Math.random());

                var desc14 = new ActionDescriptor();

                desc14.putInteger( idFrom, m );

                desc14.putInteger( idT, m+1 );

                    var desc15 = new ActionDescriptor();

                    desc15.putString( stringIDToTypeID( "fontPostScriptName" ), theFont );

// size;

                    desc15.putUnitDouble( charIDToTypeID( "Sz  " ), idPxl, thisSize );

                    desc15.putUnitDouble( stringIDToTypeID( "impliedFontSize" ), idPxl, thisSize );

// baseline shift:

                    desc15.putUnitDouble( charIDToTypeID( "Bsln" ), idPxl, thisShift );

                    desc15.putUnitDouble( stringIDToTypeID( "impliedBaselineShift" ), idPxl, thisShift );

                desc14.putObject( idTxtS, idTxtS, desc15 );

            list2.putObject( idTxtt, desc14 );

  };

        desc7.putList( idTxtt, list2 );

    desc6.putObject( idT, idTxLr, desc7 );

executeAction( idsetd, desc6, DialogModes.NO );

}

catch (e) {};

};

Inspiring
February 10, 2019

just in case you want to replace  the color too insert these lines :

            theColor = activeDocument.activeLayer.textItem.color

            theRed = Math.min(theColor.rgb.red * Math.random(), 255)

            theGrn = Math.min(theColor.rgb.green * Math.random(), 255)

            theBlue = Math.min(theColor.rgb.blue * Math.random(), 255)

            var d16 = new ActionDescriptor();

            d16.putDouble( s2t( "red" ), theRed );

            d16.putDouble( c2t( "Grn " ), theGrn );

            d16.putDouble( s2t( "blue" ), theBlue );

            d15.putObject( s2t( "color" ), s2t( "RGBColor" ), d16 );       

before the line

desc14.putObject( idTxtS, idTxtS, desc15 );

Brainiac
February 6, 2019