Skip to main content
jonohunt
Known Participant
September 3, 2018
Answered

Set the fill colour of a vector shape?

  • September 3, 2018
  • 1 reply
  • 1335 views

Is it possible to set the fill colour and/or stroke colour of a vector shape in the currently selected layer with a script?

I'd use it in a similar way to the one I had help with before at this forum, by using a colour copied previously to the clipboard (in RGB Hex format) and applying that colour to the vector shape.

Here's the script I'm using to grab the colour from the clipboard and use when adding the colour overlay layer style, in case it helps understanding what I want to achieve with the new script:

set colourFromClipboard to the clipboard

tell application id "com.adobe.Photoshop"

  set docRef to the current document

  try

  tell docRef

  do javascript "var c = new SolidColor();   

 

c.rgb.hexValue = " & quoted form of colourFromClipboard & "

 

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

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

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

   

function aaa()   

    {   

    app.activeDocument.artLayers.add();   

     

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

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

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

    }" show debugger on runtime error

  end tell

  end try

end tell

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

Have you tried using ScriptListener?

You could get about this kind of code for functions.

I can not test how applescript works.

set colourFromClipboard to the clipboard 

 

tell application id "com.adobe.Photoshop" 

  set docRef to the current document 

  try 

  tell docRef 

  do javascript "var c = new SolidColor();     

   

c.rgb.hexValue = " & quoted form of colourFromClipboard & "  

set_fill_color(c);

//set_stroke_color(c);

function set_stroke_color(c)

    {

    try {

        var d = new ActionDescriptor();

        var r = new ActionReference();

        r.putEnumerated(stringIDToTypeID('contentLayer'), stringIDToTypeID('ordinal'), stringIDToTypeID('targetEnum'));

        d.putReference(stringIDToTypeID('null'), r);

        var d1 = new ActionDescriptor();

        var d2 = new ActionDescriptor();

        var d3 = new ActionDescriptor();

        var d4 = new ActionDescriptor();

        d4.putDouble(stringIDToTypeID('red'),   c.rgb.red);

        d4.putDouble(stringIDToTypeID('green'), c.rgb.green);

        d4.putDouble(stringIDToTypeID('blue'),  c.rgb.blue);

        d3.putObject(stringIDToTypeID('color'), stringIDToTypeID('RGBColor'), d4);

        d2.putObject(stringIDToTypeID('strokeStyleContent'), stringIDToTypeID('solidColorLayer'), d3);

        d2.putBoolean(stringIDToTypeID('strokeEnabled'), true);

        d1.putObject(stringIDToTypeID('strokeStyle'), stringIDToTypeID('strokeStyle'), d2);

        d.putObject(stringIDToTypeID('to'), stringIDToTypeID('shapeStyle'), d1);

        executeAction(stringIDToTypeID('set'), d, DialogModes.NO);

        }

    catch (e) { throw(e); }

    }

function set_fill_color(c)

    {

    try {

        var d = new ActionDescriptor();

        var r = new ActionReference();

        r.putEnumerated(stringIDToTypeID('contentLayer'), stringIDToTypeID('ordinal'), stringIDToTypeID('targetEnum'));

        d.putReference(stringIDToTypeID('null'), r);

        var d1 = new ActionDescriptor();

        var d2 = new ActionDescriptor();

        var d3 = new ActionDescriptor();

        d3.putDouble(stringIDToTypeID('red'),   c.rgb.red);

        d3.putDouble(stringIDToTypeID('green'), c.rgb.green);

        d3.putDouble(stringIDToTypeID('blue'),  c.rgb.blue);

        d2.putObject(stringIDToTypeID('color'), stringIDToTypeID('RGBColor'), d3);

        d1.putObject(stringIDToTypeID('fillContents'), stringIDToTypeID('solidColorLayer'), d2);

        var d4 = new ActionDescriptor();

        d4.putBoolean(stringIDToTypeID('fillEnabled'), true);

        d1.putObject(stringIDToTypeID('strokeStyle'), stringIDToTypeID('strokeStyle'), d4);

        d.putObject(stringIDToTypeID('to'), stringIDToTypeID('shapeStyle'), d1);

        executeAction(stringIDToTypeID('set'), d, DialogModes.NO);

        }

    catch (e) { throw(e); }

    }" show debugger on runtime error 

  end tell 

  end try 

end tell

1 reply

r-binCorrect answer
Legend
September 4, 2018

Have you tried using ScriptListener?

You could get about this kind of code for functions.

I can not test how applescript works.

set colourFromClipboard to the clipboard 

 

tell application id "com.adobe.Photoshop" 

  set docRef to the current document 

  try 

  tell docRef 

  do javascript "var c = new SolidColor();     

   

c.rgb.hexValue = " & quoted form of colourFromClipboard & "  

set_fill_color(c);

//set_stroke_color(c);

function set_stroke_color(c)

    {

    try {

        var d = new ActionDescriptor();

        var r = new ActionReference();

        r.putEnumerated(stringIDToTypeID('contentLayer'), stringIDToTypeID('ordinal'), stringIDToTypeID('targetEnum'));

        d.putReference(stringIDToTypeID('null'), r);

        var d1 = new ActionDescriptor();

        var d2 = new ActionDescriptor();

        var d3 = new ActionDescriptor();

        var d4 = new ActionDescriptor();

        d4.putDouble(stringIDToTypeID('red'),   c.rgb.red);

        d4.putDouble(stringIDToTypeID('green'), c.rgb.green);

        d4.putDouble(stringIDToTypeID('blue'),  c.rgb.blue);

        d3.putObject(stringIDToTypeID('color'), stringIDToTypeID('RGBColor'), d4);

        d2.putObject(stringIDToTypeID('strokeStyleContent'), stringIDToTypeID('solidColorLayer'), d3);

        d2.putBoolean(stringIDToTypeID('strokeEnabled'), true);

        d1.putObject(stringIDToTypeID('strokeStyle'), stringIDToTypeID('strokeStyle'), d2);

        d.putObject(stringIDToTypeID('to'), stringIDToTypeID('shapeStyle'), d1);

        executeAction(stringIDToTypeID('set'), d, DialogModes.NO);

        }

    catch (e) { throw(e); }

    }

function set_fill_color(c)

    {

    try {

        var d = new ActionDescriptor();

        var r = new ActionReference();

        r.putEnumerated(stringIDToTypeID('contentLayer'), stringIDToTypeID('ordinal'), stringIDToTypeID('targetEnum'));

        d.putReference(stringIDToTypeID('null'), r);

        var d1 = new ActionDescriptor();

        var d2 = new ActionDescriptor();

        var d3 = new ActionDescriptor();

        d3.putDouble(stringIDToTypeID('red'),   c.rgb.red);

        d3.putDouble(stringIDToTypeID('green'), c.rgb.green);

        d3.putDouble(stringIDToTypeID('blue'),  c.rgb.blue);

        d2.putObject(stringIDToTypeID('color'), stringIDToTypeID('RGBColor'), d3);

        d1.putObject(stringIDToTypeID('fillContents'), stringIDToTypeID('solidColorLayer'), d2);

        var d4 = new ActionDescriptor();

        d4.putBoolean(stringIDToTypeID('fillEnabled'), true);

        d1.putObject(stringIDToTypeID('strokeStyle'), stringIDToTypeID('strokeStyle'), d4);

        d.putObject(stringIDToTypeID('to'), stringIDToTypeID('shapeStyle'), d1);

        executeAction(stringIDToTypeID('set'), d, DialogModes.NO);

        }

    catch (e) { throw(e); }

    }" show debugger on runtime error 

  end tell 

  end try 

end tell

jonohunt
jonohuntAuthor
Known Participant
September 4, 2018

I did try ScriptListener. I managed to change the colour of one selected layer, but struggled with multiple layers selected.

But this script works great, thanks!