Remove All Expressions from Selected Layers

Find out why the . goes before the /

Moderator: Paul Tuersley

Post Reply
CoonKunsen
Posts: 3
Joined: April 25th, 2018, 1:47 am

Hi,

I found a script online which removes all Expressions from all layers within a composition. I tried to change it up so it only removes all expressions from selected layers and it works, but I always get an Alert after I run it that says "Unable to execute script at line 14. undefined is not an object". I was able to get rid of this error but than it only works on the last selected layer.

Also I don't really know what the recurse_children function does. Maybe someone knows a better way to do this?

Thanks in advance
Christian

Code: Select all

app.beginUndoGroup("Remove all Expressions");
{
    var comp = app.project.activeItem;
    var layerNumber = comp.selectedLayers.length;
    var chosenLayers = comp.selectedLayers;
    
    if (layerNumber >= 1){

       for (var i = 0; i <= layerNumber; i++)
       {
          var layerNames = chosenLayers[i].index;
          recurse_children(comp.layers[layerNames]);
       }
    }    
    else{    
       alert('no layer selected');
    }

    function recurse_children(propParent)
    {
       if (propParent != null)
       {
          var prop;
          
          for (var i=1; i<=propParent.numProperties; i++)
          {
             prop = propParent.property(i);
             switch (prop.propertyType)
             {
                case PropertyType.PROPERTY:
                   // do action
                    if (prop.canSetExpression && prop.expression) prop.expression = '';
                   break;
                case PropertyType.INDEXED_GROUP:
                   recurse_children(prop);
                   break;
                case PropertyType.NAMED_GROUP:
                   recurse_children(prop);
                   break;
                default:
                   break;
             }
          }
       }
    }
    }
app.endUndoGroup();
wysee
Posts: 7
Joined: November 21st, 2016, 9:24 am

Hi,
your loop is wrong. Selected Layers being an Array starting at index 0,  your loop should be ( i < layerNumber NOT <= ) like so:

Code: Select all

 if (layerNumber >= 1){

       for (var i = 0; i < layerNumber; i++)
       {
          var layerNames = chosenLayers[i].index;
          recurse_children(comp.layers[layerNames]);
       }
    }    
    else{    
       alert('no layer selected');
    }
CoonKunsen
Posts: 3
Joined: April 25th, 2018, 1:47 am

wysee wrote:Hi,
your loop is wrong. Selected Layers being an Array starting at index 0,  your loop should be ( i < layerNumber NOT <= ) like so:
Awesome thanks :)
Post Reply