Page 2 of 2

rename layer comps

Posted: Sun Mar 15, 2015 10:45 am
by pfaffenbichler
It’s a potentially time-consuming work-around to duplicate the image in order to determine the selected LayerComps but creating a Snapshot or making sure the HistoryStates number is set high enough seem a bother to me, so if someone wants to give it a try here is some code:
Code: Select all// determine selected layercomps;
// 2015, use it at your own risk;
#target "photoshop-70.032"
if (app.documents.length > 0) {
   alert (getSelectedLayerComps().join("\n"))
   };
////////////////////////////////////
////// determine selected layer comps //////
function getSelectedLayerComps () {
////////////////////////////////////
var theComps = app.activeDocument.layerComps;
var theNumber = theComps.length;
////////////////////////////////////
if (theNumber > 0) {
var myDocument = app.activeDocument.duplicate("thecopy", false);
var theComps2 = app.activeDocument.layerComps;
////////////////////////////////////
for (var m  = 0; m < theNumber; m++) {
theComps2[m].name = String(m);
};
////////////////////////////////////
deleteLayerComps ();
try {
////////////////////////////////////
var theMissing = new Array;
var theCounter = 0;
// check against old list;
for (var n = 0; n < theNumber; n++) {
if (theCounter < myDocument.layerComps.length) {
if (Number(myDocument.layerComps[theCounter].name) != n) {theMissing.push(theComps[n])}
else {theCounter ++}
} else {theMissing.push(theComps[n])};
};
////////////////////////////////////
} catch (e) {myDocument.close(SaveOptions.DONOTSAVECHANGES)};
};
try {myDocument.close(SaveOptions.DONOTSAVECHANGES)} catch (e) {};
return (theMissing)
};
////// delete layer comps //////
function deleteLayerComps () {
try {
// =======================================================
var idDlt = charIDToTypeID( "Dlt " );
    var desc2 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref1 = new ActionReference();
        var idcompsClass = stringIDToTypeID( "compsClass" );
        var idOrdn = charIDToTypeID( "Ordn" );
        var idTrgt = charIDToTypeID( "Trgt" );
        ref1.putEnumerated( idcompsClass, idOrdn, idTrgt );
    desc2.putReference( idnull, ref1 );
executeAction( idDlt, desc2, DialogModes.NO );
} catch (e) {}
};

////// rename layer comp //////
function nameLayerComp () {
// =======================================================
var idsetd = charIDToTypeID( "setd" );
    var desc1 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref1 = new ActionReference();
        var idcompsClass = stringIDToTypeID( "compsClass" );
        ref1.putName( idcompsClass, "2" );
    desc1.putReference( idnull, ref1 );
    var idT = charIDToTypeID( "T   " );
        var desc2 = new ActionDescriptor();
        var iduseVisibility = stringIDToTypeID( "useVisibility" );
        desc2.putBoolean( iduseVisibility, true );
        var idusePosition = stringIDToTypeID( "usePosition" );
        desc2.putBoolean( idusePosition, true );
        var iduseAppearance = stringIDToTypeID( "useAppearance" );
        desc2.putBoolean( iduseAppearance, true );
        var idTtl = charIDToTypeID( "Ttl " );
        desc2.putString( idTtl, """22""" );
    var idcompsClass = stringIDToTypeID( "compsClass" );
    desc1.putObject( idT, idcompsClass, desc2 );
executeAction( idsetd, desc1, DialogModes.NO );
};

Re: rename layer comps

Posted: Wed Oct 18, 2017 11:51 pm
by pwu
Here you go:

Code: Select all

#target photoshop

if (app.documents.length > 0) {

var doc = app.activeDocument;
var selected = [];

var findReplaceString = prompt("Find:Replace\nRenames selected Layer Comps.\nIf none are selected, all get processed.\n\n<< CASE Sensitive >>\nUse ' : ' as a separator.", "CHANGE THIS:TO THIS").split(":");

//Gather selected layer comps, if none selected, set collection to all layer comps
for (var m = 0; m < doc.layerComps.length; m++) {
if (doc.layerComps[m].selected == true) {
selected.push(doc.layerComps[m]);
}
}

if (selected.length < 1) selected = doc.layerComps;

for (var m = 0; m < selected.length; m++) {
var CompName = selected[m].name.replace(findReplaceString[0], findReplaceString[1]);
selected[m].name = CompName;
}
};

Re: rename layer comps

Posted: Wed Feb 20, 2019 12:55 am
by blastframe
Here is my version. It renames the active document's layer comps by a prefix of your choice and the index.

Code: Select all


#target photoshop

function renameLayerComps()
{
var activeDoc = app.activeDocument;
comps = activeDoc.layerComps;

function pad(n, width, z)
{
z = z || '0';
n = n + '';
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}

var dialog = "dialog { \
text: 'Batch Rename Layer Comps', \
destination: Panel { \
alignment: 'left', \
alignChildren:['left','top'], \
orientation: 'row', \
text: 'Layer Comp Name Prefix', \
input: EditText { text: 'Layer_Comp_', preferredSize: [230,20] }, \
}, \
rename: Group { \
alignment: 'right', \
orientation: 'row', \
zeroPad: Checkbox { text: 'Pad with leading zero (e.g. 01, 02, etc.)', value: false }, \
}, \
buttons: Group { \
alignment: 'left', \
orientation: 'row', \
rename: Button { text: 'Rename', properties:{ name: 'ok' } }, \
cancel: Button { text: 'Cancel' }, \
} \ }";

dlg = new Window( dialog );

dlg.buttons.rename.onClick = function() {
prefix=dlg.destination.input.text;
zeroPad=dlg.rename.zeroPad.value;
rename(prefix,zeroPad);
dlg.close();
}

dlg.buttons.cancel.onClick = function() {
dlg.close();
}

dlg.show();

function rename(prefix, zeroPad)
{
for (var i=0;i<comps.length;i++)
{
indexNumber = i+1;
if (i<10 && zeroPad)
{
indexNumber = pad(i+1, 2);
}
comps[i].name = prefix + indexNumber;
}
}
}

renameLayerComps();