-- Set “Sort Artists” for iTunes Selection, by Jacob Rus, 20 March 2007
---- This script will pop up a dialog for each artist represented
-- among the selected tracks in iTunes, asking what to put in the
-- “Sort Artists” field for all selected songs by that artist.
---- Note: this script breaks if the the current iTunes view is sorted by
-- artist, or by ‘album by artist’ or ‘album by year’, because the order
-- of the selection then changes as the tracks gain a ‘sort artist’
-- attribute. Also, it may bog down a bit if very large numbers of
-- tracks are selected.
---- Suggestion: create a new smart playlist called “Not Sorted” which
-- matches tracks where “Sort Artist” is "". Make sure that this
-- smart playlist is *NOT* set to “Live Updating”, as this will
-- cause the same problem as sorting by Artist. Sort this by Album,
-- and then select up to a few dozen albums, and run this script.
-- set this to true if you want the script to ignore any artists all
-- of whose tracks already have a “Sort Artist” attribute
property check_for_existing_sort_artist :falsetellapplication"iTunes"activate-- set track_list to (tracks in selection)
set artist_list to{}repeat with track_ in(selection)set artist_ to(artist of track_)-- if we are not checking for already sorted artists, then we
-- should make sure `no_sort_artist` is true, so every artist
-- will be included in the list
if check_for_existing_sort_artist thenset no_sort_artist to(sort artist of track_ is"")elseset no_sort_artist totrueend ifif(artist_list does not contain artist_)and no_sort_artist thenset artist_list to artist_list & artist_
end ifend repeat-- pop up a dialog box for every unique artist, and then add the
-- “Sort Artist” attribute to all tracks for that artist when the
-- user confirms.
repeat with artist_ in artist_list
set dialog_prompt to"Type the desired value for “Sort Artist”, for the artist:"&return&return& artist_
set default_text tomy put_last_name_first(artist_)set b2 to"Use “Artist” for “Sort Artist”"set b3 to"Set “Sort Artist”"set the_result todisplay dialog dialog_prompt with icon 1¬
default answer default_text ¬
buttons {"Cancel", b2, b3} default button b3
set button_pressed to button returned of the_result
set text_typed totext returned of the_result
if button_pressed is b3 then-- use typed text
my set_sort_artist_on_selection(artist_, text_typed)else if button_pressed is b2 then-- use artist
my set_sort_artist_on_selection(artist_, artist_)end ifend repeat-- we're finished; better let the user know
display dialog"All Done" with icon 1 buttons {"OK"} default button 1end tell-- Subroutines:
-- changes every song in the selection by 'artist_' to have sort artist 'sort_artist'
onset_sort_artist_on_selection(artist_, sort_artist)tellapplication"iTunes"set artist_ to artist_ asUnicode textrepeat with track_ inselectionif(artist_ = artist of track_)thenset(sort artist of track_)to sort_artist
end ifend repeatend tellendset_sort_artist_on_selection-- Turn "First Middle Last" into "Last, First Middle". Note, this function
-- does not get every name perfect. That's what the text box is for. ;)
onput_last_name_first(artist_name)-- handle the case where multiple artists are involved. Put last name
-- first on the first artist, and then just leave everyone else's name
-- alone.
repeat with conj_ in{" & "," and "}if(artist_name contains conj_)thenset parts_ to split of artist_name by conj_
set(item1of parts_)to put_last_name_first(item1of parts_)return(join of parts_ by (conj_ asstring))end ifend repeatset name_list to split of artist_name by ""if(length of name_list)≤1thenreturn artist_name
elsereturn(item-1of name_list)&", "&¬(join of({items1thru-2of name_list}) by "")end ifendput_last_name_first-- split the string into a list by some separator
to split of aString by sep
local aList, delims
tellAppleScriptset delims totext item delimiterssettext item delimitersto sep
set aList totext itemsof aString
settext item delimitersto delims
end tellreturn aList
end split
-- join a list into a string with some separator
to join of aList by sep
local aString, delims
tellAppleScriptset delims totext item delimiterssettext item delimitersto sep
set aString to aList asstringsettext item delimitersto delims
end tellreturn aString
end join