Highlighted

Change a certain color of texts to a new color

Community Beginner ,
Sep 29, 2020

Copy link to clipboard

Copied

I want to change the pink color of the text to a violet color in all the artboards in one document by script/action. Can anyone help me? Am attaching a screenshot for more clarity.

* this is code from another project, there was an additional check for the presence of a background layer
EDIT 1:

#target photoshop

var oldColor = [255, 0, 0], // [R,G,B]
    newColor = [0, 255, 0], // [R,G,B]
    s2t = stringIDToTypeID;

(r = new ActionReference()).putProperty(s2t('property'), p = s2t('numberOfLayers'));
r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
var len = executeActionGet(r).getInteger(p);

for (var i = 1; i <= len; i++) {
    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('textKey'));
    r.putIndex(s2t('layer'), i);
    if (executeActionGet(r).hasKey(p)) {
        var textKey = executeActionGet(r).getObjectValue(p),
            styleList = textKey.getList(s2t('textStyleRange')),
            l = new ActionList(),
            d = new ActionDescriptor(),
            z = false;
        for (var x = 0; x < styleList.count; x++) {
            k = styleList.getObjectValue(x)
            if (k.getObjectValue(s2t('textStyle')).hasKey(s2t('color'))) {
                s = k.getObjectValue(s2t('textStyle'))
                c = s.getObjectValue(s2t('color'))
                if ((c.getInteger(s2t('red')) == oldColor[0] && c.getInteger(s2t('grain')) == oldColor[1] && c.getInteger(s2t('blue')) == oldColor[2])) {
                    var d = new ActionDescriptor();
                    d.putDouble(s2t('red'), newColor[0])
                    d.putDouble(s2t('grain'), newColor[1])
                    d.putDouble(s2t('blue'), newColor[2])
                    s.putObject(s2t('color'), s2t('RGBColor'), d)
                }
                k.putObject(s2t('textStyle'), s2t('textStyle'), s)
            }
            l.putObject(s2t('textStyleRange'), k)
        }
        if (d.count) {
            textKey.putList(s2t('textStyleRange'), l)
            var d = new ActionDescriptor();
            (r = new ActionReference()).putIndex(s2t('layer'), i);
            d.putReference(s2t('null'), r);
            d.putObject(s2t('to'), s2t('textLayer'), textKey);
            executeAction(s2t('set'), d, DialogModes.NO);
        }
    }
}

* don't forget to change the color values at the beginning of the script as desired:

oldColor = [255, 0, 0], // [R,G,B]
newColor = [0, 255, 0], // [R,G,B]

 

TOPICS
Actions and scripting, How to, Windows

Views

180

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Change a certain color of texts to a new color

Community Beginner ,
Sep 29, 2020

Copy link to clipboard

Copied

I want to change the pink color of the text to a violet color in all the artboards in one document by script/action. Can anyone help me? Am attaching a screenshot for more clarity.

* this is code from another project, there was an additional check for the presence of a background layer
EDIT 1:

#target photoshop

var oldColor = [255, 0, 0], // [R,G,B]
    newColor = [0, 255, 0], // [R,G,B]
    s2t = stringIDToTypeID;

(r = new ActionReference()).putProperty(s2t('property'), p = s2t('numberOfLayers'));
r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
var len = executeActionGet(r).getInteger(p);

for (var i = 1; i <= len; i++) {
    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('textKey'));
    r.putIndex(s2t('layer'), i);
    if (executeActionGet(r).hasKey(p)) {
        var textKey = executeActionGet(r).getObjectValue(p),
            styleList = textKey.getList(s2t('textStyleRange')),
            l = new ActionList(),
            d = new ActionDescriptor(),
            z = false;
        for (var x = 0; x < styleList.count; x++) {
            k = styleList.getObjectValue(x)
            if (k.getObjectValue(s2t('textStyle')).hasKey(s2t('color'))) {
                s = k.getObjectValue(s2t('textStyle'))
                c = s.getObjectValue(s2t('color'))
                if ((c.getInteger(s2t('red')) == oldColor[0] && c.getInteger(s2t('grain')) == oldColor[1] && c.getInteger(s2t('blue')) == oldColor[2])) {
                    var d = new ActionDescriptor();
                    d.putDouble(s2t('red'), newColor[0])
                    d.putDouble(s2t('grain'), newColor[1])
                    d.putDouble(s2t('blue'), newColor[2])
                    s.putObject(s2t('color'), s2t('RGBColor'), d)
                }
                k.putObject(s2t('textStyle'), s2t('textStyle'), s)
            }
            l.putObject(s2t('textStyleRange'), k)
        }
        if (d.count) {
            textKey.putList(s2t('textStyleRange'), l)
            var d = new ActionDescriptor();
            (r = new ActionReference()).putIndex(s2t('layer'), i);
            d.putReference(s2t('null'), r);
            d.putObject(s2t('to'), s2t('textLayer'), textKey);
            executeAction(s2t('set'), d, DialogModes.NO);
        }
    }
}

* don't forget to change the color values at the beginning of the script as desired:

oldColor = [255, 0, 0], // [R,G,B]
newColor = [0, 255, 0], // [R,G,B]

 

TOPICS
Actions and scripting, How to, Windows

Views

181

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Sep 29, 2020 0
LEGEND ,
Sep 29, 2020

Copy link to clipboard

Copied

How familiar are you with JavaScript and Photoshop’s Action Manager code? 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 29, 2020 0
Community Beginner ,
Sep 29, 2020

Copy link to clipboard

Copied

Am not an expert actually. So I just have to run this code as .js in scripts right?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 29, 2020 0
Enthusiast ,
Sep 29, 2020

Copy link to clipboard

Copied

#target photoshop

var oldColor = [255, 0, 0], // [R,G,B]
    newColor = [0, 255, 0], // [R,G,B]
    s2t = stringIDToTypeID;

(r = new ActionReference()).putProperty(s2t('property'), p = s2t('numberOfLayers'));
r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
var len = executeActionGet(r).getInteger(p);

for (var i = 0; i <= len; i++) {
    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('textKey'));
    r.putIndex(s2t('layer'), i);
    if (executeActionGet(r).hasKey(p)) {
        var textKey = executeActionGet(r).getObjectValue(p),
            styleList = textKey.getList(s2t('textStyleRange')),
            l = new ActionList(),
            d = new ActionDescriptor(),
            z = false;
        for (var x = 0; x < styleList.count; x++) {
            k = styleList.getObjectValue(x)
            if (k.getObjectValue(s2t('textStyle')).hasKey(s2t('color'))) {
                s = k.getObjectValue(s2t('textStyle'))
                c = s.getObjectValue(s2t('color'))
                if ((c.getInteger(s2t('red')) == oldColor[0] && c.getInteger(s2t('grain')) == oldColor[1] && c.getInteger(s2t('blue')) == oldColor[2])) {
                    var d = new ActionDescriptor();
                    d.putDouble(s2t('red'), newColor[0])
                    d.putDouble(s2t('grain'), newColor[1])
                    d.putDouble(s2t('blue'), newColor[2])
                    s.putObject(s2t('color'), s2t('RGBColor'), d)
                }
                k.putObject(s2t('textStyle'), s2t('textStyle'), s)
            }
            l.putObject(s2t('textStyleRange'), k)
        }
        if (d.count) {
            textKey.putList(s2t('textStyleRange'), l)
            var d = new ActionDescriptor();
            (r = new ActionReference()).putIndex(s2t('layer'), i);
            d.putReference(s2t('null'), r);
            d.putObject(s2t('to'), s2t('textLayer'), textKey);
            executeAction(s2t('set'), d, DialogModes.NO);
        }
    }
}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 29, 2020 2
Community Beginner ,
Sep 29, 2020

Copy link to clipboard

Copied

So I just have to run this code as .js in scripts right?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 29, 2020 0
Community Beginner ,
Sep 29, 2020

Copy link to clipboard

Copied

How should I run this?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 29, 2020 0
Community Beginner ,
Sep 29, 2020

Copy link to clipboard

Copied

Photoshop_eOHYpEGDlm.png

When running this pops up.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 29, 2020 0
Enthusiast ,
Sep 29, 2020

Copy link to clipboard

Copied

* this is code from another project, there was an additional check for the presence of a background layer
EDIT 1:

#target photoshop

var oldColor = [255, 0, 0], // [R,G,B]
    newColor = [0, 255, 0], // [R,G,B]
    s2t = stringIDToTypeID;

(r = new ActionReference()).putProperty(s2t('property'), p = s2t('numberOfLayers'));
r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
var len = executeActionGet(r).getInteger(p);

for (var i = 1; i <= len; i++) {
    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('textKey'));
    r.putIndex(s2t('layer'), i);
    if (executeActionGet(r).hasKey(p)) {
        var textKey = executeActionGet(r).getObjectValue(p),
            styleList = textKey.getList(s2t('textStyleRange')),
            l = new ActionList(),
            d = new ActionDescriptor(),
            z = false;
        for (var x = 0; x < styleList.count; x++) {
            k = styleList.getObjectValue(x)
            if (k.getObjectValue(s2t('textStyle')).hasKey(s2t('color'))) {
                s = k.getObjectValue(s2t('textStyle'))
                c = s.getObjectValue(s2t('color'))
                if ((c.getInteger(s2t('red')) == oldColor[0] && c.getInteger(s2t('grain')) == oldColor[1] && c.getInteger(s2t('blue')) == oldColor[2])) {
                    var d = new ActionDescriptor();
                    d.putDouble(s2t('red'), newColor[0])
                    d.putDouble(s2t('grain'), newColor[1])
                    d.putDouble(s2t('blue'), newColor[2])
                    s.putObject(s2t('color'), s2t('RGBColor'), d)
                }
                k.putObject(s2t('textStyle'), s2t('textStyle'), s)
            }
            l.putObject(s2t('textStyleRange'), k)
        }
        if (d.count) {
            textKey.putList(s2t('textStyleRange'), l)
            var d = new ActionDescriptor();
            (r = new ActionReference()).putIndex(s2t('layer'), i);
            d.putReference(s2t('null'), r);
            d.putObject(s2t('to'), s2t('textLayer'), textKey);
            executeAction(s2t('set'), d, DialogModes.NO);
        }
    }
}

* don't forget to change the color values at the beginning of the script as desired:

oldColor = [255, 0, 0], // [R,G,B]
newColor = [0, 255, 0], // [R,G,B]

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 29, 2020 2
Abin0D4D LATEST
Community Beginner ,
Sep 29, 2020

Copy link to clipboard

Copied

Thanks a lot, brother. Really helpful. Thank You so much.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 29, 2020 0