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

set Shape Color with Colorpicker or Foregroundcolor Script

Contributor ,
Nov 29, 2023 Nov 29, 2023

Hello dear community 😊,
I hope you can help me?
I have selected several shape layers in Photoshop and would like to either call up the color picker for "Solidfill" and then apply the color to all selected layers or simply automatically apply the foreground color to all selected shape layers.

Is there any way to do this via script? It would be extremely helpful.

 

Shape Layer.jpg

TOPICS
Actions and scripting
329
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 2 Correct answers

Community Expert , Nov 29, 2023 Nov 29, 2023

I found an old Script that changes selected Solid Color Layers randomly, feel free to adapt it to apply the Foreground Color instead.  

 

// change selected solid color layers to a random color;
// 2020, use it at your own risk;
if (app.documents.length > 0) {
var myDocument = app.activeDocument;
myDocument.suspendHistory("stuff", "main()");
};
////// the function //////
function main () {
var myDocument = app.activeDocument;
var theLayers = getSelectedSolidColorLayersIdentifier();
for (var m = 
...
Translate
Community Expert , Nov 30, 2023 Nov 30, 2023

The following script will set the fill colour of all (multiple) selected solid or shape layers to the same foreground colour picker value. It offers a single undo history step. It's late here, so I'll need to add some other basic checks later in an updated 1.1 version.

 

/*
Change Fill Colour Of All Selected Layers.jsx
https://community.adobe.com/t5/photoshop-ecosystem-discussions/set-shape-color-with-colorpicker-or-foregroundcolor-script/td-p/14267798
v1.0, 30th November 2023, Stephen Marsh
*/
...
Translate
Adobe
Community Expert ,
Nov 29, 2023 Nov 29, 2023

I found an old Script that changes selected Solid Color Layers randomly, feel free to adapt it to apply the Foreground Color instead.  

 

// change selected solid color layers to a random color;
// 2020, use it at your own risk;
if (app.documents.length > 0) {
var myDocument = app.activeDocument;
myDocument.suspendHistory("stuff", "main()");
};
////// the function //////
function main () {
var myDocument = app.activeDocument;
var theLayers = getSelectedSolidColorLayersIdentifier();
for (var m = 0; m < theLayers.length; m++) {
changeSolidColor (theLayers[m], Math.floor(Math.random()*255), Math.floor(Math.random()*255), Math.floor(Math.random()*255));
}
};
//////////////////////////////////////////
////// based on code by paul mr //////
function getSelectedSolidColorLayersIdentifier(){
var selectedLayers = new Array;
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var desc = executeActionGet(ref);
if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){
desc = desc.getList( stringIDToTypeID( 'targetLayers' ));
var c = desc.count;
var selectedLayers = new Array();
// run through selected layers;
for(var i=0;i<c;i++){
try{activeDocument.backgroundLayer;
var theIndex = desc.getReference( i ).getIndex();
}catch(e){var theIndex = desc.getReference( i ).getIndex()+1 };
// get id for solid color layers;
try {
var ref = new ActionReference();
ref.putIndex( charIDToTypeID("Lyr "), theIndex ); 
var layerDesc = executeActionGet(ref);
var theIdentifier = layerDesc.getInteger(stringIDToTypeID ("layerID"));
var adjList = layerDesc.getList(stringIDToTypeID('adjustment'));
var theColors = adjList.getObjectValue(0).getObjectValue(stringIDToTypeID('color'));
selectedLayers.push(  theIdentifier );
} catch (e) {};
};
// if only one:
}else{
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var layerDesc = executeActionGet(ref);
try {
var theIdentifier = layerDesc.getInteger(stringIDToTypeID ("layerID"));
var adjList = layerDesc.getList(stringIDToTypeID('adjustment'));
var theColors = adjList.getObjectValue(0).getObjectValue(stringIDToTypeID('color'));
selectedLayers = [theIdentifier]
} catch (e) {};
};
return selectedLayers;
};
////// change color of solid color layer //////
function changeSolidColor (theIdentifier, theR, theG, theB) {
// =======================================================
var idsetd = charIDToTypeID( "setd" );
    var desc4 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref1 = new ActionReference();
        ref1.putIdentifier ( stringIDToTypeID( "contentLayer" ), theIdentifier );
    desc4.putReference( idnull, ref1 );
    var idT = charIDToTypeID( "T   " );
        var desc5 = new ActionDescriptor();
        var idClr = charIDToTypeID( "Clr " );
            var desc6 = new ActionDescriptor();
            var idRd = charIDToTypeID( "Rd  " );
            desc6.putDouble( idRd, theR );
            var idGrn = charIDToTypeID( "Grn " );
            desc6.putDouble( idGrn, theG );
            var idBl = charIDToTypeID( "Bl  " );
            desc6.putDouble( idBl, theB );
        var idRGBC = charIDToTypeID( "RGBC" );
        desc5.putObject( idClr, idRGBC, desc6 );
    var idsolidColorLayer = stringIDToTypeID( "solidColorLayer" );
    desc4.putObject( idT, idsolidColorLayer, desc5 );
	executeAction( idsetd, desc4, DialogModes.NO );
};

 

 

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
Contributor ,
Nov 30, 2023 Nov 30, 2023

tthank you very much, that really helps me a lot! but if anyone knows another solution to use the colorpicker that would be a nice nice to have 🙂 👍

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 ,
Nov 30, 2023 Nov 30, 2023

@Innotex Nico Tanne wrote:

tthank you very much, that really helps me a lot! but if anyone knows another solution to use the colorpicker that would be a nice nice to have 🙂 👍


 

The following script changes a single selected shape/solid fill layer using the foreground colour picker.

 

It would need to be adapted for multiple selected layers.

 

/*
Change Solid Fill Layer via Color Picker.jsx
https://community.adobe.com/t5/photoshop-ecosystem-discussions/how-to-update-solid-color-adjustment-layer-color-using-photoshop-scripting/m-p/13702428
v1.0 - 4th April 2023, Stephen Marsh
*/

#target photoshop

if (app.documents.length > 0) {

    // Add check for color fill layer...
    if (activeDocument.activeLayer.kind == LayerKind.SOLIDFILL) {

        var savedForegroundColor = app.foregroundColor;
        getColorpickerColor();
        var fColor = app.foregroundColor;
        var rValue = fColor.rgb.red.toFixed(2);
        var gValue = fColor.rgb.green.toFixed(2);
        var bValue = fColor.rgb.blue.toFixed(2);
        setColorFill(rValue, gValue, bValue);
        app.foregroundColor = savedForegroundColor;


        ///// FUNCTIONS /////

        function getColorpickerColor() {
            if (app.showColorPicker()) {
                return app.foregroundColor;
            } else {
                return false;
            }
        }

        function setColorFill(red, grain, blue) {
            function s2t(s) {
                return app.stringIDToTypeID(s);
            }
            var descriptor = new ActionDescriptor();
            var descriptor2 = new ActionDescriptor();
            var descriptor3 = new ActionDescriptor();
            var reference = new ActionReference();
            reference.putEnumerated(s2t("contentLayer"), s2t("ordinal"), s2t("targetEnum"));
            descriptor.putReference(s2t("null"), reference);
            descriptor3.putDouble(s2t("red"), red);
            descriptor3.putDouble(s2t("grain"), grain);
            descriptor3.putDouble(s2t("blue"), blue);
            descriptor2.putObject(s2t("color"), s2t("RGBColor"), descriptor3);
            descriptor.putObject(s2t("to"), s2t("solidColorLayer"), descriptor2);
            executeAction(s2t("set"), descriptor, DialogModes.NO);
        }

    } else {
        alert("The active layer isn't a Color Fill layer!");
    }

} else {
    alert('You must have a document open!');
}

 

 

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 ,
Nov 30, 2023 Nov 30, 2023

The following script will set the fill colour of all (multiple) selected solid or shape layers to the same foreground colour picker value. It offers a single undo history step. It's late here, so I'll need to add some other basic checks later in an updated 1.1 version.

 

/*
Change Fill Colour Of All Selected Layers.jsx
https://community.adobe.com/t5/photoshop-ecosystem-discussions/set-shape-color-with-colorpicker-or-foregroundcolor-script/td-p/14267798
v1.0, 30th November 2023, Stephen Marsh
*/

#target photoshop

function main() {

    // Save the current dialog display settings
    var savedDisplayDialogs = app.displayDialogs;
    app.displayDialogs = DialogModes.NO;

    // Get the selected layers: courtesy of jazz-y
    var s2t = stringIDToTypeID;
    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('targetLayersIDs'));
    r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
    var lrs = executeActionGet(r).getList(p),
        sel = new ActionReference();

    // Store the current foreground colour
    var savedForegroundColor = app.foregroundColor;

    // Foreground colour picker stuff
    getColorpickerColor();
    var fColor = app.foregroundColor;
    var rValue = fColor.rgb.red.toFixed(2);
    var gValue = fColor.rgb.green.toFixed(2);
    var bValue = fColor.rgb.blue.toFixed(2);

    // Loop over the selected layers: courtesy of jazz-y
    for (var i = 0; i < lrs.count; i++) {
        sel.putIdentifier(s2t('layer'), p = lrs.getReference(i).getIdentifier(s2t('layerID')));
        (r = new ActionReference()).putIdentifier(s2t('layer'), p);
        (d = new ActionDescriptor()).putReference(s2t("target"), r);
        executeAction(s2t('select'), d, DialogModes.NO);

        // Add check for color fill layer
        if (activeDocument.activeLayer.kind == LayerKind.SOLIDFILL) {
            // Set the colour
            setColorFill(rValue, gValue, bValue);
        } else {
            //alert("The active layer isn't a Color Fill layer!");
        }
    }
    // Finish the loop

    // Restore the original foreground colour
    app.foregroundColor = savedForegroundColor;

    // Restore the dialogs
    app.displayDialogs = savedDisplayDialogs;
    
}

// Single history stage undo
activeDocument.suspendHistory("Undo script...", "main()");


///// FUNCTIONS /////

function getColorpickerColor() {
    if (app.showColorPicker()) {
        return app.foregroundColor;
    } else {
        return false;
    }
}

function setColorFill(red, grain, blue) {
    function s2t(s) {
        return app.stringIDToTypeID(s);
    }
    var descriptor = new ActionDescriptor();
    var descriptor2 = new ActionDescriptor();
    var descriptor3 = new ActionDescriptor();
    var reference = new ActionReference();
    reference.putEnumerated(s2t("contentLayer"), s2t("ordinal"), s2t("targetEnum"));
    descriptor.putReference(s2t("null"), reference);
    descriptor3.putDouble(s2t("red"), red);
    descriptor3.putDouble(s2t("grain"), grain);
    descriptor3.putDouble(s2t("blue"), blue);
    descriptor2.putObject(s2t("color"), s2t("RGBColor"), descriptor3);
    descriptor.putObject(s2t("to"), s2t("solidColorLayer"), descriptor2);
    executeAction(s2t("set"), descriptor, DialogModes.NO);
}
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
Contributor ,
Nov 30, 2023 Nov 30, 2023

Uh Yeah, thats so nice 👍👍👍

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 ,
Nov 30, 2023 Nov 30, 2023
LATEST

A 1.1 version with a couple of additional checks added:

 

/*
Change Fill Colour Of All Selected Layers.jsx
https://community.adobe.com/t5/photoshop-ecosystem-discussions/set-shape-color-with-colorpicker-or-foregroundcolor-script/td-p/14267798
v1.1, 1st December 2023, Stephen Marsh
*/

#target photoshop

if (app.documents.length) {

    // Check for selected layers: courtesy of jazz-y
    s2t1 = stringIDToTypeID;
    (r = new ActionReference()).putProperty(s2t1('property'), p = s2t1('targetLayers'));
    r.putEnumerated(s2t1("document"), s2t1("ordinal"), s2t1("targetEnum"));
    if (executeActionGet(r).getList(p).count) {

        function main() {

            // Save the current dialog display settings
            var savedDisplayDialogs = app.displayDialogs;
            app.displayDialogs = DialogModes.NO;

            // Capture the initial layer visibility and layer selection
            var currentLayersState = getLayersVisiblity();

            // Get the selected layers: courtesy of jazz-y
            var s2t2 = stringIDToTypeID;
            (r = new ActionReference()).putProperty(s2t2('property'), p = s2t2('targetLayersIDs'));
            r.putEnumerated(s2t2('document'), s2t2('ordinal'), s2t2('targetEnum'));
            var lrs = executeActionGet(r).getList(p),
                sel = new ActionReference();

            // Store the current foreground colour
            var savedForegroundColor = app.foregroundColor;

            // Foreground colour picker stuff
            getColorpickerColor();
            var fColor = app.foregroundColor;
            var rValue = fColor.rgb.red.toFixed(2);
            var gValue = fColor.rgb.green.toFixed(2);
            var bValue = fColor.rgb.blue.toFixed(2);

            // Loop over the selected layers: courtesy of jazz-y
            for (var i = 0; i < lrs.count; i++) {
                sel.putIdentifier(s2t('layer'), p = lrs.getReference(i).getIdentifier(s2t('layerID')));
                (r = new ActionReference()).putIdentifier(s2t('layer'), p);
                (d = new ActionDescriptor()).putReference(s2t("target"), r);
                executeAction(s2t('select'), d, DialogModes.NO);

                // DOM check:
                //if (activeDocument.activeLayer.kind == LayerKind.SOLIDFILL) {

                // AM code to determine layer kind: courtesy of jazz-y
                s2t = stringIDToTypeID;
                (r = new ActionReference()).putProperty(s2t('property'), p = s2t('layerKind'));
                r.putEnumerated(s2t('layer'), s2t('ordinal'), s2t('targetEnum'));
                var layerKind = executeActionGet(r).getInteger(p);
                // Add check for color fill layer - "kVectorSheet" || "kSolidColorSheet"
                if (layerKind == 4 || layerKind == 11) {
                    // Set the colour
                    setColorFill(rValue, gValue, bValue);
                } else {}
            }
            // Finish the loop over selected layers

            // Restore the original foreground colour
            app.foregroundColor = savedForegroundColor;

            // Restore the initial layer visibility and selection
            setLayersVisiblity(currentLayersState);

            // Restore the dialogs
            app.displayDialogs = savedDisplayDialogs;
        }

    } else {
        alert('One or more Solid Fill or Shape layers should be selected!');
    }

} else {
    alert('A document must be open to run this script...');
}

// Single history stage undo
activeDocument.suspendHistory("Undo script...", "main()");


///// FUNCTIONS /////

function getColorpickerColor() {
    if (app.showColorPicker()) {
        return app.foregroundColor;
    } else {
        return false;
    }
}

function setColorFill(red, grain, blue) {
    function s2t(s) {
        return app.stringIDToTypeID(s);
    }
    var descriptor = new ActionDescriptor();
    var descriptor2 = new ActionDescriptor();
    var descriptor3 = new ActionDescriptor();
    var reference = new ActionReference();
    reference.putEnumerated(s2t("contentLayer"), s2t("ordinal"), s2t("targetEnum"));
    descriptor.putReference(s2t("null"), reference);
    descriptor3.putDouble(s2t("red"), red);
    descriptor3.putDouble(s2t("grain"), grain);
    descriptor3.putDouble(s2t("blue"), blue);
    descriptor2.putObject(s2t("color"), s2t("RGBColor"), descriptor3);
    descriptor.putObject(s2t("to"), s2t("solidColorLayer"), descriptor2);
    executeAction(s2t("set"), descriptor, DialogModes.NO);
}

function getLayersVisiblity() {
    // by jazz-y
    var s2t = stringIDToTypeID,
        t2s = typeIDToStringID;
    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('targetLayersIDs'));
    r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
    var targetLayers = executeActionGet(r).getList(p),
        seletion = [],
        visiblity = {};
    for (var i = 0; i < targetLayers.count; i++) seletion.push(targetLayers.getReference(i).getIdentifier());
    (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('layerSection'));
        r.putIndex(s2t('layer'), i);
        if (t2s(executeActionGet(r).getEnumerationValue(p)) == 'layerSectionEnd') continue;
        (r = new ActionReference()).putProperty(s2t('property'), p = s2t('layerID'));
        r.putIndex(s2t('layer'), i);
        var id = executeActionGet(r).getInteger(p);
        (r = new ActionReference()).putProperty(s2t('property'), p = s2t('visible'));
        r.putIndex(s2t('layer'), i);
        var visible = executeActionGet(r).getBoolean(p);
        visiblity[id] = visible;
    }
    return {
        selection: seletion,
        visiblity: visiblity
    }
}

function setLayersVisiblity(layersStateObject) {
    // by jazz-y
    var s2t = stringIDToTypeID;
    for (var a in layersStateObject.visiblity) {
        makeVisible = layersStateObject.visiblity[a] ? "show" : "hide";
        (r = new ActionReference()).putIdentifier(s2t('layer'), a);
        (d = new ActionDescriptor()).putReference(s2t('target'), r);
        executeAction(s2t(makeVisible), d, DialogModes.NO);
    }
    if (layersStateObject.selection.length) {
        var r = new ActionReference()
        for (var i = 0; i < layersStateObject.selection.length; i++)
            r.putIdentifier(s2t("layer"), layersStateObject.selection[i]);
        (d = new ActionDescriptor()).putReference(s2t("target"), r);
        d.putBoolean(s2t("makeVisible"), false);
        executeAction(s2t("select"), d, DialogModes.NO);
    } else {
        (r = new ActionReference()).putEnumerated(s2t("layer"), s2t('ordinal'), s2t('targetEnum'));
        (d = new ActionDescriptor()).putReference(s2t('target'), r);
        executeAction(s2t('selectNoLayers'), d, DialogModes.NO);
    }
}

 

 

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