Adobe Photoshop Scripting: (Part-03)

How to create Layer & Group layer

Working with layer objects

Photoshop object models contain two types of layer objects.

  1. ArtLayer objects can contain image contents or text contents and are basically equivalent to Layers in the Photoshop application.
  2. LayerSet objects, which can contain zero or more ArtLayer objects.

Creating a new layer, a new text layer & fill the new layer.
Well, it does look simple and is easy to script & shows you how to handle layers using scripting too.

Before considering the code, here are the keys in doing the script:

  • Set the ruler unit.
  • Create a new document or already have the active document.
  • Create a new layer.
  • Filling that layer.
  • Select the entire layer.
  • Deselect the selection.
  • Create a new text layer & add to the layer text.
1. Creating a Layer

Layer Code:

//Set the ruler unit
var defaultRulerUnits = preferences.rulerUnits;
preferences.rulerUnits = Units.PIXELS;

// Get the Current document
var docRef = app.activeDocument;

// Create a new art layer at the beginning of the current document
var layerRef = docRef.artLayers.add();
layerRef.name = "My first layer";
layerRef.blendMode = BlendMode.NORMAL;

//In created Layer fill it with a new fill color (Orange Color)
// First we create color object of color orange
var fillColor = new SolidColor();
fillColor.rgb.red = 243;
fillColor.rgb.green = 112;
fillColor.rgb.blue = 33;

// Select the entire layer
docRef.selection.selectAll();

// Fill the selection with color
docRef.selection.fill(fillColor);

// Deselect
docRef.selection.deselect();

// Add a new text layer
var layerRef = docRef.artLayers.add()
layerRef.kind = LayerKind.TEXT;
layerRef.textItem.contents = "Hello Photoshop!";
layerRef.textItem.size = 100;
layerRef.name = "First Text Layer"

//if we do not use layer name, layer name will be the layer contents name

//Back to the default ruler unit
preferences.rulerUnits = defaultRulerUnits;

Code explanations:

Part 3 Layer code

var defaultRulerUnits = preferences.rulerUnits;
preferences.rulerUnits = Units.PIXELS;

Photoshop saves preferences (Edit>Preferences, ctrl-K for Windows users) each time you quit Photoshop. Since we are changing the ‘Units’ type from whatever it was to pixels, we want to do the opposite thing when the script will be over; that is, the script will have had no impact on your preferences. This explains why the script ends with:

“preferences.rulerUnits = defaultRulerUnits”

2. Creating a LayerSet (Group Layer):

Layer set or Groups are collections of layers. To this document, we will add a “TEXT” layer.

Group Layer Code:

//Set the ruler unit
var defaultRulerUnits = preferences.rulerUnits;
preferences.rulerUnits = Units.PIXELS;

// Get the Current document
var docRef = app.activeDocument;

//Creating a Layer Set (Group Layer)
var layerSets = docRef.layerSets.add();
layerSets.name = "TEXT";

// Create a new text layer inside our layour group called layerSets
var newLayer = layerSets.artLayers.add();
newLayer.kind = LayerKind.TEXT;
newLayer.name = "This is Group Layer Text"

// Set font size, font style and position
newLayer.textItem.size = 50;
newLayer.textItem.font = "Verdana";
newLayer.textItem.position = [300, 400];
newLayer.textItem.contents = "I'm Group Layer Text";

//Back to the default ruler unit
preferences.rulerUnits = defaultRulerUnits;

Code explanations:

Part 03 Group Layer

Final Code

//Set the ruler unit
var defaultRulerUnits = preferences.rulerUnits;
preferences.rulerUnits = Units.PIXELS;

// Get the Current document
var docRef = app.activeDocument;

// Create a new art layer at the beginning of the current document
var layerRef = docRef.artLayers.add();
layerRef.name = "My first layer";
layerRef.blendMode = BlendMode.NORMAL;

//In created Layer fill it with a new fill color (Orange Color)
// First we create color object of color orange
var fillColor = new SolidColor();
fillColor.rgb.red = 243;
fillColor.rgb.green = 112;
fillColor.rgb.blue = 33;

// Select the entire layer
docRef.selection.selectAll();

// Fill the selection with color
docRef.selection.fill(fillColor);

// Deselect
docRef.selection.deselect();

// Add a new text layer
var layerRef = docRef.artLayers.add()
layerRef.kind = LayerKind.TEXT;
layerRef.textItem.contents = "Hello Photoshop!";
layerRef.textItem.size = 100;
layerRef.name = "First Text Layer"
//if we do not use layer name, layer name will be the layer contents name

//Creating a Layer Set (Group Layer)
var layerSets = docRef.layerSets.add();
layerSets.name = "TEXT";

// Create a new text layer inside our layour group called layerSets
var newLayer = layerSets.artLayers.add();
newLayer.kind = LayerKind.TEXT;
newLayer.name = "This is Group Layer Text"

// Set font size, font style and position
newLayer.textItem.size = 50;
newLayer.textItem.font = "Verdana";
newLayer.textItem.position = [300, 400];
newLayer.textItem.contents = "I'm Group Layer Text";

//Back to the default ruler unit
preferences.rulerUnits = defaultRulerUnits;

1 thought on “Adobe Photoshop Scripting: (Part-03)

Comments are closed.