Skip to main content
Inspiring
October 2, 2022
Answered

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

  • October 2, 2022
  • 8 replies
  • 2248 views

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 topic has been closed for replies.
Correct answer Chuck Uebele

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

 

8 replies

Chuck Uebele
Chuck UebeleCorrect answer
Adobe Expert
October 25, 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); }
}

 

Vibi DevAuthor
Inspiring
October 26, 2022

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

Chuck Uebele
Adobe Expert
October 26, 2022

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

Chuck Uebele
Adobe Expert
October 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
Vibi DevAuthor
Inspiring
October 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?  

Chuck Uebele
Adobe Expert
October 24, 2022

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

Chuck Uebele
Adobe Expert
October 4, 2022

I made a slight change to the script. I put the artboard dimensions into single variables, so that the if statement, listing the sizes, isn't so long and will be easier to add more sizes.

#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++){
    doc.activeLayer = doc.layers[i];
    aLayer = doc.activeLayer
    ab = artboard_rectangle(doc.activeLayer);
    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++){
        doc.activeLayer = pLayer.layers[j];
        var layerRef = doc.activeLayer
        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
                    layerRef.textItem.contents = desktopHead;
                    }//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); }
    }

Vibi DevAuthor
Inspiring
October 6, 2022

This is awesome Chuck. This is working great. This script also turns all the inactive layers in the current artboard into active ones (layer Visibility off --> on). Do you know why that is happening? Thanks in advance. 

Vibi DevAuthor
Inspiring
October 6, 2022

It actually turns on all the inactive layers above the text layer in the artboard to be precise. 

Chuck Uebele
Adobe Expert
October 3, 2022

One more time. This will change the text layer within a group. To add more sizes, you have to add them to the if statement that is commented about adding sizes. The different size should be separated by the double pipe symbol ||, which stands for "or". The different sizes should be set in parentheses.

 

#target photoshop

var doc = app.activeDocument;
var oldPref = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;

var ab, aLayer

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++){
    doc.activeLayer = doc.layers[i];
    aLayer = doc.activeLayer
    ab = artboard_rectangle(doc.activeLayer);
    if(((ab[2]-ab[0]) == 1000 && (ab[3]-ab[1]) == 1000) || ((ab[2]-ab[0]) == 400 && (ab[3]-ab[1]) == 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++){
        doc.activeLayer = pLayer.layers[j];
        var layerRef = doc.activeLayer
        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
                    layerRef.textItem.contents = desktopHead;
                    }//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); }
    }

Chuck Uebele
Adobe Expert
October 3, 2022

Okay, try this. You need to know the artboard size to put into the UI. You also need to have the correct name for the font. I had to change your Arial-Bold to Arial-BoldMT.

 

#target photoshop

var doc = app.activeDocument;

var aWidth = 0
var aHeight = 0
var desktopHead = '';
var ab

var dlg = new Window('dialog','Replace Headline');
    dlg.textGP = dlg.add('group');
        dlg.textGP.orientation = 'row';
        dlg.textGP.alignment = ['right','top'];
        dlg.textGP.alignChildren = ['right','top'];
        dlg.textGP.sTxt = dlg.textGP.add('statictext',undefined,'Paste your new headline')
        dlg.textGP.eTxt = dlg.textGP.add('edittext',undefined,'')
            dlg.textGP.eTxt.size = [500,21];    
    dlg.widthGP = dlg.add('group');
        dlg.widthGP.orientation = 'row';
        dlg.widthGP.alignment = ['right','top'];
        dlg.widthGP.alignChildren = ['right','top'];
        dlg.widthGP.sTxt = dlg.widthGP.add('statictext',undefined,'Enter width of artboard')
        dlg.widthGP.eTxt = dlg.widthGP.add('edittext',undefined,'')
            dlg.widthGP.eTxt.size = [500,21];              
    dlg.heightGP = dlg.add('group');
        dlg.heightGP.orientation = 'row';
        dlg.heightGP.alignment = ['right','top'];
        dlg.heightGP.alignChildren = ['right','top'];
        dlg.heightGP.sTxt = dlg.heightGP.add('statictext',undefined,'Enter width of artboard')
        dlg.heightGP.eTxt = dlg.heightGP.add('edittext',undefined,'')
            dlg.heightGP.eTxt.size = [500,21];      
    dlg.btnGp = dlg.add('group');
        dlg.btnGp.orientation = 'row';
        dlg.btnGp.ok = dlg.btnGp.add('button',undefined,'Okay');
        dlg.btnGp.cancel = dlg.btnGp.add('button',undefined,'Cancel');
        
   dlg.btnGp.ok.onClick = function(){

       dlg.close();
       aWidth = Number(dlg.widthGP.eTxt.text);
       aHeight = Number(dlg.heightGP.eTxt.text);
       desktopHead = dlg.textGP.eTxt.text;
       }
            
     dlg.show();

var docLayers = doc.layers.length; //getting top layers because artboards are top layers

for(var i=0;i<docLayers;i++){
    doc.activeLayer = doc.layers[i];
    var aLayer = doc.activeLayer
    ab = artboard_rectangle(doc.activeLayer);
    if((ab[2]-ab[0]) == aWidth && (ab[3]-ab[1]) == aHeight)
        var layLen = aLayer.layers.length
        for(var j=0;j<layLen;j++){
            doc.activeLayer = aLayer.layers[j];
            var layerRef = doc.activeLayer

            if (layerRef.kind === LayerKind.TEXT) {
                if(layerRef.textItem.font === "Arial-BoldMT"){//make sure you have correct font name
                    layerRef.textItem.contents = desktopHead;
                    }
                }
            }
    }

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

Vibi DevAuthor
Inspiring
October 3, 2022

This is awesome, Chuck. It works as expected. But there's a challenge in this script. Whenever the text layer is inside a group, the script doesn't work or gives an error. Is there a fix to it? Please let me know. I really appreciate your support.  

Chuck Uebele
Adobe Expert
October 3, 2022

Yea, I can look at that. So the text layer you want to change is within a group, inside the artboard?

Vibi DevAuthor
Inspiring
October 3, 2022
var fileRef = app.activeDocument;

// Gets user input
var defaultValue = '';
var desktopHead = prompt("Paste your new headline", defaultValue);
layers = fileRef.layers; //getting top layers because artboards are top layers

function changeTextLayerContent(doc, newTextString) {
for (var i = 0, max = doc.layers.length; i < max; i++) {
var layerRef = doc.layers[i];
 
if (layerRef.typename === "ArtLayer") {

if (layerRef.kind === LayerKind.TEXT) {
if (layerRef.textItem.font === "Arial-Bold") {
layerRef.textItem.contents = desktopHead;
}
}
} else {
changeTextLayerContent(layerRef, desktopHead);
}
}
}
changeTextLayerContent(fileRef, desktopHead);


This script will access all the artboard's text layers. But I would like to access only selective artboard's text layers using the artboard's dimensions. 
Inspiring
October 3, 2022

Forgive me. I'm not sure what you mean by "select all the text layers by font type" and "Artboard needs to be identified by its dimension".

 

Do you want to create an array of all the text layers and sort them by font type? Do you want to rename the ArtBoards with the dimensions? Can you give an example of the output you're looking for?

Vibi DevAuthor
Inspiring
October 3, 2022

Hey Jugenjury, Thanks for pitching in. I will make sure to put it clear this time. 

I have 10 artboards in a single PSD file. Each artboard is different in size. I would like to access X number of artboards in terms of dimensions and change the text layers content(which are in Arial-bold) within those selected artboards. One single text content to replace all the text layers(which are in Arial-bold) across the selected artboards. Thanks in advance. I hope this is not confusing. 

Chuck Uebele
Adobe Expert
October 3, 2022

Your script seems to select more that just artboards, so it needs some refinement. Here's a script by r-bin (slightly modified) that will displace the artboard size. So this script needs to be incorporated into your script to find the artboard the size you want it. Then you need to loop through the correct artboard, find the text layers, and the see if it's the text font that you want.

 

var layer = activeDocument.layers[0];
var lb = layer_bounds(layer);
var ab = artboard_rectangle(layer);
alert("DOM width = " + (activeDocument.activeLayer.bounds[2]-activeDocument.activeLayer.bounds[0]) + "\n\n"+
      "AM layer width in px = " + (lb[2]-lb[0]) + "\n\n"+
      "AM artboardr width in px = " + (ab[2]-ab[0]) + '\n\n' +
      'AM artboard height in px = ' +(ab[3]-ab[1]));

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

function layer_bounds(layer, no_effects)
    {
    try {        
        var prop = stringIDToTypeID("bounds");
        if (no_effects == true)  prop = stringIDToTypeID("boundsNoEffects");
        if (no_effects == false) prop = stringIDToTypeID("boundsNoMask");
        var r = new ActionReference();    
        r.putProperty(stringIDToTypeID("property"), prop);
        if (layer) r.putIdentifier(stringIDToTypeID("layer"), layer.id);
        else       r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));   

        var d = executeActionGet(r).getObjectValue(prop);

        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 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); }
    }
Vibi DevAuthor
Inspiring
October 3, 2022

Thank you so much, Chuck. I will check and get back. I have also added a new script to this post. Could you please check that as well? 

Chuck Uebele
Adobe Expert
October 3, 2022

I'm working on it. Might take a bit, as I've run into a snag. Hopefully by this evening.