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

Rename layer if it contains text

New Here ,
Jan 04, 2017 Jan 04, 2017

Copy link to clipboard

Copied

Hope someone can help my with there two scripts I started:

#target photoshop

var Name=activeDocument.name

if (Name.indexOf(".") != -1 ) Name=Name.substr(0,Name.lastIndexOf("."));

activeDocument.activeLayer.name="115 x 115 "+ Name +"-115.jpg";

#target photoshop

var Name=activeDocument.name

if (Name.indexOf(".") != -1 ) Name=Name.substr(0,Name.lastIndexOf("."));

activeDocument.activeLayer.name="1366 x 1366 "+ Name +"-1366.jpg";

How can I merge them together and use an if then statement so that IF any layer name contains 115 or 1366 THEN the layer will be renamed as shown in the above script?  I plan on attaching this script to the save command to get it to execute.

Thank you!

TOPICS
Actions and scripting

Views

2.3K

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

Participant , Jan 04, 2017 Jan 04, 2017

Hey toddr44415872​, try this:

var doc = app.activeDocument;

var Name = doc.name;

if( Name.indexOf('.') != -1 ) {

     Name = Name.substr( 0, Name.lastIndexOf('.') );

}

var docLayerSets = getLayerSets( doc.layerSets, [] );

for( var ii = 0; ii < docLayerSets.length; ii++ ) {

     var layerName = docLayerSets[ii].name;

     if( layerName.indexOf('115') != -1 ) {

          docLayerSets[ii].name = '115 x 115 ' + Name + '-115.jpg';

     } else if ( layerName.indexOf('1366') != -1 ) {

          docLayerSets[ii].na

...

Votes

Translate

Translate
Adobe
LEGEND ,
Jan 04, 2017 Jan 04, 2017

Copy link to clipboard

Copied

For the beginning:

#target photoshop

if (nme = (aDN = (aD = activeDocument).name).match(/.*115.*(?=\.)/)) {

  aD.activeLayer.name ="115 x 115 " + nme +"-115.jpg";

}

if (nme = aDN.match(/.*1366.*(?=\.)/)) {

  aD.activeLayer.name ="1366 x 1366 " + nme +"-1366.jpg";

}

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
New Here ,
Jan 04, 2017 Jan 04, 2017

Copy link to clipboard

Copied

Thank you!  I have tried this script and I am not getting anything to happen.  Sorry to bother but should there be a loop somewhere that checks all of the layer group names?  Thanks again.

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 ,
Jan 04, 2017 Jan 04, 2017

Copy link to clipboard

Copied

Please show us at first one sample file (or screenshot) with visible layers palette and some layer names. And one example with layer name before and one after running the script.

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
New Here ,
Jan 04, 2017 Jan 04, 2017

Copy link to clipboard

Copied

Thank you! Please see current workflow video attached.  Problem is that if filename changes, layer names won't change unless I remember to run the scripts.  Dropbox - 2017-01-04_13-38-24.mp4

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 ,
Jan 04, 2017 Jan 04, 2017

Copy link to clipboard

Copied

Thx for the video. But I can't see enough details of the image name and the layers names

IdidntSee.png

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
New Here ,
Jan 04, 2017 Jan 04, 2017

Copy link to clipboard

Copied

Hope this is clearer for everyone.

2017 01 04 14 05 44 - YouTube

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
LEGEND ,
Jan 04, 2017 Jan 04, 2017

Copy link to clipboard

Copied

It's not about video, I saw enough before with my inner focus I hope I don't have to send you to mind reader, I even don't know any who can so, so be back when you finish better description and I promise to write some script back

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
New Here ,
Jan 04, 2017 Jan 04, 2017

Copy link to clipboard

Copied

Sorry for the confusion!

Current workflow:

1. Click on layer group to be renamed

2. Go to File>Scripts and choose 115 Layer Rename.

3. This script we be executed:

#target photoshop

var Name=activeDocument.name

if (Name.indexOf(".") != -1 ) Name=Name.substr(0,Name.lastIndexOf("."));

activeDocument.activeLayer.name="115 x 115 "+ Name +"-115.jpg";

Preferred workflow:

1. Go to File>Scripts and choose "Layer Rename"

2. Script will search thru all layer group names.

a. If it contains any text with 115 then this script is exucuted:

#target photoshop

var Name=activeDocument.name

if (Name.indexOf(".") != -1 ) Name=Name.substr(0,Name.lastIndexOf("."));

activeDocument.activeLayer.name="115 x 115 "+ Name +"-115.jpg";

b. If it contains any text with 1366 then this script is exucuted:

#target photoshop

var Name=activeDocument.name

if (Name.indexOf(".") != -1 ) Name=Name.substr(0,Name.lastIndexOf("."));

activeDocument.activeLayer.name="1366 x 1366 "+ Name +"-1366.jpg";

Hope this is clear!

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
LEGEND ,
Jan 04, 2017 Jan 04, 2017

Copy link to clipboard

Copied

Now I won't write it - I'm close to leave work, but in 1,5 hour why not - it's enough clear now.

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
Participant ,
Jan 04, 2017 Jan 04, 2017

Copy link to clipboard

Copied

Hey toddr44415872​, try this:

var doc = app.activeDocument;

var Name = doc.name;

if( Name.indexOf('.') != -1 ) {

     Name = Name.substr( 0, Name.lastIndexOf('.') );

}

var docLayerSets = getLayerSets( doc.layerSets, [] );

for( var ii = 0; ii < docLayerSets.length; ii++ ) {

     var layerName = docLayerSets[ii].name;

     if( layerName.indexOf('115') != -1 ) {

          docLayerSets[ii].name = '115 x 115 ' + Name + '-115.jpg';

     } else if ( layerName.indexOf('1366') != -1 ) {

          docLayerSets[ii].name = '1366 x 1366 ' + Name + '-1366.jpg';

     }

}

// getLayerSets - recursive function to get all layer sets in current document

function getLayerSets( docLayers, sets ) {

     for( var i = 0; i < docLayers.length; i++ ) {

          if( docLayers.typename == 'LayerSet' ) {

               sets.push( docLayers );

               sets = getLayerSets( docLayers.layers, sets );

          }

     }

     return sets;

}

If you have a lot of layers in your documents, you could improve performance by using ActionManager code. If you only have a few, this should be fine.

Hope it helps!

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 ,
Jan 04, 2017 Jan 04, 2017

Copy link to clipboard

Copied

Hi JavierAroche​,

nice try.

Do you really need the two if clauses?

How about to replace line 13 -17 in your code with

var checkpart; if ((checkpart = layerName.match(/115|1366/)) != null)

{docLayerSets[ii].name = checkpart[0] + ' x ' + checkpart[0] + ' ' + Name + '-' + checkpart[0] + '.jpg';}

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
Participant ,
Jan 04, 2017 Jan 04, 2017

Copy link to clipboard

Copied

Hey pixxxel schubser​, nope you don't necessarily need both if clauses. Luckily, there's a million ways to script this. I wrote this using Todd's logic, so he can follow along and see where I'm using his code. I also did it so he could understand how to add another variable, if he ever needs to. I know that helped me when I started learning JavaScript

Also, he asked about how to set an if statement, so I thought it'd be useful to show how to keep stacking them in pretty basic JavaScript. If Todd knows more than basic scripting, then he'll be able to improve the code without a problem.

Btw, I just tested both options (double if / regex match) and they both take around 12.2 seconds on a document with 70 groups. My code just looks a little longer because I like to have separate lines of code for variables, if's, functions, etc... but you could just have all ifs in 1 line

Like I said, if you have more complicated documents with a lot of groups, you might want to try doing it with ActionManager code.

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
LEGEND ,
Jan 04, 2017 Jan 04, 2017

Copy link to clipboard

Copied

Just shorter version without hard-level recursion, but simple loop on Action Manager code:

function sze() {

  if ((dln = (aL = (aD = activeDocument).activeLayer).name).match(/115/)) {

  aL.name ="115 x 115 " + aD.name.match(/.*(?=\.)/ )+"-115.jpg";

  }

  if (nme = dln.match(/1366/)) {

  aL.name ="1366 x 1366 " + aD.name.match(/.*(?=\.)/ )+"-1366.jpg";

  }

}

function cTT(v) {return charIDToTypeID(v)}; function sTT(v) {return stringIDToTypeID(v)}

for (i = 1;; i++) {

  (ref1 = new ActionReference()).putIndex(sTT('layer'), i),

  (dsc1 = new ActionDescriptor()).putReference(sTT('null'), ref1)

  dsc1.putBoolean(sTT('makeVisible'), false)

  try{executeAction(sTT('select'), dsc1, DialogModes.NO)} catch (err) {break}

  if (String(activeDocument.activeLayer).slice(1, 9) == 'LayerSet') sze()

}

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
New Here ,
Jan 05, 2017 Jan 05, 2017

Copy link to clipboard

Copied

LATEST

Thank you everyone!!

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
LEGEND ,
Jan 04, 2017 Jan 04, 2017

Copy link to clipboard

Copied

It's still not enough clear to me, so let me ask another qestion.

Let's say you got some file contianing groups and layers. Only groups (not layers as I thought before, because video shows differently) are named 115 or 1366, right?

The file with these groups and layers is named somehow, but meantime for some reason you change its name and you want that at same time or after running specific script only groups with similar name to document name were changed.

For example if there are 115 group and 1366 group and you changed name of your file to name cointaining 115 you want all groups with name 115 get changed to appriopae name while those with 1366 left untouched, right?

When I'm home today I download this video because I can't see it watching it live well, anyway the main qestion is what I described above, so please be more precise because in first post you didn't mention anything about looping and groups, and later you do, the best will be if you point evertying, step by step, thx)

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
LEGEND ,
Jan 04, 2017 Jan 04, 2017

Copy link to clipboard

Copied

If you just create a new document with some name like "115" then this script won't be working, but when you save that new document with name for ex, aaaaa115aaaaa.jpg and then run the script it'll change name of active Layer to "115 x 115 115-115.jpg".

It's not so difficult to write more advanced script to loop over all layers in a document. It has sense as far there is only one layer beetwen others with name cointaining 115 or 1366 number. So if you don't plan to have one document with at least 2 layers where name of one of them contain 115 and second 1366 I can write a script it's going to loop all layers searching for that one. The question is are you going to have there also groups, i mean layerSets in your document, or only layers. And if layerSets also will there searched for layer inside one of them, or there always layer with 115 or  1366 name having no parent (so no beeing in any group)?

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