Skip to content

Instantly share code, notes, and snippets.

@metaphore
Last active May 10, 2020 13:25
Show Gist options
  • Save metaphore/a27c085e0b20d682d8203eca5c6d3985 to your computer and use it in GitHub Desktop.
Save metaphore/a27c085e0b20d682d8203eca5c6d3985 to your computer and use it in GitHub Desktop.
Simple Photoshop script that renames selected layers
#target photoshop
function main() {
if (app.activeDocument == null) {
alert("You should open some document to execute that script" + selectedLayers.length);
return;
}
selectedLayers = getSelectedLayers();
if (selectedLayers.length == 0) {
alert("You should select at least one layer" + selectedLayers.length);
return;
}
var newLayerName = prompt("Rename selected layers to:", selectedLayers[0].name);
if (newLayerName == null) return;
for (var i=0; i<selectedLayers.length;i++) {
selectedLayers[i].name = newLayerName;
}
}
main();
// Get selected layers function from http://stackoverflow.com/a/37107154/3802890
function getSelectedLayers() {
var resultLayers = new Array();
try {
var idGrp = stringIDToTypeID( "groupLayersEvent" );
var descGrp = new ActionDescriptor();
var refGrp = new ActionReference();
refGrp.putEnumerated(charIDToTypeID( "Lyr " ),charIDToTypeID( "Ordn" ),charIDToTypeID( "Trgt" ));
descGrp.putReference(charIDToTypeID( "null" ), refGrp );
executeAction( idGrp, descGrp, DialogModes.NO );
for (var ix=0;ix<app.activeDocument.activeLayer.layers.length;ix++) {
resultLayers.push(app.activeDocument.activeLayer.layers[ix])
}
var id8 = charIDToTypeID( "slct" );
var desc5 = new ActionDescriptor();
var id9 = charIDToTypeID( "null" );
var ref2 = new ActionReference();
var id10 = charIDToTypeID( "HstS" );
var id11 = charIDToTypeID( "Ordn" );
var id12 = charIDToTypeID( "Prvs" );
ref2.putEnumerated( id10, id11, id12 );
desc5.putReference( id9, ref2 );
executeAction( id8, desc5, DialogModes.NO );
} catch (err) {
// Ignore error
}
return resultLayers;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment