Ben Zotto Ben Zotto - 4 months ago 20
AppleScript Question

Using "whose" on arrays in Javascript for Automation

Playing with the new JS for automation using Script Editor. I'm getting an error on the final line of the following:

var iTunes = Application("iTunes");
var sources = iTunes.sources();
var library = sources.whose({name : "Library"});


Confirmed that the sources array is as expected (two elements, one with
name
"Library" and one "Internet Radio"). But that final line chokes with
Error on line 3: TypeError: undefined is not a function (evaluating 'sources.whose({name : "Library"})')
.

As far as I can tell, I'm using the right syntax for the
whose
function
. (I also tried with an explicit
_equals
clause to the same result.) What am I doing wrong?

Answer

This now works as theory would predict.

(function () {
    'use strict';

    var iTunes = Application('iTunes'),
        filtered = iTunes.sources.whose({
            name: 'Library'
        });

    return filtered().length;

})();