I'm trying to shove all the songs in my iTunes library into an array so
that I can sort them and then remove a ton of duplicates.
However, using the following script, I get an Out of Memory error when
I attempt to create the sort comparison function using new
Function(..). (The sort function is pseudo-documented here:
http://phrogz.net/JS/SortBy_js.txt and has been tested to work in other
JS UAs like Safari.)
function SortBy(){
var args = (arguments.length==1 &&
arguments[0].constructor==Array)?arguments[0]:arguments;
var body = "return ";
for (var i=0,len=args.length;i<len;i++){
var param = args[i];
if (typeof(param)=='string'){
var colName=param;
var sortAsc=1;
} else for (var colName in param) sortAsc = param[colName];
body+='a.'+colName+(sortAsc?'<':'>')+"b."+colName+"?-1:
a."+colName+(sortAsc?'>':'<')+"b."+colName+"?1:";
}
return new Function('a','b',body+0);
}
var itunes = MacOS.appBySignature('hook');
var bw = itunes.browser_window[1];
var pl = bw.view;
var songs = [];
for (var i=1,len=pl.tracks.length;i<=len;i++) songs.push(pl.tracks[i]);
var sortFunc = SortBy('name','artist','album'); // <-- Runs out of
memory here
songs.sort(sortFunc);
Core.message(songs[2].name);
(time passes)
So I got around that by hardcoding the sort function. Now I want to
delete the files. Since delete is a reserved keyword, something like
itunes.delete(song)
doesn't work. How do I use methods/Actions which are reserved keywords
in JS?
I'm trying:
itunes['delete'](song)
now, which at least compiles, but I'm not sure if that's the right call
method.
--
"When I am working on a problem I never think about beauty. I only
think about how to solve the problem. But when I have finished, if the
solution is not beautiful, I know it is wrong."
- R. Buckminster Fuller