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

Prefix layer names

Explorer ,
Jun 05, 2015 Jun 05, 2015

Copy link to clipboard

Copied

Hi, I'm not very good at java scripts so I was wondering if you could help.

I'd like to be able to select a bunch of layers in photoshop, and run a script that would prefix all the layers names with whatever I choose in the script.

I don't want any UI, I just want it to add the prefix and be done, no questions asked. I'll edit the script to modify the exact prefix text. It has to only affect selected layers OR the layers within a selected folder.

I know it's probably stupid simple.

Thanks in advance.

Ian

TOPICS
Actions and scripting

Views

915

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

Enthusiast , Jun 05, 2015 Jun 05, 2015

Yup simple...

#target photoshop;

app.bringToFront();

if(documents.length){

var prefix = "my new prefix-"

var selLayers = getSelectedLayersIdx();

for (var a in selLayers){

    selectLayerByIndex(selLayers);

    activeDocument.activeLayer.name = prefix + activeDocument.activeLayer.name;

    }

}

function selectLayerByIndex(index,add){

var ref = new ActionReference();

ref.putIndex(charIDToTypeID("Lyr "), index);

var desc = new ActionDescriptor();

desc.putReference(charIDToTypeID("null"), ref );

if(

...

Votes

Translate

Translate
Adobe
Enthusiast ,
Jun 05, 2015 Jun 05, 2015

Copy link to clipboard

Copied

Yup simple...

#target photoshop;

app.bringToFront();

if(documents.length){

var prefix = "my new prefix-"

var selLayers = getSelectedLayersIdx();

for (var a in selLayers){

    selectLayerByIndex(selLayers);

    activeDocument.activeLayer.name = prefix + activeDocument.activeLayer.name;

    }

}

function selectLayerByIndex(index,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){}

};

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;

};

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
Explorer ,
Jun 08, 2015 Jun 08, 2015

Copy link to clipboard

Copied

Super helpful!

More complicated of a script than I though it would be.

If you get a min, can you point me in the direction of a script that will remove "copy" without a UI?

Many thanks!

Ian

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 ,
Jun 08, 2015 Jun 08, 2015

Copy link to clipboard

Copied

Moving to Photoshop Scripting Forum.

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
Enthusiast ,
Jun 08, 2015 Jun 08, 2015

Copy link to clipboard

Copied

Here you are.

#target photoshop;

app.bringToFront();

if(documents.length) main();

function main(){

   var ref = new ActionReference();

   ref.putEnumerated( charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );

   var count = executeActionGet(ref).getInteger(charIDToTypeID('NmbL')) +1;

try{

    activeDocument.backgroundLayer;

var i = 0; }catch(e){ var i = 1; };

   for(i;i<count;i++){

       if(i == 0) continue;

        ref = new ActionReference();

        ref.putIndex( charIDToTypeID( 'Lyr ' ), i );

        var desc = executeActionGet(ref);

        var layerName = desc.getString(charIDToTypeID( 'Nm  ' ));

        if(layerName.match(/copy/gi)) putLayerNameByIndex( i, layerName.replace(/copy/gi,'').replace(/\s+/g,' ') );

   };

};

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

};

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
Explorer ,
Jun 08, 2015 Jun 08, 2015

Copy link to clipboard

Copied

LATEST

Perfect! Worked great!

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