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

Looping through numProperties always ends with error: undefined

New Here ,
Jun 24, 2019 Jun 24, 2019

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

TOPICS
Scripting

Views

549

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 24, 2019 Jun 24, 2019

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

Votes

Translate

Translate
Enthusiast ,
Jun 24, 2019 Jun 24, 2019

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

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 ,
Jun 24, 2019 Jun 24, 2019

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

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 24, 2019 Jun 24, 2019

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.

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 24, 2019 Jun 24, 2019

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.

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 ,
Jun 25, 2019 Jun 25, 2019

Copy link to clipboard

Copied

LATEST

Ah yes, that makes sense.

Thanks for your help, really appreciate it!

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