Parsing layer names

Find out why the . goes before the /

Moderator: Paul Tuersley

Post Reply
bkan
Posts: 51
Joined: November 6th, 2013, 8:33 am

Hello,
I would want to parse all the layer names of the active comp, read these names and compare it to the string of a variable in my code. If one of the layer name is the same name than my variable, I would like to launch a pop up in after effects, with a question tag like "Do you want to?" with 2 answers (yes or no). Then, depending of the answer, I would like to launch a function. I have no idea on how do that ! (I'm quite a beginner, sorry!). Any idea? Thanks in advance (sorry for my english, I'm french!)
beginUndoGroup
Posts: 81
Joined: November 27th, 2012, 6:41 am

Assuming that a variable comp exists that refers to an actual composition somewhere in your project,
and another variable theName has also be declared (a string, the name to be searched), this should do it:

Code: Select all

var k=1, K=comp.numLayers, layer;
for (	; k<=K; k++) if (comp.layer(k).name === theName) {layer = comp.layer(k);break;};
if (layer instanceof Layer && confirm("Do you want to do this?"))
{
	// do something
	};
This will process only the first occurrence only. If you think there are several layers with the same name and you want to process all of them,
you shouldnt break the loop:

Code: Select all

var k=1, K=comp.numLayers, layer;
for (	; k<=K; k++) if (comp.layer(k).name === theName)
{
	layer = comp.layer(k);
	if (confirm("Layer("+k+") has the desired name. Do you want to process it?"))
	{
		// do something
		};
	};
confirm() is a built-in ExtendScript function. I think it can accept more arguments. See more details in the ExtendScript Tools Guide.

Xavier.
Post Reply