Search code examples
javascriptextendscriptafter-effects

After Effects / ExtendScript Function.caller equivalent


Since ExtendScript as implemented in After Effects does not appear to support many of the Function members like "Function.caller", is there another known way that would let you see the calling function's name, that works for AE CS5.5 upwards? Both built-in or external libs would be great.


Solution

  • If you want the running function name(callee), you can do it with:

    arguments.callee.name
    

    Example:

    function someFuncName() {
        $.write(arguments.callee.name);
    }
    someFuncName();
    //Result: someFuncName
    

    In your case(where you need the caller function name), ExtendScript hasn't built-in function that does it, so you need to create one:

    function caller() {
        var stack = $.stack.split('\n');
        return stack.length === 4 ? null : stack[stack.length - 4].slice(0, -2);
    }
    

    Now, if we have a function that invoked by the top level code, so we will get null, otherwise we get the caller function name:

    Example:

    function someFuncName() {
        $.write(caller());
    }
    function callerFuncName() {
        someFuncName();
    }
    callerFuncName(); //Result: callerFuncName
    someFuncName(); //Result: null