Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions [Scripting] What Am I Doing Wrong?

  • [Scripting] What Am I Doing Wrong?

    Posted by Osama Sayegh on March 28, 2016 at 11:52 am

    Hey there,

    I want to add try and catch functions to several expressions. You know it’s painful to add them manually, so I decided to do that via scripting.

    var selComp = app.project.activeItem;
    var numSelLay = selComp.selectedLayers.length;
    var addExprBeg = "try { \r \r ";
    var addExprEnd = ";\r \r} catch(err) { \r \r value; \r \r}";

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

    var numSelProp = selComp.selectedLayers[i].selectedProperties.length;

    for (x = 1; x <= numSelProp; x++) {

    var Express = selComp.selectedLayers[i].selectedProperties[x].expression;

    selComp.selectedLayers[i].selectedProperties[x].expression = addExprBeg + Express + addExprEnd;

    }

    }

    I selected the layers and their properties that I wanted to add the try and catch functions to, and ran this code. It added the try and catch functions to the properties of the selected layer #2 only and didn’t touch the rest of the selected layers. What did I do wrong?

    If you have another way to do what I’m looking for, feel free to post it. I’m open-minded about new ways of doing things.

    Thanks!

    Osama Sayegh replied 10 years, 1 month ago 3 Members · 3 Replies
  • 3 Replies
  • Dan Ebberts

    March 28, 2016 at 5:18 pm

    I see a couple of things. Your array indexing (for selected layers and properties) should start a 0, not 1 (and end with <, not <=). Also, (I’m guessing) changing an expression probably changes what’s selected, so you will probably end up having to fist save all the selected properties into an array, and then process the array.

    Dan

  • Miguel De mendoza

    March 28, 2016 at 5:20 pm

    Short answer: selectedLayers and selectedProperties are arrays , that means that the first element is 0, not 1, is not the same when you are selecting layers through the layerCollection object: layer(1). So you must change your for loops consecuently:


    for (var i = 0; i < numSelLay; i++) {
    ...
    for (var x = 0; x < numSelProp; x++) {
    ...
    }
    }

  • Osama Sayegh

    March 28, 2016 at 6:01 pm

    YAY! Thanks a lot Dan and Miguel! Dunno how I forgot such a fundamental rule of coding 🙂
    Appreciate your time guys!

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy