Add Suffix to Selected Layer Name

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: Tom, Kukurykus

mycort

Add Suffix to Selected Layer Name

Post by mycort »

Can someone help me write a small and hopefully easy script. Here's what I need:

1) I'd like to select one or multiple layers
2) Run the script
3) Script add's a ".png+@2x" suffix to the end of each layer name that is selected.

thx alot

Professional AI Audio Generation within Adobe Premiere Pro - Download Free Plugin here

pfaffenbichler

Add Suffix to Selected Layer Name

Post by pfaffenbichler »

This Script utilizing code by Paul and Mike might work.
Code: Select all// thanks to mike hale and paul mr;
// 2013, use it at your own risk;
#target photoshop
if (app.documents.length > 0) {
var myDocument = app.activeDocument;
var theLayers = GetSelectedLayers();
myDocument.suspendHistory("operation", "main(myDocument)");
};
////////////////////////////////////
function main () {
// do stuff;
for (var n = 0; n < theLayers.length; n++) {
myDocument.activeLayer = theLayers[n];
try {
myDocument.activeLayer.name = myDocument.activeLayer.name + ".png+@2x"
}
catch (e) {}
};
// reselect layers;
var theIndices = new Array;
for (var o = 0; o < theLayers.length; o++) {
   myDocument.activeLayer = theLayers[o];
   theIndices.push(getSelectedLayerID())
   };
for (var p = 0; p < theLayers.length; p++) {
   selectLayerByIndex(theIndices[p], true)
   };
};
// function to get selected layers by paul mr;
function GetSelectedLayers() {
var A=[];
    var desc11 = new ActionDescriptor();
        var ref9 = new ActionReference();
        ref9.putClass( stringIDToTypeID('layerSection') );
    desc11.putReference( charIDToTypeID('null'), ref9 );
        var ref10 = new ActionReference();
        ref10.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
    desc11.putReference( charIDToTypeID('From'), ref10 );
    executeAction( charIDToTypeID('Mk  '), desc11, DialogModes.NO );
var gL = activeDocument.activeLayer.layers;
for(var i=0;i<gL.length;i++){
A.push(gL);
}
executeAction( charIDToTypeID('undo'), undefined, DialogModes.NO );
return A;
};
// by mike hale;
// http://ps-scripts.com/bb/viewtopic.php? ... dex#p14468
function getSelectedLayerID() {
   try{
      activeDocument.backgroundLayer;
      var mod = 1;
   }catch(e){
      var mod = 0
   }
    var ref = new ActionReference();
    ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "ItmI" ));
    ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
   return desc = executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))-mod;
}
// by mike hale, via paul riggott;
// http://forums.adobe.com/message/1944754#1944754
function selectLayerByIndex(index,add){
add = undefined ? add = false:add
var ref = new ActionReference();
    ref.putIndex(charIDToTypeID("Lyr "), index);
    var desc = new ActionDescriptor();
    desc.putReference(charIDToTypeID("null"), ref );
       if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) );
      desc.putBoolean( charIDToTypeID( "MkVs" ), false );
   try{
    executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );
}catch(e){
alert(e.message);
}
};
mycort

Add Suffix to Selected Layer Name

Post by mycort »

Thanks for Paul and Mike for the code snippets................but especially a great thanks to pfaffenbichler for putting this together, it is purely great work that will help me immensely. thanks again!
pfaffenbichler

Add Suffix to Selected Layer Name

Post by pfaffenbichler »

You’re welcome.
mycort

Add Suffix to Selected Layer Name

Post by mycort »

thx again, this somewhat simply task and script will help me alot.

if it's not too much trouble.....just one more thing, is it possible to add this to not just layers but also a folder or a bunch of folders names?

Thx again!
pfaffenbichler

Add Suffix to Selected Layer Name

Post by pfaffenbichler »

Does this work?

Code: Select all// thanks to mike hale and paul mr;
// 2013, use it at your own risk;
#target photoshop
if (app.documents.length > 0) {
var myDocument = app.activeDocument;
myDocument.suspendHistory("operation", "main(myDocument)");
};
////////////////////////////////////
function main () {
var theLayers = getSelectedLayersIdx();
// do stuff;
for (var n = 0; n < theLayers.length; n++) {
selectLayerByIndex(theLayers[n], false);
try {
myDocument.activeLayer.name = myDocument.activeLayer.name + ".png+@2x"
}
catch (e) {}
};
// reselect layers;
for (var p = 0; p < theLayers.length; p++) {
   selectLayerByIndex(theLayers[p], true)
   };
};
// function to get selected layers by paul mr;
function GetSelectedLayers() {
var A=[];
    var desc11 = new ActionDescriptor();
        var ref9 = new ActionReference();
        ref9.putClass( stringIDToTypeID('layerSection') );
    desc11.putReference( charIDToTypeID('null'), ref9 );
        var ref10 = new ActionReference();
        ref10.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
    desc11.putReference( charIDToTypeID('From'), ref10 );
    executeAction( charIDToTypeID('Mk  '), desc11, DialogModes.NO );
var gL = activeDocument.activeLayer.layers;
for(var i=0;i<gL.length;i++){
A.push(gL);
}
executeAction( charIDToTypeID('undo'), undefined, DialogModes.NO );
return A;
};
// by mike hale, via paul riggott;
// http://forums.adobe.com/message/1944754#1944754
function selectLayerByIndex(index,add){
add = undefined ? add = false:add
var ref = new ActionReference();
    ref.putIndex(charIDToTypeID("Lyr "), index);
    var desc = new ActionDescriptor();
    desc.putReference(charIDToTypeID("null"), ref );
       if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) );
      desc.putBoolean( charIDToTypeID( "MkVs" ), false );
   try{
    executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );
}catch(e){
alert(e.message);
}
};
////// by paul mr;
function getSelectedLayersIdx(){
      var selectedLayers = new Array;
      var ref = new ActionReference();
      ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
      var desc = executeActionGet(ref);
      if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){
         desc = desc.getList( stringIDToTypeID( 'targetLayers' ));
          var c = desc.count
          var selectedLayers = new Array();
          for(var i=0;i<c;i++){
            try{
               activeDocument.backgroundLayer;
               selectedLayers.push(  desc.getReference( i ).getIndex() );
            }catch(e){
               selectedLayers.push(  desc.getReference( i ).getIndex()+1 );
            }
          }
       }else{
         var ref = new ActionReference();
         ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "ItmI" ));
         ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
         try{
            activeDocument.backgroundLayer;
            selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))-1);
         }catch(e){
            selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" )));
         }
      }
      return selectedLayers;
};
mycort

Add Suffix to Selected Layer Name

Post by mycort »

Works perfectly, thanks alot man!!!!