Skip to main content
jonohunt
Known Participant
July 23, 2018
Answered

Add Colour Overlay layer style with colour from Clipboard?

  • July 23, 2018
  • 2 replies
  • 3551 views

I managed to grab a script using the ScriptingListener plugin that will add the 'Colour Overlay' layer style to the selected layer or folder.

// =======================================================

var idsetd = charIDToTypeID( "setd" );

    var desc9 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref1 = new ActionReference();

        var idPrpr = charIDToTypeID( "Prpr" );

        var idLefx = charIDToTypeID( "Lefx" );

        ref1.putProperty( idPrpr, idLefx );

        var idLyr = charIDToTypeID( "Lyr " );

        var idOrdn = charIDToTypeID( "Ordn" );

        var idTrgt = charIDToTypeID( "Trgt" );

        ref1.putEnumerated( idLyr, idOrdn, idTrgt );

    desc9.putReference( idnull, ref1 );

    var idT = charIDToTypeID( "T   " );

        var desc10 = new ActionDescriptor();

        var idScl = charIDToTypeID( "Scl " );

        var idPrc = charIDToTypeID( "#Prc" );

        desc10.putUnitDouble( idScl, idPrc, 100.000000 );

        var idSoFi = charIDToTypeID( "SoFi" );

            var desc11 = new ActionDescriptor();

            var idenab = charIDToTypeID( "enab" );

            desc11.putBoolean( idenab, true );

            var idpresent = stringIDToTypeID( "present" );

            desc11.putBoolean( idpresent, true );

            var idshowInDialog = stringIDToTypeID( "showInDialog" );

            desc11.putBoolean( idshowInDialog, true );

            var idMd = charIDToTypeID( "Md  " );

            var idBlnM = charIDToTypeID( "BlnM" );

            var idNrml = charIDToTypeID( "Nrml" );

            desc11.putEnumerated( idMd, idBlnM, idNrml );

            var idClr = charIDToTypeID( "Clr " );

                var desc12 = new ActionDescriptor();

                var idRd = charIDToTypeID( "Rd  " );

                desc12.putDouble( idRd, 213.996735 );

                var idGrn = charIDToTypeID( "Grn " );

                desc12.putDouble( idGrn, 213.996735 );

                var idBl = charIDToTypeID( "Bl  " );

                desc12.putDouble( idBl, 213.996735 );

            var idRGBC = charIDToTypeID( "RGBC" );

            desc11.putObject( idClr, idRGBC, desc12 );

            var idOpct = charIDToTypeID( "Opct" );

            var idPrc = charIDToTypeID( "#Prc" );

            desc11.putUnitDouble( idOpct, idPrc, 100.000000 );

        var idSoFi = charIDToTypeID( "SoFi" );

        desc10.putObject( idSoFi, idSoFi, desc11 );

    var idLefx = charIDToTypeID( "Lefx" );

    desc9.putObject( idT, idLefx, desc10 );

executeAction( idsetd, desc9, DialogModes.NO );

// =======================================================

It only works with a single selected layer or group (not multiple selected layers/groups), and what I'd like to be able to do if possible is instead of having the colour specified within the script, grab the colour from the system clipboard (I guess as a variable) in a RGB HEX format (e.g. I'd already have something like this copied to the clipboard #AE2993 before running the script), and also be able to run it on multiple selected layers.

Is something like this even possible?

This topic has been closed for replies.
Correct answer r-bin

Select one or more layers. Copy the hex text of the color into the clipboard.

Start the script. Press Ctrl+V (paste).

app.activeDocument.suspendHistory("TMP", "aaa()");

executeAction( charIDToTypeID( "undo" ), undefined, DialogModes.NO );

executeAction( charIDToTypeID( "PaFX" ), undefined, DialogModes.NO );

function aaa()

    {

    var c = new SolidColor();

    var d = new Window("dialog");

    var t = d.add("edittext");

    t.preferredSize.width = 100;

    t.active = true;                                                      

    t.onChanging = function() { c.rgb.hexValue = this.text.replace("#", ""); d.close(); }

    d.show();

    app.activeDocument.artLayers.add();

    // ======================================================= 

    // ====== YOUR CODE ====================================== 

    // ======================================================= 

    // ======================================================= 

    var idsetd = charIDToTypeID( "setd" ); 

        var desc9 = new ActionDescriptor(); 

        var idnull = charIDToTypeID( "null" ); 

            var ref1 = new ActionReference(); 

            var idPrpr = charIDToTypeID( "Prpr" ); 

            var idLefx = charIDToTypeID( "Lefx" ); 

            ref1.putProperty( idPrpr, idLefx ); 

            var idLyr = charIDToTypeID( "Lyr " ); 

            var idOrdn = charIDToTypeID( "Ordn" ); 

            var idTrgt = charIDToTypeID( "Trgt" ); 

            ref1.putEnumerated( idLyr, idOrdn, idTrgt ); 

        desc9.putReference( idnull, ref1 ); 

        var idT = charIDToTypeID( "T   " ); 

            var desc10 = new ActionDescriptor(); 

            var idScl = charIDToTypeID( "Scl " ); 

            var idPrc = charIDToTypeID( "#Prc" ); 

            desc10.putUnitDouble( idScl, idPrc, 100.000000 ); 

            var idSoFi = charIDToTypeID( "SoFi" ); 

                var desc11 = new ActionDescriptor(); 

                var idenab = charIDToTypeID( "enab" ); 

                desc11.putBoolean( idenab, true ); 

                var idpresent = stringIDToTypeID( "present" ); 

                desc11.putBoolean( idpresent, true ); 

                var idshowInDialog = stringIDToTypeID( "showInDialog" ); 

                desc11.putBoolean( idshowInDialog, true ); 

                var idMd = charIDToTypeID( "Md  " ); 

                var idBlnM = charIDToTypeID( "BlnM" ); 

                var idNrml = charIDToTypeID( "Nrml" ); 

                desc11.putEnumerated( idMd, idBlnM, idNrml ); 

                var idClr = charIDToTypeID( "Clr " ); 

                    var desc12 = new ActionDescriptor(); 

                    var idRd = charIDToTypeID( "Rd  " ); 

                    desc12.putDouble( idRd, c.rgb.red );     // RED

                    var idGrn = charIDToTypeID( "Grn " ); 

                    desc12.putDouble( idGrn, c.rgb.green );  // GREEN

                    var idBl = charIDToTypeID( "Bl  " ); 

                    desc12.putDouble( idBl, c.rgb.blue );    // BLUE

                var idRGBC = charIDToTypeID( "RGBC" ); 

                desc11.putObject( idClr, idRGBC, desc12 ); 

                var idOpct = charIDToTypeID( "Opct" ); 

                var idPrc = charIDToTypeID( "#Prc" ); 

                desc11.putUnitDouble( idOpct, idPrc, 100.000000 ); 

            var idSoFi = charIDToTypeID( "SoFi" ); 

            desc10.putObject( idSoFi, idSoFi, desc11 ); 

        var idLefx = charIDToTypeID( "Lefx" ); 

        desc9.putObject( idT, idLefx, desc10 ); 

    executeAction( idsetd, desc9, DialogModes.NO ); 

    // ======================================================= 

    // ======================================================= 

    // ======================================================= 

    executeAction( charIDToTypeID( "CpFX" ), undefined, DialogModes.NO );

    }

2 replies

r-binCorrect answer
Legend
July 23, 2018

Select one or more layers. Copy the hex text of the color into the clipboard.

Start the script. Press Ctrl+V (paste).

app.activeDocument.suspendHistory("TMP", "aaa()");

executeAction( charIDToTypeID( "undo" ), undefined, DialogModes.NO );

executeAction( charIDToTypeID( "PaFX" ), undefined, DialogModes.NO );

function aaa()

    {

    var c = new SolidColor();

    var d = new Window("dialog");

    var t = d.add("edittext");

    t.preferredSize.width = 100;

    t.active = true;                                                      

    t.onChanging = function() { c.rgb.hexValue = this.text.replace("#", ""); d.close(); }

    d.show();

    app.activeDocument.artLayers.add();

    // ======================================================= 

    // ====== YOUR CODE ====================================== 

    // ======================================================= 

    // ======================================================= 

    var idsetd = charIDToTypeID( "setd" ); 

        var desc9 = new ActionDescriptor(); 

        var idnull = charIDToTypeID( "null" ); 

            var ref1 = new ActionReference(); 

            var idPrpr = charIDToTypeID( "Prpr" ); 

            var idLefx = charIDToTypeID( "Lefx" ); 

            ref1.putProperty( idPrpr, idLefx ); 

            var idLyr = charIDToTypeID( "Lyr " ); 

            var idOrdn = charIDToTypeID( "Ordn" ); 

            var idTrgt = charIDToTypeID( "Trgt" ); 

            ref1.putEnumerated( idLyr, idOrdn, idTrgt ); 

        desc9.putReference( idnull, ref1 ); 

        var idT = charIDToTypeID( "T   " ); 

            var desc10 = new ActionDescriptor(); 

            var idScl = charIDToTypeID( "Scl " ); 

            var idPrc = charIDToTypeID( "#Prc" ); 

            desc10.putUnitDouble( idScl, idPrc, 100.000000 ); 

            var idSoFi = charIDToTypeID( "SoFi" ); 

                var desc11 = new ActionDescriptor(); 

                var idenab = charIDToTypeID( "enab" ); 

                desc11.putBoolean( idenab, true ); 

                var idpresent = stringIDToTypeID( "present" ); 

                desc11.putBoolean( idpresent, true ); 

                var idshowInDialog = stringIDToTypeID( "showInDialog" ); 

                desc11.putBoolean( idshowInDialog, true ); 

                var idMd = charIDToTypeID( "Md  " ); 

                var idBlnM = charIDToTypeID( "BlnM" ); 

                var idNrml = charIDToTypeID( "Nrml" ); 

                desc11.putEnumerated( idMd, idBlnM, idNrml ); 

                var idClr = charIDToTypeID( "Clr " ); 

                    var desc12 = new ActionDescriptor(); 

                    var idRd = charIDToTypeID( "Rd  " ); 

                    desc12.putDouble( idRd, c.rgb.red );     // RED

                    var idGrn = charIDToTypeID( "Grn " ); 

                    desc12.putDouble( idGrn, c.rgb.green );  // GREEN

                    var idBl = charIDToTypeID( "Bl  " ); 

                    desc12.putDouble( idBl, c.rgb.blue );    // BLUE

                var idRGBC = charIDToTypeID( "RGBC" ); 

                desc11.putObject( idClr, idRGBC, desc12 ); 

                var idOpct = charIDToTypeID( "Opct" ); 

                var idPrc = charIDToTypeID( "#Prc" ); 

                desc11.putUnitDouble( idOpct, idPrc, 100.000000 ); 

            var idSoFi = charIDToTypeID( "SoFi" ); 

            desc10.putObject( idSoFi, idSoFi, desc11 ); 

        var idLefx = charIDToTypeID( "Lefx" ); 

        desc9.putObject( idT, idLefx, desc10 ); 

    executeAction( idsetd, desc9, DialogModes.NO ); 

    // ======================================================= 

    // ======================================================= 

    // ======================================================= 

    executeAction( charIDToTypeID( "CpFX" ), undefined, DialogModes.NO );

    }

jonohunt
jonohuntAuthor
Known Participant
July 24, 2018

That's great, thanks a lot!

If I wanted to add the hex colour to the script (in this format 5DAFAA) without showing the dialog box (just run the script using the colour in the script), would that be easy to do? If I had the script for this I might be able run it through an AppleScript and use that to grab the colour from the clipboard and insert it into the JavaScript.

Legend
July 24, 2018

jonohunt  написал(а)

If I wanted to add the hex colour to the script (in this format 5DAFAA) without showing the dialog box (just run the script using the colour in the script), would that be easy to do?

OK. Something like this.

var c = new SolidColor(); 

c.rgb.hexValue = "5DAFAA"

app.activeDocument.suspendHistory("TMP", "aaa()"); 

 

executeAction( charIDToTypeID( "undo" ), undefined, DialogModes.NO ); 

 

executeAction( charIDToTypeID( "PaFX" ), undefined, DialogModes.NO ); 

 

function aaa() 

    { 

    app.activeDocument.artLayers.add(); 

 

    // =======================================================   

    // ====== YOUR CODE ======================================   

    // =======================================================   

    // =======================================================   

    var idsetd = charIDToTypeID( "setd" );   

        var desc9 = new ActionDescriptor();   

        var idnull = charIDToTypeID( "null" );   

            var ref1 = new ActionReference();   

            var idPrpr = charIDToTypeID( "Prpr" );   

            var idLefx = charIDToTypeID( "Lefx" );   

            ref1.putProperty( idPrpr, idLefx );   

            var idLyr = charIDToTypeID( "Lyr " );   

            var idOrdn = charIDToTypeID( "Ordn" );   

            var idTrgt = charIDToTypeID( "Trgt" );   

            ref1.putEnumerated( idLyr, idOrdn, idTrgt );   

        desc9.putReference( idnull, ref1 );   

        var idT = charIDToTypeID( "T   " );   

            var desc10 = new ActionDescriptor();   

            var idScl = charIDToTypeID( "Scl " );   

            var idPrc = charIDToTypeID( "#Prc" );   

            desc10.putUnitDouble( idScl, idPrc, 100.000000 );   

            var idSoFi = charIDToTypeID( "SoFi" );   

                var desc11 = new ActionDescriptor();   

                var idenab = charIDToTypeID( "enab" );   

                desc11.putBoolean( idenab, true );   

                var idpresent = stringIDToTypeID( "present" );   

                desc11.putBoolean( idpresent, true );   

                var idshowInDialog = stringIDToTypeID( "showInDialog" );   

                desc11.putBoolean( idshowInDialog, true );   

                var idMd = charIDToTypeID( "Md  " );   

                var idBlnM = charIDToTypeID( "BlnM" );   

                var idNrml = charIDToTypeID( "Nrml" );   

                desc11.putEnumerated( idMd, idBlnM, idNrml );   

                var idClr = charIDToTypeID( "Clr " );   

                    var desc12 = new ActionDescriptor();   

                    var idRd = charIDToTypeID( "Rd  " );   

                    desc12.putDouble( idRd, c.rgb.red );     // RED 

                    var idGrn = charIDToTypeID( "Grn " );   

                    desc12.putDouble( idGrn, c.rgb.green );  // GREEN 

                    var idBl = charIDToTypeID( "Bl  " );   

                    desc12.putDouble( idBl, c.rgb.blue );    // BLUE 

                var idRGBC = charIDToTypeID( "RGBC" );   

                desc11.putObject( idClr, idRGBC, desc12 );   

                var idOpct = charIDToTypeID( "Opct" );   

                var idPrc = charIDToTypeID( "#Prc" );   

                desc11.putUnitDouble( idOpct, idPrc, 100.000000 );   

            var idSoFi = charIDToTypeID( "SoFi" );   

            desc10.putObject( idSoFi, idSoFi, desc11 );   

        var idLefx = charIDToTypeID( "Lefx" );   

        desc9.putObject( idT, idLefx, desc10 );   

    executeAction( idsetd, desc9, DialogModes.NO );   

    // =======================================================   

    // =======================================================   

    // =======================================================   

 

    executeAction( charIDToTypeID( "CpFX" ), undefined, DialogModes.NO ); 

    } 

Kukurykus
Legend
July 23, 2018

As far as I know you can't by Ps sctipting reads clipboard, however you can store something in. If you're on Macintosh I can't advice anything, but on Windows with system is the same problem. With .vbs I never tried, while I know it's possible with C language and autohotky that I use in this case. It's better you saved your clipboard to name of .txt file or as its content, then it's easy to read by javascrtipt. Regarding applying 'Colour Overlay' to multiply layer(Set)s it's possible. Just say for your OS.

jonohunt
jonohuntAuthor
Known Participant
July 23, 2018

Thanks for the answer, yes I'm on a Mac.

Actually, I've just found an AppleScript that can run some JavaScript within it. I know a bit of AppleScript, so could grab the clipboard contents, set it as a variable, and (hopefully) insert the value into the JavaScript.

But I'd need to be able to change this colour format in the code below to the simpler RGB HEX format (#AE2993) that I often copy to the clipboard.

            var idClr = charIDToTypeID( "Clr " );

                var desc12 = new ActionDescriptor();

                var idRd = charIDToTypeID( "Rd  " );

                desc12.putDouble( idRd, 213.996735 );

                var idGrn = charIDToTypeID( "Grn " );

                desc12.putDouble( idGrn, 213.996735 );

                var idBl = charIDToTypeID( "Bl  " );

                desc12.putDouble( idBl, 213.996735 );

Although I wouldn't mind storing the colour value in a .txt file somewhere if that would be easier. I could write an AppleScript to grab the colour from the clipboard and write it to the .txt file, and the JavaScript could then (hopefully) read the colour from that .txt file.

And also ideally I'd like to be able to run this on multiple selected layers, not just a single layer.

Kukurykus
Legend
July 23, 2018

From your earlier post I understood you want to grab HEX then change to RGB so you can use it by a script, but now you additionally want to change RGB to HEX and put into clipboard back? And if you are on Mac then once again, copy your clipboard to temporary .txt file you save on desktop using AppleScript or whatever so it'll make things much simpler...