Create a JavaScript JavaScript layer

You can probably guess what I'm trying to do with this snippet:

var docRef = app.activeDocument;

var layerRef = app.activeDocument.artLayers.add();
layerRef.kind = LayerKind.SOLIDFILL;

I want a script to create a fill layer (and then specify a color, remove a mask, etc.). I get this answer from the ExtendScript Toolkit: "You can change the type of a layer to text or plain only"

I would have thought that one could somehow pass arguments to a method add() artLayers?! Am I missing something really simple? Thank.

I know that this can be done with the help of actions, but I would like to learn how to make this (seemingly) very simple task and build on it to create more complex and useful scripts.

Also, I don’t know if this is important, but I run Ps CC15, ES toolkit 4 and using link script CC14

+4
source share
1 answer

For easy filling you just need to change the third line to

app.activeDocument.selection.fill(app.foregroundColor, ColorBlendMode.NORMAL, 100, false);

If you want to add a new solid fill to the script, I added a function to it. Hard fill works with RGB colors, foreground color works with HEX cols

var docRef = app.activeDocument;

var layerRef = app.activeDocument.artLayers.add();

// set the foreground colour
var myColour = "F7E7CE";
setColour(myColour);

// fill this
// app.activeDocument.selection.fill(app.foregroundColor, ColorBlendMode.NORMAL, 100, false);

//or
// new solid fill
fillSolidColour(247,231,206);


// function SET COLOUR (hexcolour, set background?)
// --------------------------------------------------------
function setColour(hexcolour)
{
    // set foreground colour to matching colour
    var tempColor = new SolidColor;
    hexcolour = hexcolour.toString(); // stringify it

    tempColor.rgb.hexValue = hexcolour;

    // set foreground
    foregroundColor = tempColor;
}


function fillSolidColour(R, G, B)
{
  // =======================================================
  var id117 = charIDToTypeID( "Mk  " );
  var desc25 = new ActionDescriptor();
  var id118 = charIDToTypeID( "null" );
  var ref13 = new ActionReference();
  var id119 = stringIDToTypeID( "contentLayer" );
  ref13.putClass( id119 );
  desc25.putReference( id118, ref13 );
  var id120 = charIDToTypeID( "Usng" );
  var desc26 = new ActionDescriptor();
  var id121 = charIDToTypeID( "Type" );
  var desc27 = new ActionDescriptor();
  var id122 = charIDToTypeID( "Clr " );
  var desc28 = new ActionDescriptor();
  var id123 = charIDToTypeID( "Rd  " );
  desc28.putDouble( id123, R ); //red
  var id124 = charIDToTypeID( "Grn " );
  desc28.putDouble( id124, G ); //green
  var id125 = charIDToTypeID( "Bl  " );
  desc28.putDouble( id125, B ); //blue
  var id126 = charIDToTypeID( "RGBC" );
  desc27.putObject( id122, id126, desc28 );
  var id127 = stringIDToTypeID( "solidColorLayer" );
  desc26.putObject( id121, id127, desc27 );
  var id128 = stringIDToTypeID( "contentLayer" );
  desc25.putObject( id120, id128, desc26 );
  executeAction( id117, desc25, DialogModes.NO );
}
+1
source

All Articles