Copy link to clipboard
Copied
I want to select all the text layers by font type in a particular Artboard. Artboard needs to be identified by its dimension.
I have a script to select all the artboards in an active document. Could anyone help me modify this?
var activeDoc = app.activeDocument; var artboards = activeDoc.layers; var visibleArtboards = []; for (var i = 0; i < artboards.length; i++) { if (artboards[i].visible) { // select ID as we go along var layerID = artboards[i].id; visibleArtboards.push(layerID); } } // Loop over the layers again and select all the art boards for (var i = 0; i < visibleArtboards.length; i++) { // add to current selection of layers select_by_ID(visibleArtboards[i], true); } function select_by_ID(id, add) { if (add == undefined) add = false; var desc1 = new ActionDescriptor(); var ref1 = new ActionReference(); ref1.putIdentifier(charIDToTypeID('Lyr '), id); desc1.putReference(charIDToTypeID('null'), ref1); if (add) desc1.putEnumerated(stringIDToTypeID("selectionModifier"), stringIDToTypeID("selectionModifierType"), stringIDToTypeID("addToSelection")); executeAction(charIDToTypeID('slct'), desc1, DialogModes.NO); } // end of selectByID()
This script should keep the layers visibility the same.
#target photoshop
var doc = app.activeDocument;
var oldPref = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var ab, aLayer, aW, aH
var defaultValue = '';
var desktopHead = prompt("Paste your new headline", defaultValue);
var docLayers = doc.layers.length; //getting top layers because artboards are top layers
for(var i=0;i<docLayers;i++){
aLayer = doc.layers[i];
ab = artboard_rectangle(aLayer);
aW =
...
Here's the script to change "discount" to another color. I don't know about changing the 1 to super script. I would need the code for that as well.
#target photoshop
var testText = 'no no'
var doc = app.activeDocument;
var oldPref = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var ab, aLayer, aW, aH
var defaultValue = '';
var desktopHead = prompt("Paste your new headline", defaultValue);
var docLayers = doc.layers.length; //getting top layers because artboards are
...
Okay, I think I have the script you want. Thanks to @r-bin and @pixxxelschubser for the code to change the color and superscript of text items.
#target photoshop
// some code written by r-bin
// other code adapted for actuel requirements by pixxxelschubser
// final code by Chuck Uebele
var doc = app.activeDocument;
var oldPref = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var useSuperscript = false;
var ab, aLayer, aW, aH
var defaultValue = '';
var desktopHead = p
...
Copy link to clipboard
Copied
I'll have to see about that. I know there is a way to change text within a text block, but I'm not sure how to do it.
Copy link to clipboard
Copied
Thank you, Chuck. I am looking forward to it.
Copy link to clipboard
Copied
I tried looking at this, but I. Thinks it's beyond my skill level to figure this out.
Copy link to clipboard
Copied
Hi Chuck,
I really appreciate it. Thanks for trying. I figured out that substr() method returns a portion of the string. And I found this script and modified it a bit. Can you please help in incorporating this in the previous script?
Copy link to clipboard
Copied
Let me look at it a bit and see if I can.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Ok, I'll try and look at it this evening, morning where I am, now.
Copy link to clipboard
Copied
Thank you, Chuck. You are the best.
Copy link to clipboard
Copied
I would start a new post and just ask about how to script changing only some text in a text block. Once that is figured out, then the code can be incorporated into the script on this post.
Copy link to clipboard
Copied
Here's the script to change "discount" to another color. I don't know about changing the 1 to super script. I would need the code for that as well.
#target photoshop
var testText = 'no no'
var doc = app.activeDocument;
var oldPref = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var ab, aLayer, aW, aH
var defaultValue = '';
var desktopHead = prompt("Paste your new headline", defaultValue);
var docLayers = doc.layers.length; //getting top layers because artboards are top layers
var color1 = new SolidColor();
with (color1.rgb) { red = 82; green = 176; blue = 52; }
for(var i=0;i<docLayers;i++){
aLayer = doc.layers[i];
ab = artboard_rectangle(aLayer);
aW = ab[2]-ab[0];
aH = ab[3]-ab[1];
if((aW == 1000 && aH == 1000) || (aW == 400 && aH == 500)){//add more sizes here
findTextLayer (aLayer)
}//end if for artboard size
}//end for loop
app.preferences.rulerUnits = oldPref;
function findTextLayer(pLayer){
var layLen = pLayer.layers.length;
for(var j = 0;j<layLen;j++){
var layerRef = pLayer.layers[j];
if(pLayer.layers[j].typename == 'LayerSet'){
findTextLayer (pLayer.layers[j]);
}//end if for finding layerset
else{
if (layerRef.kind === LayerKind.TEXT) {
if(layerRef.textItem.font === "Arial-BoldMT"){//make sure you have correct font name
var layVis = layerRef.visible
doc.activeLayer = layerRef
layerRef.textItem.contents = desktopHead;
changeColor ('discount', color1, layerRef)
layerRef.visible = layVis
}//end if for text font
}//end if for layerkind text
}//end else
};//end for loop
};//end function
function artboard_rectangle(layer)
{
try {
var r = new ActionReference();
r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("artboard"));
if (layer) r.putIdentifier(stringIDToTypeID("layer"), layer.id);
else r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
var d = executeActionGet(r).getObjectValue(stringIDToTypeID("artboard")).getObjectValue(stringIDToTypeID("artboardRect"));
var bounds = new Array();
bounds[0] = d.getUnitDoubleValue(stringIDToTypeID("left"));
bounds[1] = d.getUnitDoubleValue(stringIDToTypeID("top"));
bounds[2] = d.getUnitDoubleValue(stringIDToTypeID("right"));
bounds[3] = d.getUnitDoubleValue(stringIDToTypeID("bottom"));
return bounds;
}
catch(e) { alert(e); }
}
function changeColor(substr, useColor, useLayer){
try {
//var size = activeDocument.activeLayer.textItem.size;
var size = useLayer.textItem.size;
//var fpsname = activeDocument.activeLayer.textItem.font;
var fpsname = useLayer.textItem.font;
//var txt = activeDocument.activeLayer.textItem.contents;
var txt = useLayer.textItem.contents;
$.writeln(doc.activeLayer.name)
if (substr) {
substr = substr.toLowerCase();
var idx = [];
var i = -1;
while (1) {
i = txt.indexOf(substr, i + 1);
if (i < 0) break;
idx.push(i);
}
var len = substr.length;
for (var i = 0; i < idx.length; i++)
set_text_style(idx[i], substr.length, size, useColor, fpsname)
}
function set_text_style(from, len, size, color, fname) {
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);
if (size != undefined) {
var d3 = new ActionDescriptor();
d3.putUnitDouble(stringIDToTypeID("size"), stringIDToTypeID("pointsUnit"), size);
}
if (color != undefined) {
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);
}
if (fname != undefined) {
d3.putString(stringIDToTypeID("fontPostScriptName"), fname);
}
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); }
}
} catch (e) { alert(e); }
}//end main function
Copy link to clipboard
Copied
Thank you so much for the great work, Chuck. This is working as expected.
I need one more clarification on this. Say my input is "Discount 10%" and it should turn the entire phrase (Discount 10%) to green which is currently possible. But I would like to make the "10%" a variable that might range from 10% to 80%. I understand that we can achieve it using multiple "changeColor" function calls(that might be a lot). Is there a way to input it in an easier & simple way?
Copy link to clipboard
Copied
I think there might be. Maybe add what you want to turn green to the dialog box.
Copy link to clipboard
Copied
Sorry, I dint get that chuck.
Copy link to clipboard
Copied
Okay, I think I have the script you want. Thanks to @r-bin and @pixxxelschubser for the code to change the color and superscript of text items.
#target photoshop
// some code written by r-bin
// other code adapted for actuel requirements by pixxxelschubser
// final code by Chuck Uebele
var doc = app.activeDocument;
var oldPref = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var useSuperscript = false;
var ab, aLayer, aW, aH
var defaultValue = '';
var desktopHead = prompt("Paste your new headline", defaultValue);
var docLayers = doc.layers.length; //getting top layers because artboards are top layers
var color1 = new SolidColor();
with (color1.rgb) { red = 82; green = 176; blue = 52; }
for(var i=0;i<docLayers;i++){
aLayer = doc.layers[i];
ab = artboard_rectangle(aLayer);
aW = ab[2]-ab[0];
aH = ab[3]-ab[1];
if((aW == 1000 && aH == 1000) || (aW == 400 && aH == 500)){//add more sizes here
findTextLayer (aLayer)
}//end if for artboard size
}//end for loop
app.preferences.rulerUnits = oldPref;
function findTextLayer(pLayer){
var layLen = pLayer.layers.length;
for(var j = 0;j<layLen;j++){
var layerRef = pLayer.layers[j];
if(pLayer.layers[j].typename == 'LayerSet'){
findTextLayer (pLayer.layers[j]);
}//end if for finding layerset
else{
if (layerRef.kind === LayerKind.TEXT) {
if(layerRef.textItem.font === "Arial-BoldMT"){//make sure you have correct font name
var layVis = layerRef.visible
doc.activeLayer = layerRef
layerRef.textItem.contents = desktopHead;
var s = activeDocument.activeLayer.textItem.size;
var c = activeDocument.activeLayer.textItem.color;
var f = activeDocument.activeLayer.textItem.font;
var disIdx = layerRef.textItem.contents.indexOf ('discount');
var perIdx = layerRef.textItem.contents.indexOf ('%');
var oneIdx = layerRef.textItem.contents.indexOf ('1');
if(disIdx != -1 && perIdx != -1){
var strLen = perIdx-disIdx+1
setSuperscript (disIdx, strLen, s, color1, f, false)
}
else{alert("a layer doesn't doesn't contain 'discount or %")}
if(oneIdx != -1){
setSuperscript (oneIdx, 1, s, c, f, true)
}
//changeColor ('discount', color1, layerRef)
layerRef.visible = layVis
}//end if for text font
}//end if for layerkind text
}//end else
};//end for loop
};//end function
function artboard_rectangle(layer)
{
try {
var r = new ActionReference();
r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("artboard"));
if (layer) r.putIdentifier(stringIDToTypeID("layer"), layer.id);
else r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
var d = executeActionGet(r).getObjectValue(stringIDToTypeID("artboard")).getObjectValue(stringIDToTypeID("artboardRect"));
var bounds = new Array();
bounds[0] = d.getUnitDoubleValue(stringIDToTypeID("left"));
bounds[1] = d.getUnitDoubleValue(stringIDToTypeID("top"));
bounds[2] = d.getUnitDoubleValue(stringIDToTypeID("right"));
bounds[3] = d.getUnitDoubleValue(stringIDToTypeID("bottom"));
return bounds;
}
catch(e) { alert(e); }
}
function setSuperscript (from, len, size, color, font, superS){
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);
if(superS){
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); }
}
Copy link to clipboard
Copied
Thanks again @Chuck Uebele. I am actually facing an issue with this script. It turns the color of the superscript to green.
Copy link to clipboard
Copied
Yea, I was wondering if you wanted it green or black. Easy to change to black.
Copy link to clipboard
Copied
I have a question: is there only one discount word and discount percentage per text layer? Right now, the script will only find the last instance of it.
Copy link to clipboard
Copied
@Vibi Dev I edited the last script that I posted, so now the superscript 1 will be the same color as the original text.