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

Selecting a text layer in a particular artboard by Artboard size using Java script?

Participant ,
Oct 02, 2022 Oct 02, 2022

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()

 

TOPICS
Actions and scripting
1.6K
Translate
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 3 Correct answers

Community Expert , Oct 06, 2022 Oct 06, 2022

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 = 
...
Translate
Community Expert , Oct 23, 2022 Oct 23, 2022

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 
...
Translate
Community Expert , Oct 24, 2022 Oct 24, 2022

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
...
Translate
Adobe
Community Expert ,
Oct 22, 2022 Oct 22, 2022

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.

Translate
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
Participant ,
Oct 22, 2022 Oct 22, 2022

Thank you, Chuck. I am looking forward to it. 

Translate
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 ,
Oct 23, 2022 Oct 23, 2022

I tried looking at this, but I. Thinks it's beyond my skill level to figure this out.

Translate
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
Participant ,
Oct 23, 2022 Oct 23, 2022

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? 


try {

var c = new SolidColor();
with (c.rgb) { red = 204; green = 0; blue = 0; }


var txt = activeDocument.activeLayer.textItem.contents;

var substr = "Discount";

if (substr) {

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(c)
}

function set_text_style(color) {
try {
var d = new ActionDescriptor();
var r = new ActionReference();

r.putEnumerated(stringIDToTypeID("textLayer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
d.putReference(stringIDToTypeID("null"), r);
var d;
var d2;

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);
d2.putObject(stringIDToTypeID("color"), stringIDToTypeID("RGBColor"), d4);


d2.putObject(stringIDToTypeID("textStyle"), stringIDToTypeID("textStyle"), d2);
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); }

 

Translate
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 ,
Oct 23, 2022 Oct 23, 2022

Let me look at it a bit and see if I can.

Translate
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
Participant ,
Oct 23, 2022 Oct 23, 2022
Thanks, Chuck. Please try this script(Ignore the previous one). It turns the word "discount" to green in any text layer.

try
{

var c = new SolidColor();
with (c.rgb) { red = 82; green = 176; blue = 52; }

var size = activeDocument.activeLayer.textItem.size;

var fpsname = activeDocument.activeLayer.textItem.font;

var txt = activeDocument.activeLayer.textItem.contents;

var substr = "discount";

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, c, 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); }

 

Translate
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 ,
Oct 23, 2022 Oct 23, 2022

Ok, I'll try and look at it this evening, morning where I am, now.

Translate
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
Participant ,
Oct 23, 2022 Oct 23, 2022

Thank you, Chuck. You are the best. 

Translate
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 ,
Oct 23, 2022 Oct 23, 2022

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.

Translate
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 ,
Oct 23, 2022 Oct 23, 2022

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
Translate
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
Participant ,
Oct 24, 2022 Oct 24, 2022

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?  

Translate
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 ,
Oct 24, 2022 Oct 24, 2022

I think there might be. Maybe add what you want to turn green to the dialog box.

Translate
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
Participant ,
Oct 24, 2022 Oct 24, 2022

Sorry, I dint get that chuck. 

Translate
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 ,
Oct 24, 2022 Oct 24, 2022

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); }
}

 

Translate
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
Participant ,
Oct 26, 2022 Oct 26, 2022

Thanks again @Chuck Uebele. I am actually facing an issue with this script. It turns the color of the superscript to green. 

Translate
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 ,
Oct 26, 2022 Oct 26, 2022

Yea, I was wondering if you wanted it green or black. Easy to change to black.

Translate
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 ,
Oct 26, 2022 Oct 26, 2022

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.

Translate
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 ,
Oct 26, 2022 Oct 26, 2022
LATEST

@Vibi Dev I edited the last script that I posted, so now the superscript 1 will be the same color as the original text.

Translate
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