Adobe Photoshop Scripting: (Part-05)

Adobe Photoshop Scripting: (Part-05) Delete, Show / Hide, and Duplicate layers & group layers.

Deleting Layer:

We can remove a layer from the document by calling the remove() method.

// Get the layer by name and call remove
var docRef = app.activeDocument;
docRef.artLayers.getByName(‘My first layer’).remove();

Deleting Group Layer:

// Get the layer set by name and call remove
docRef.layerSets.getByName(‘TEXT’).remove();

However, if we just want to delete all layers within a layer set but not the layer set (Group), then we need to first get all the layers within that layer set and use removeAll() to delete all the layers within that layer set.

// Get all layers in a layerset and call removeAll()
docRef.layerSets.getByName(‘TEXT’).layers.removeAll()

Show/hide the layer & group layer

// Hide the layer
var docRef = app.activeDocument;

var layerRef = docRef.artLayers.getByName (‘My first layer’);

layerRef.visible=false;

** If we want to show the hide the layer
layerRef.visible=true;

** Instead of artLayers we could use layerSets if we want to show/hide the group layer (layerSets),

Duplicate the layer & group layer

// Duplicate the layer
var docRef = app.activeDocument;

var layerRef = docRef.artLayers.getByName (‘My first layer’);

//duplicate with copy
layerRef.duplicate();

//duplicate with new layer name
layerRef.duplicate().name=’My duplicate layer’;

** Instead of artLayers we could use layerSets if we want to duplicate the group layer (layerSets)