Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions ExtendScript selected layer index

  • ExtendScript selected layer index

    Posted by James Ronan on April 15, 2016 at 4:09 pm

    Hi all, Sorry I know this isn’t expressions but couldn’t see an ExtendScript forum.

    For my first ExtendScript project I’ve created a panel with a button! When I click my button (myButtonCtrl) I want to retrieve the index number of the layer currently selected, and show it in an alert. So far I have this, however it doesn’t appear to be working.

    myButtonCtrl.onClick = function () {

    var layerIndex = app.project.activeItem.selectedLayers.index;
    alert (layerIndex);

    }

    Any ideas?

    James

    James Ronan replied 10 years, 3 months ago 2 Members · 7 Replies
  • 7 Replies
  • Dan Ebberts

    April 15, 2016 at 7:42 pm

    What you get with selectedLayers is an array. So something like this would give you the index of the first selected layer:


    var myLayers = app.project.activeItem.selectedLayers;
    if (myLayers.length > 0){
    alert (myLayers[0].index);
    }else{
    alert ("No layers selected.");
    }

    Dan

  • James Ronan

    April 16, 2016 at 10:39 am

    Thanks a lot Dan! That works perfectly! One more Question…

    when the button clicks I also have a variable that creates a new solid:

    var newSolid = app.project.activeItem.layers.addSolid([0,1,0], "My First Solid", 1920, 1080, 1.0, 10);

    It automatically puts the solid at the top (Index 1) What I’d like it to do is add it one above the selected layer…

    So far I’ve tried a few combinations but nothing is working.

    Any suggestions?

    Thank you again!!

    James

  • James Ronan

    April 16, 2016 at 11:00 am

    Also, when I said ‘one above’ I was thinking of the timeline and actually meant an index of -1

    I’ve been trying to figure it out and I think that because once you add a new solid, the selected layer index is going to change +1

    so far this what i’ve wrote but it isn’t working.

    newSolid.index = (layerIndex[0].index - 1);

  • Dan Ebberts

    April 16, 2016 at 4:08 pm

    Try it this way:


    var myLayers = app.project.activeItem.selectedLayers;
    var newSolid = app.project.activeItem.layers.addSolid([0,1,0], "My First Solid", 1920, 1080, 1.0, 10);
    if (myLayers.length > 0){
    newSolid.moveBefore(myLayers[0]);
    }

    Dan

  • James Ronan

    April 16, 2016 at 10:05 pm

    Hey thanks so much that works! What is the reason for the [0] after myLayers?

    Really appreciate the help… but I have one more question (last I promise). After the solid has been moved to correct index, I wanted to change the myLayers (The layer originally selected) Track Matte to Alpha.

    I’ve added: myLayers.trackMatteType = TrackMatteType.ALPHA;

    However it doesn’t seem to work. I’ve tried a few different lines of code and I think the issue is coming from the .selectedLayers.

    This works if I specify the layer: app.project.activeItem.layer(7).trackMatteType = TrackMatteType.ALPHA;

    The complete code is now:

    var myLayers = app.project.activeItem.selectedLayers;
    var newSolid = app.project.activeItem.layers.addSolid([0,1,0], "My First Solid", 1920, 1080, 1.0, 10);

    if (myLayers.length > 0){
    newSolid.moveBefore(myLayers[0]);
    myLayers.trackMatteType = TrackMatteType.ALPHA;
    }

    Thanks once again.

    James

  • Dan Ebberts

    April 16, 2016 at 10:22 pm

    You almost have it. Try it this way:


    var myLayers = app.project.activeItem.selectedLayers;
    var newSolid = app.project.activeItem.layers.addSolid([0,1,0], "My First Solid", 1920, 1080, 1.0, 10);
    if (myLayers.length > 0){
    newSolid.moveBefore(myLayers[0]);
    myLayers[0].trackMatteType = TrackMatteType.ALPHA;
    }

    myLayers is an array of the layers selected at the time you run the script. myLayers[0] is the layer that was selected first. The 0 is the index into the array, which gives you the first array element, or in this case, the first selected layer.

    Dan

  • James Ronan

    April 16, 2016 at 10:34 pm

    Ah Yes! that makes sense. Wow thank you so much!

    Really helped, thanks a lot Dan

    James

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