Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Accessing marker.nearestKey from ExtendScript

  • Accessing marker.nearestKey from ExtendScript

    Posted by Reuben Lara on October 8, 2018 at 2:54 pm

    Dan Ebberts: I’m using your marker code to implement a ScriptUI panel to manage some marker functionality. I’m trying to access nearest key from within a button function based on your code at motionscript.com. I’ve used it successfully inside property expressions, but can’t get the nearestKey to work when called from a .jsx file.

    Essentially just trying to retrieve the name of a marker if the current time is within the span of a split layer marker.

    I can get to the alert(“got here 1”) and no further. No combination of the following seem to work for me:

    n = mySelection[0].marker.nearestKey(time).index;
    n = mySelection[0].markers.nearestKey(time).index;
    n = markers.nearestKey(time).index;

    What am I doing wrong? or maybe I’m way over complicating this? Thanks!

    DAN'S CODE:
    n = 0;
    if (marker.numKeys > 0){
    n = marker.nearestKey(time).index;
    if (marker.key(n).time > time){
    n--;
    }
    }

    if (n == 0){
    0
    }else{
    //...some code
    }

    MY CODE:
    function getMarkerName(){

    var mySelection = app.project.activeItem.selectedLayers;
    var myComp = app.project.activeItem;

    if (mySelection.length < 1){

    alert("select a layer");

    }else{
    //grab marker data
    var layer = mySelection[0];
    var markers = layer.property("Marker");

    var n = 0;

    if (markers.numKeys > 0){
    alert("got here 1");
    n = mySelection[0].marker.nearestKey(time).index;
    alert("got here 2");
    if (n > time){
    alert("got here 3");
    n--;
    }
    alert("got here 4");
    }

    if (n == 0){
    alert("got here 5");
    }else{
    alert("got here 6");
    var m = markers.key(n);
    var markerName = m.comment;

    if( (time > m.time) && (time < (m.time-m.duration)) ){

    return(markerName);

    }else{
    alert("no marker here");
    }
    }
    }

    }

    Reuben Lara replied 7 years, 7 months ago 2 Members · 2 Replies
  • 2 Replies
  • Dan Ebberts

    October 8, 2018 at 3:12 pm

    In general, scripting isn’t going to understand expression syntax. Specifically, nearestKey(time) isn’t going to work. Scripting does have nearestKeyIndex(), so you could do something like this:

    var myComp = app.project.activeItem;
    var myLayer = myComp.selectedLayers[0];
    var myMarker = myLayer.property(“Marker”);
    var myIndex = myMarker.nearestKeyIndex(myComp.time);
    alert (myIndex);

    Dan

  • Reuben Lara

    October 8, 2018 at 3:14 pm

    No wonder i’ve been banging my head. Thanks!

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