rename layer comps

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: xbytor, DavideBarranca, Paul MR

pfaffenbichler

rename layer comps

Postby pfaffenbichler » Sun Mar 15, 2015 10:45 am

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 );
};



pwu
Posts: 0
Joined: Wed Oct 18, 2017 11:38 pm

Re: rename layer comps

Postby pwu » Wed Oct 18, 2017 11:51 pm

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;
        }
};


Return to “Help Me”

Who is online

Users browsing this forum: No registered users and 1 guest