Skip to main content
Known Participant
January 16, 2024
解決済み

How to Apply Color to Specific Text in Photoshop scripts

  • January 16, 2024
  • 返信数 2.
  • 499 ビュー

Hi,

 

Please provide the script for applying the color in particular content.

 

FYI Screenshot:

 

 

Thanks.

 

このトピックへの返信は締め切られました。
解決に役立った回答 c.pfaffenbichler

Please provide a meaningful description of the task. 

 

Edit: 

// apply properties like color to letters of words in selected type layer;
// 2024, use it at your own risk;
if (app.documents.length > 0) {
    var theRegExp = /ipsum/gi;
	changeWordProperties (theRegExp)
};
//////////////////
////// change word properties //////
function changeWordProperties (theRegExp) {
var originalRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
//////////////////
try {
var theFonts = new Array;
var theStyleRanges = new Array;
var theStyleRanges2 = 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'));
// get indices for string;
var indicesCount = 0;
var theIndices = new Array;
while ((result = theRegExp.exec(theText))!=null) {
    theIndices.push([result.index, result.index+result[0].length])
};
//alert ("\n"+theIndices.join("\n"));
//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'));
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')));
theStyleRanges2.push(thisList.getObjectValue(stringIDToTypeID('textStyle')));
}
}
};
// the change arrays;
var theColors = [[255,0,0]];
// change text;
var idPxl = charIDToTypeID( "#Pxl" );
var idPnt = charIDToTypeID( "#Pnt" );
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, theText );
            var list2 = new ActionList();
//////////////////////////////////////////////////////
// define each letter individually;
var theIndex = 0;
var theRef = theStyleRanges[theIndex];
var theRef2 = theFonts[theIndex];
var theSize = theStyleRanges[theIndex].getUnitDoubleValue(charIDToTypeID( "Sz  " ));
var targetStart = theIndices[indicesCount][0];
var targetEnd = theIndices[indicesCount][1];
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 theSize = theStyleRanges[theIndex].getUnitDoubleValue(charIDToTypeID( "Sz  " ));
			};
		var desc14 = new ActionDescriptor();
		desc14.putInteger( idFrom, m );
		desc14.putInteger( idT, m+1 );
		var theStyle = theRef;
// the changes;
        if (m >= targetStart && m < targetEnd) {
// color;
            anIndex = Math.floor(Math.random() * (theColors.length));
            var desc21 = new ActionDescriptor();
            desc21.putDouble( charIDToTypeID( "Rd  " ), theColors[0][0] );
            desc21.putDouble( charIDToTypeID( "Grn " ), theColors[0][1]  );
            desc21.putDouble( charIDToTypeID( "Bl  " ), theColors[0][2]  );
            theStyle.putObject( charIDToTypeID( "Clr " ), charIDToTypeID( "RGBC" ), desc21 );
            desc14.putObject( idTxtS, idTxtS, theStyle);
        } else {
            desc14.putObject( idTxtS, idTxtS, theStyleRanges2[theIndex]);
            if (m == targetEnd && indicesCount < theIndices.length-1) {
                indicesCount++;
                var targetStart = theIndices[indicesCount][0];
                var targetEnd = theIndices[indicesCount][1];
            }
        };
        list2.putObject( charIDToTypeID( "Txtt" ),  desc14);
		};
//////////////////////////////////////////////////////
var list3 = new ActionList();
for (var n = 0; n < kernRange.count; n++) {
var thisOne = kernRange.getObjectValue(n);
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) {};
app.preferences.rulerUnits = originalRulerUnits;
};

返信数 2

c.pfaffenbichler
Community Expert
Community Expert
January 17, 2024

New Beginner1作成者
Known Participant
January 17, 2024

This script is useful, Thank you so much keep helping

c.pfaffenbichler
Community Expert
Community Expert
January 16, 2024

Please provide a meaningful description of the task. 

 

Edit: 

// apply properties like color to letters of words in selected type layer;
// 2024, use it at your own risk;
if (app.documents.length > 0) {
    var theRegExp = /ipsum/gi;
	changeWordProperties (theRegExp)
};
//////////////////
////// change word properties //////
function changeWordProperties (theRegExp) {
var originalRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
//////////////////
try {
var theFonts = new Array;
var theStyleRanges = new Array;
var theStyleRanges2 = 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'));
// get indices for string;
var indicesCount = 0;
var theIndices = new Array;
while ((result = theRegExp.exec(theText))!=null) {
    theIndices.push([result.index, result.index+result[0].length])
};
//alert ("\n"+theIndices.join("\n"));
//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'));
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')));
theStyleRanges2.push(thisList.getObjectValue(stringIDToTypeID('textStyle')));
}
}
};
// the change arrays;
var theColors = [[255,0,0]];
// change text;
var idPxl = charIDToTypeID( "#Pxl" );
var idPnt = charIDToTypeID( "#Pnt" );
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, theText );
            var list2 = new ActionList();
//////////////////////////////////////////////////////
// define each letter individually;
var theIndex = 0;
var theRef = theStyleRanges[theIndex];
var theRef2 = theFonts[theIndex];
var theSize = theStyleRanges[theIndex].getUnitDoubleValue(charIDToTypeID( "Sz  " ));
var targetStart = theIndices[indicesCount][0];
var targetEnd = theIndices[indicesCount][1];
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 theSize = theStyleRanges[theIndex].getUnitDoubleValue(charIDToTypeID( "Sz  " ));
			};
		var desc14 = new ActionDescriptor();
		desc14.putInteger( idFrom, m );
		desc14.putInteger( idT, m+1 );
		var theStyle = theRef;
// the changes;
        if (m >= targetStart && m < targetEnd) {
// color;
            anIndex = Math.floor(Math.random() * (theColors.length));
            var desc21 = new ActionDescriptor();
            desc21.putDouble( charIDToTypeID( "Rd  " ), theColors[0][0] );
            desc21.putDouble( charIDToTypeID( "Grn " ), theColors[0][1]  );
            desc21.putDouble( charIDToTypeID( "Bl  " ), theColors[0][2]  );
            theStyle.putObject( charIDToTypeID( "Clr " ), charIDToTypeID( "RGBC" ), desc21 );
            desc14.putObject( idTxtS, idTxtS, theStyle);
        } else {
            desc14.putObject( idTxtS, idTxtS, theStyleRanges2[theIndex]);
            if (m == targetEnd && indicesCount < theIndices.length-1) {
                indicesCount++;
                var targetStart = theIndices[indicesCount][0];
                var targetEnd = theIndices[indicesCount][1];
            }
        };
        list2.putObject( charIDToTypeID( "Txtt" ),  desc14);
		};
//////////////////////////////////////////////////////
var list3 = new ActionList();
for (var n = 0; n < kernRange.count; n++) {
var thisOne = kernRange.getObjectValue(n);
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) {};
app.preferences.rulerUnits = originalRulerUnits;
};