• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
1

generate sequential number level

Engaged ,
Dec 11, 2023 Dec 11, 2023

Copy link to clipboard

Copied

 I work with many levels and I would like it to generate a progressive number for each level I create. for example, if the layer is called face, the next layer will be called face 1, the other layer face 2, and then onwards. I hope for help.

TOPICS
Actions and scripting

Views

173

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Dec 11, 2023 Dec 11, 2023

Select all layers with the same layer name.

 

Run this script. Looking at the code, I think that it came from Paul Riggott. I have updated the code to use the current user interface colour scheme and to auto-populate the replace field with the top selected layer name + space:

 

#target photoshop
app.bringToFront();
main();
function main(){
if(!documents.length) return;
var selectedLayers = getSelectedLayersIdx();
var win = new Window( 'dialog', '' ); 
g = win.graphics;
var myBrush = g.newBrush(g
...

Votes

Translate

Translate
Adobe
Participant ,
Dec 11, 2023 Dec 11, 2023

Copy link to clipboard

Copied

This does the job:

var c = 0;
var layName = "";

iterLayers(app.activeDocument);

function iterLayers(l) {
    for(var i=0; i<l.layers.length; i++) {
        var curLay = l.layers[i];
        app.activeDocument.activeLayer = curLay;
        
        if(layName == "") {
            layName = curLay.name;
        }

        if(curLay.typename == "LayerSet") {
            iterLayers(curLay);
        } else {
            if(c != 0) {
                curLay.name = layName + " " + c;
            }
            c++;
        }
    }
}
____________________
Robotic Process Automation in Desktop Publishing (Book): https://doi.org/10.1007/978-3-658-39375-5

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 11, 2023 Dec 11, 2023

Copy link to clipboard

Copied

@Ciccillotto 


What is your expectation?

 

Are you looking for a script to rename selected layers? For example, let's say that you had 4 layers selected that all were called "face".... Would the script rename them as: face, face 1, face 2, face 3 

 

Or do you want a script to make a new layer based on the current single active layer name? So if the current layer was named face 2, then running the script would create a new layer named face 3

 

Are these always  standard art layers, or adjustment or groups or other kinds?

 

Can you post a screenshot of your layer panel so a better understanding of the structure can be as ascertained?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 11, 2023 Dec 11, 2023

Copy link to clipboard

Copied

Stephen the layer with the name face exists, I would like to create a button that only adds the sequential number to the layer. I hope I have explained myself. @GNDGN Now I'm not at home I'll try it tomorrow and let you know, anyway thanks for the help

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 11, 2023 Dec 11, 2023

Copy link to clipboard

Copied

quote

Stephen the layer with the name face exists, I would like to create a button that only adds the sequential number to the layer. I hope I have explained myself. 


By @Ciccillotto

 

With only a single layer selected, I don't know how Photoshop would know what sequential number to apply... Short of using the  crystalBall(); function – the script would need to loop over all the layers, get the layer names, find all matching layers and what the last number was in the matching layer, and then increment the number.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 11, 2023 Dec 11, 2023

Copy link to clipboard

Copied

Select all layers with the same layer name.

 

Run this script. Looking at the code, I think that it came from Paul Riggott. I have updated the code to use the current user interface colour scheme and to auto-populate the replace field with the top selected layer name + space:

 

#target photoshop
app.bringToFront();
main();
function main(){
if(!documents.length) return;
var selectedLayers = getSelectedLayersIdx();
var win = new Window( 'dialog', '' ); 
g = win.graphics;
var myBrush = g.newBrush(g.BrushType.THEME_COLOR, "appDialogBackground");
g.backgroundColor = myBrush;
win.orientation='stack';
win.p1= win.add("panel", undefined, undefined, {borderStyle:"black"}); 
win.g1 = win.p1.add('group');
win.g1.orientation = "row";
win.title = win.g1.add('statictext',undefined,'Rename Layers');
win.title.alignment="fill";
var g = win.title.graphics;
g.font = ScriptUI.newFont("Arial","BOLDITALIC",22);
win.g5 =win.p1.add('group');
win.g5.orientation = "column";
win.g5.alignChildren='left';
win.g5.spacing=10;
win.g5.st1 = win.g5.add('statictext',undefined,'New layer name');
win.g5.et1 = win.g5.add('edittext', undefined, activeDocument.activeLayer.name + ' '); 
win.g5.et1.preferredSize=[250,20];
win.g10 =win.p1.add('group');
win.g10.orientation = "row";
win.g10.alignment='fill';
win.g10.spacing=10;
win.g10.st1 = win.g10.add('statictext',undefined,'Serial Number'); 
win.g10.et1 = win.g10.add('edittext',undefined,'1');
win.g10.et1.preferredSize=[50,20];
win.g10.et1.onChanging = function() { 
  if (this.text.match(/[^\-\.\d]/)) { 
    this.text = this.text.replace(/[^\-\.\d]/g, ''); 
  } 
};
win.g10.st1 = win.g10.add('statictext',undefined,'Length'); 
var nums=[2,3,4,5];
win.g10.dl1 = win.g10.add('dropdownlist',undefined,nums);
win.g10.dl1.selection=0;
win.g15 =win.p1.add('group');
win.g15.orientation = "row";
win.g15.alignment='fill';
win.g15.cb1 = win.g15.add('checkbox',undefined,'Reverse layer order');
win.g100 =win.p1.add('group');
win.g100.orientation = "row";
win.g100.alignment='center';
win.g100.spacing=10;
win.g100.bu1 = win.g100.add('button',undefined,'Rename');
win.g100.bu1.preferredSize=[120,30];
win.g100.bu2 = win.g100.add('button',undefined,'Cancel');
win.g100.bu2.preferredSize=[120,30];
win.g100.bu1.onClick=function(){
if(win.g5.et1.text == ''){
    alert("No layer name has been entered!");
    return;
    }
win.close(0);
if(win.g15.cb1.value) selectedLayers.reverse();
for(var a in selectedLayers){
    var LayerName = win.g5.et1.text + zeroPad((Number(win.g10.et1.text)+Number(a)), Number(win.g10.dl1.selection.text));
    putLayerNameByIndex(Number(selectedLayers[a]), LayerName );
    }
}
win.center();
win.show(); 
}
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" ))); 
         } 
     var vis = app.activeDocument.activeLayer.visible;
        if(vis == true) app.activeDocument.activeLayer.visible = false;
        var desc9 = new ActionDescriptor();
    var list9 = new ActionList();
    var ref9 = new ActionReference();
    ref9.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
    list9.putReference( ref9 );
    desc9.putList( charIDToTypeID('null'), list9 );
    executeAction( charIDToTypeID('Shw '), desc9, DialogModes.NO );
    if(app.activeDocument.activeLayer.visible == false) selectedLayers.shift();
        app.activeDocument.activeLayer.visible = vis;
      } 
      return selectedLayers; 
};
function zeroPad(n, s) { 
n = n.toString(); 
while (n.length < s) n = '0' + n; 
return n; 
};
function putLayerNameByIndex( idx, name ) {
     if( idx == 0 ) return;
    var desc = new ActionDescriptor();
        var ref = new ActionReference();
        ref.putIndex( charIDToTypeID( 'Lyr ' ), idx );
    desc.putReference( charIDToTypeID('null'), ref );
        var nameDesc = new ActionDescriptor();
        nameDesc.putString( charIDToTypeID('Nm  '), name );
    desc.putObject( charIDToTypeID('T   '), charIDToTypeID('Lyr '), nameDesc );
    executeAction( charIDToTypeID( 'slct' ), desc, DialogModes.NO ); 
    executeAction( charIDToTypeID('setd'), desc, DialogModes.NO );
}

 

https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 12, 2023 Dec 12, 2023

Copy link to clipboard

Copied

LATEST

This is fine for what I need to do,
Stephen Thanks

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines