Copy link to clipboard
Copied
Working on a function that checks wether the selected layer has a specific effect applied to it. Here's a simplified version of it:
function hasTransformEffect(layer) {
for (i = 1; i <= layer.Effects.numProperties; i++) {
alert(i);
}
}
alert(hasTransformEffect(app.project.activeItem.selectedLayers[0]));
When running this, I always end up with an undefined after it's finished looping. Here's a video of me trying this https://streamable.com/bcg48
Few things I have tried:
- Using a try/catch to bypass it
- Using an if statement to check wether numProperties > 0 before running the loop
- Running it from Extendscript IDE
- Running it from a saved .jsx file
- Running it on a different machine
- Running it on a different version of AE
Wondering what I'm missing, I'm assuming it's something about numProperties that I don't know. Any help is greatly appreciated!
Hugo
Your alert outside the function is reporting what the function is returning, but the function isn't returning anything.
function hasTransformEffect(layer) {
for (i = 1; i <= layer.Effects.numProperties; i++) {
if (layer.Effects.property(i).name == "Transform") return true;
}
return false;
}
alert(hasTransformEffect(app.project.activeItem.selectedLayers[0]));
Copy link to clipboard
Copied
Your alert outside the function is reporting what the function is returning, but the function isn't returning anything.
function hasTransformEffect(layer) {
for (i = 1; i <= layer.Effects.numProperties; i++) {
if (layer.Effects.property(i).name == "Transform") return true;
}
return false;
}
alert(hasTransformEffect(app.project.activeItem.selectedLayers[0]));
Copy link to clipboard
Copied
Thanks for your help Paul, that works!
Could you explain why returning false within the if statement like so still gets me an undefined:
function hasTransformEffect(layer) {
for (i = 1; i <= layer.Effects.numProperties; i++) {
if (layer.Effects.property(i).name == "Transform"){
return true;
} else {
return false;
}
}
}
alert(hasTransformEffect(app.project.activeItem.selectedLayers[0]));
Just trying to get my head around this for future reference
Thanks again
Copy link to clipboard
Copied
Not at my computer to test right now. Would think it would return false but it’s wrong anyway. This will return after the first effect whether found Transform or not. My example only returned false if you got through full for loop without finding a match.
Copy link to clipboard
Copied
Looking at it again, my guess is it's coming up undefined if there are no effects. No effects means numProperties equals 0 so it will never go into the for loop where you now have it returning false. i.e. it will return nothing if there are no effects, true if the first effect is called Transform, otherwise false.
Copy link to clipboard
Copied
Ah yes, that makes sense.
Thanks for your help, really appreciate it!