Skip to main content
October 23, 2019
Answered

Script to Find layer names that include CMYK (non-case specific i.e. CMyK)

  • October 23, 2019
  • 3 replies
  • 832 views

Hi all,

I have a script that will find layers names that contains "CMYK", but I want it to be able to also find them when they contain any variation of case structure i.e. "cMYk", no matter upper or lower case. It's easy when using InDesign's Find/Change, you just add (?i) to the start of .*CMYK.* and it finds them, but it won't within my script.

Can someone please tell me why and how to change it so it works.

 

for ( count = 0; count<app.activeDocument.layers.length; count++)
{ layer = app.activeDocument.layers.item(count).name; // Create variable from Layer name
this["Layer"+(count)] = layer // Creates individual variable from Layer name and number (i.e. Layer0 = CMYK)
if (layer.replace( RegExp("(?i).*CMYK.*") , "CMYK" ) == "CMYK") {alert(count+": "+layer)} // If the layer includes "CMyK" (not case specific) then it doesn't work

}

 

Thanks for any help, Bren

This topic has been closed for replies.
Correct answer Manan Joshi

Hi Bren,

 

If you just need to test for the presence of the text CMYK(irrespective of its case) in your layer name, you can do it in two ways

1. Convert the whole string into lowercase and then match it using a regex like .*cmyk.*

2. Since ?i is not working, you could use it as a flag in the constructor of the RegExp that you are creating and then it seems to be working

 

Also there is no need to even use the replace method, just using the match method of the string or test method of the RegExp object will suffice and should be faster

Try any one of the following if statements in place of the if statement that you have

 

 

if (layer.match( RegExp(".*CMYK.*", "i")))
if(RegExp(".*CMYK.*", "i").test(layer))

 

 

 

-Manan 

3 replies

Community Expert
October 24, 2019

Hi Bren,

understood. Thought you'd like to replace all instances of CMYK, lower or upper case or mixed case with "CMYK" to "normalize" the layer names.

 

Regards,
Uwe Laubender

( ACP )

Community Expert
October 23, 2019

Hi Bren,

it's easier than you might think. Watch the modifiers g and i :

 

var layers = app.documents[0].layers.everyItem().getElements();

for( var n=0; n<layers.length; n++ )
{
	var layerName = layers[n].name;
	var newLayerName = layerName.replace(/CMYK/gi , "CMYK" );
	layers[n].name = newLayerName;
};

Every instance of "CMYK" in a layer name, e.g. "cmyK" will be replaced by "CMYK".

Even if "cmyk" is used more than one time in the same name.

 

Regards,
Uwe Laubender

( ACP )

October 24, 2019

Hi Uwe,

 

Unfortunately, for this script I don't want to replace the names, I only need to read them.

BUT... that was my next mission, to be able to find various case structures and change them to a set structure so this will be a great start for that  🙂

 

Thanks, Bren

Manan JoshiCommunity ExpertCorrect answer
Community Expert
October 23, 2019

Hi Bren,

 

If you just need to test for the presence of the text CMYK(irrespective of its case) in your layer name, you can do it in two ways

1. Convert the whole string into lowercase and then match it using a regex like .*cmyk.*

2. Since ?i is not working, you could use it as a flag in the constructor of the RegExp that you are creating and then it seems to be working

 

Also there is no need to even use the replace method, just using the match method of the string or test method of the RegExp object will suffice and should be faster

Try any one of the following if statements in place of the if statement that you have

 

 

if (layer.match( RegExp(".*CMYK.*", "i")))
if(RegExp(".*CMYK.*", "i").test(layer))

 

 

 

-Manan 

-Manan
October 24, 2019

Hi Manan,

 

Line 1 wouldn't work, it comes up with "Layer Test is not a function".

Line 2 however worked perfectly, thank you, that's so much easier  🙂  **********

I would like to mark your comment as correct but since the forum has changed I didn't realise this wasn't automatically classed as a question so I can't, I will have to remember that for future questions.

 

Thanks for your help, Bren

Community Expert
October 24, 2019

Hi Bren,

 

I have edited the answer so that the first if statement should also work, it had a typo, the test method is present on RegExp object, for string we need to use the match method

 

-Manan

-Manan