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 s
Based on the JavaScript for Automation Release Notes Mail.inbox.messages.whose(...)
example, the following should work:
var iTunes = Application('iTunes');
var filtered = iTunes.sources.whose({name : 'Library'});
The apparent goal for the "special whose
method" with "an object containing the query" is to be efficient by only bringing select items (or item references) from the OS X object hierarchy into the resulting JavaScript array.
However, ... the whose
feature of JXA appeared to have some bugs in the earlier OS X v10.10 release.
So, since the array in question is small (i.e. 2 items), filtering can be done fast & reliably on the JavaScript side after getting the elements as a JS array.
Workaround Example 1
var iTunes = Application('iTunes');
var sources = iTunes.sources();
var librarySource = null;
for (key in sources) {
var name = sources[key].name();
if (name.localeCompare("Library") == 0) {
librarySource = sources[key];
}
}
Workaround Example 2
var iTunes = Application('iTunes');
var sources = iTunes.sources();
function hasLibraryName(obj) {
var name = obj.name();
if (name.localeCompare("Library") == 0) {
return true;
}
return false;
}
var filtered = sources.filter(hasLibraryName);