Filter a list

Ok I have retrieved a list of every album name from every track I have in itunes. Then I take the list and put it into a “choose from list”. The problem is some tracks do not have a name for the album so it shows a blank choice. Second issue is when you retrieve the album of every track, album names repeat in the list because albums have more than one track. I have tried to use ("whose name is not " ") but this returns an error. And I am baffled on how to stop the repeating of the album names.

Any other possible way to get a list of albums other than the way i did it?

tell application "iTunes"
	set all_track_albums to the album of every track as list
end tell
--set filtered_albums to all_track_albums whose name is not ""  -- not working returns the error
set thelist to choose from list all_track_albums with prompt "Pick an Album:"

console:
Can’t get {“(tons of song names listed here)”} whose name of it ≠{“”}. (error -1728)

This example uses a category on NSArray. Meaning it opens up the class NSArray and adds a class method
to it. You can convert this to AppleScriptObjC or use it as is.

You do not need to coerce “as list” for every track since it is already a list.


tell application "iTunes"
	set all_track_albums to the album of every track
end tell
set the_sorted to current application's class "NSArray"'s sortAndRemoveEmpty_(all_track_albums)
set thelist to choose from list (the_sorted as list) with prompt "Pick an Album:"

Adding a category on NSArray.
Add these files to your project.

NSArray+Filter.h

NSArray+Filter.m

Thanks! It worked perfectly!

Hi Craig,

with NSPredicate you can easily filter all occurrences of an empty string

+ (NSArray *)sortAndRemoveEmpty:(NSArray *)array { NSSet *uniques = [NSSet setWithArray:array]; NSArray *sortedArray = [[uniques allObjects] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)]; NSPredicate *notEmpty = [NSPredicate predicateWithFormat:@"SELF != ''"]; return [sortedArray filteredArrayUsingPredicate]; }

Hi Stefan,

Thanks for the input. I thought about using NSPredicate, but with only one empty string possible, and it always being in the first position, removing that one item seemed the best solution in this case.

I have been using MacRuby more lately and it is making coding much easier. The combination of it and rb-appscript is really awesome.

Here is the code to get the tracks, remove duplicates, empty strings and sort. Two lines of code. Sweet!

How would you go by translating this to applescript?

Something like this (untested):

set uniques to current application's NSSet's setWithArray_(theList)
set sortedList to (uniques's allObjects()'s sortedArrayUsingSelector_("caseInsensitiveCompare:")) as list
if item 1 of sortedList = "" then set sortedList to rest of sortedList

And:

set uniques to current application's NSSet's setWithArray_(theList)
set sortedArray to uniques's allObjects()'s sortedArrayUsingSelector_("caseInsensitiveCompare:")
set notEmpty to current application's NSPredicate's predicateWithFormat_(SELF != ''")
set sortedList to (sortedArray's filteredArrayUsingPredicate_(notEmpty)) as list