Skip to content

Instantly share code, notes, and snippets.

@MarshySwamp
Last active March 18, 2022 22:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MarshySwamp/229a0676666a44cdaa007da1cfddc0b7 to your computer and use it in GitHub Desktop.
Save MarshySwamp/229a0676666a44cdaa007da1cfddc0b7 to your computer and use it in GitHub Desktop.
Various snippets for selecting a single Photoshop layer
/* Presuming that a layer has a unique name (otherwise the first layer with the name will be targeted) */
var selectLayer1 = app.activeDocument.artLayers.getByName("Layer 1");
app.activeDocument.activeLayer = selectLayer1;
/* or */
app.activeDocument.activeLayer = app.activeDocument.layers["Layer 1"];
/* The layers collection can also be used, indexing starts at zero, so the 8th layer from the top would be 7 */
var selectLayer = app.activeDocument.layers[7];
app.activeDocument.activeLayer = selectLayer;
/* or */
app.activeDocument.activeLayer = app.activeDocument.layers[7];
/* Select layer via static id value */
selectLayerById(1); // Enter layer.id number
function selectLayerById(id)
/* https://graphicdesign.stackexchange.com/questions/130739/photoshop-scripting-applying-changes-only-to-selected-artboards */
{
var desc1 = new ActionDescriptor();
var ref1 = new ActionReference();
ref1.putIdentifier(charIDToTypeID('Lyr '), id);
desc1.putReference(charIDToTypeID('null'), ref1);
executeAction(charIDToTypeID('slct'), desc1, DialogModes.NO);
}
/* Select layer via index value, Background layer index starts at 0 - no Background starts at 1 */
selectLayerByIndex(8);
function selectLayerByIndex(index) {
/* https://github.com/ES-Collection/Photoshop-Scripts/blob/master/Remove%20Unused%20Layers.jsx */
var ref = new ActionReference();
ref.putIndex(charIDToTypeID("Lyr "), index);
var desc = new ActionDescriptor();
desc.putReference(charIDToTypeID("null"), ref );
desc.putBoolean( charIDToTypeID( "MkVs" ), false );
executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );
}
/* Select the first layer [0] inside the second layer set [1] */
app.activeDocument.activeLayer = app.activeDocument.layerSets[1].layers[0];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment