Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Properly sort 'Album by Year' in iTunes and devices' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Properly sort 'Album by Year' in iTunes and devices
Authored by: x5josh5x on Apr 01, '10 06:08:30AM

I wanted the same thing and so wrote this Apple Script a while ago. It's very basic and borrows heavily from others, but it gets the job done. Make sure you have selected some tracks before running it.

(*

"Update Sort Album"
written by Josh Raub
(with code generously borrowed from others)

-- Adds tags of the format "1965 Highway 61 Revisited" in the Sort Album tag. This approximates the "Album By Year" sorting method on the iPod when using CoverFlow

v1.0
-- Initial release
-- It won't ignore the 'A' or 'The' for titles beginning with them, but that only matters if you ever want to sort "Album by Artist"

*)

tell application "iTunes"
if selection is not {} then
set sel to selection
display dialog "Update Sort Album" & return & return & "Adds year to the beginning of the \"Sort Album\" tag. This helps Cover Flow display as \"Album by Year\" on the iPod." & return & return buttons {"Cancel", "Continue"} default button 1 giving up after 30

set oldfi to fixed indexing
set fixed indexing to true
set cnt to 0

repeat with this_track in sel
try
set this_Year to this_track's year as string
set this_Album to this_track's album
set this_track's sort album to this_Year & " " & this_Album
set cnt to cnt + 1
end try
end repeat

set fixed indexing to oldfi

display dialog "Done!" & return & return & (cnt & " track(s) updated.") & return buttons {"Thanks"} default button 1 with icon 1 giving up after 15
else
display dialog "Select some tracks first..." buttons {"Cancel"} default button 1 with icon 2 giving up after 30
end if
end tell



[ Reply to This | # ]
Properly sort 'Album by Year' in iTunes and devices
Authored by: davechevell on Apr 30, '10 05:51:42AM

x5josh5x: Your script is EXCELLENT. Just what I needed to get my iPhone to sort albums by year before name.

In the spirit of collaboration, I made a few modifications =)

Notes are for intrepid googlers who find their way here:

1 - Sort Album field is now "Artist - Year - Album". This is because the "Albums" view on my iPhone is otherwise impossible to navigate unless i know the year every album in my library was made, and that nifty alphabet-quick-scroll thingie on the right hand side is completely useless when all Sort Album fields begin with a number

2 - Artists & Albums that begin with "A" or "The" (i.e. The Beatles) have these words stripped from the applied Sort Album field (not case sensitive)

3 - Script tests to see if the desired Sort Album field already exists. Running this script for the first time on a library containing thousands of tracks can take a while - running this script a second time to update new songs in your library will be much quicker (my library is about 4000 items - first time running this took a few minutes; second time took 20 - 25 seconds as it found nothing that needed updating)

I modded it purely to run on my own library, so I removed the GUI actions - running this requires opening in AppleScript editor, pasting this in, selecting the desired iTunes tracks (or hitting ⌘ + a to select all). If you're antsy about this kind of stuff, I advise you select one or two tracks in iTunes, run it on them and check the results. You'll feel more comfortable running this on your entire library afterwards.

Random googlers who have ended up here: Since this modifies every song in your library, syncing your iPhone/iPod/iPad the first time after running this is going to take a while, as it has to copy every track over again. Running this script for the first time is also going to take a few minutes at the very least, depending on the size of your music library.

Anyway, here we go. Apologies about bad formatting.. first post, idiot newbie, etc etc. Please direct posting advice this way, I want to give a little back! Thanks again, x5josh5x





tell application "iTunes"
if selection is not {} then
set sel to selection
repeat with this_track in sel
if this_track's sort album does not contain this_track's artist & " - " & this_track's year then
try
set this_year to this_track's year as string
set this_artist to this_track's artist
if word 1 of this_artist is "a" or word 1 of this_artist is "the" then set this_artist to text ((offset of " " in this_artist) + 1) thru -1 of this_artist
set this_album to this_track's album
if word 1 of this_album is "a" or word 1 of this_album is "the" then set this_album to text ((offset of " " in this_album) + 1) thru -1 of this_album
set this_track's sort album to this_artist & " - " & this_year & " - " & this_album
end try
end if
end repeat
end if
end tell



[ Reply to This | # ]
Properly sort 'Album by Year' in iTunes and devices
Authored by: davechevell on Apr 30, '10 06:43:49AM

Just because I'm bored, reincorporated the GUI (now displays time taken to run).. had to move a couple of things around but it still achieves the effect of the original script:



tell application "iTunes"
set startTime to (get current date)
set oldfi to fixed indexing
set fixed indexing to true
set cnt to 0
if selection is not {} then
set sel to selection
display dialog "Update Sort Album" & return & return & "Adds year to the beginning of the \"Sort Album\" tag. This helps iPods, iPhones and iPads sort Albums by Year." & return & return buttons {"Cancel", "Continue"} default button 1 giving up after 30

repeat with this_track in sel
if this_track's sort album does not contain this_track's artist & " - " & this_track's year then
try
set this_year to this_track's year as string
set this_artist to this_track's artist
if word 1 of this_artist is "a" or word 1 of this_artist is "the" then set this_artist to text ((offset of " " in this_artist) + 1) thru -1 of this_artist
set this_album to this_track's album
if word 1 of this_album is "a" or word 1 of this_album is "the" then set this_album to text ((offset of " " in this_album) + 1) thru -1 of this_album
set this_track's sort album to this_artist & " - " & this_year & " - " & this_album
set cnt to cnt + 1
end try
end if
end repeat

else
display dialog "Select some tracks first..." buttons {"Cancel"} default button 1 with icon 2 giving up after 30
end if
set fixed indexing to oldfi
set endTime to (get current date)
set process_duration to endTime - startTime
set mins to process_duration div minutes
set secs to process_duration mod minutes

display dialog "Done!" & return & return & (cnt & " track(s) updated.") & return & "Process took " & mins & " minutes and " & secs & " seconds." & return buttons {"Done"} default button 1 with icon 1 giving up after 15

end tell



[ Reply to This | # ]
Properly sort 'Album by Year' in iTunes and devices
Authored by: Lrwebb on Apr 23, '12 10:07:18AM

I know this thread is a couple of years old but I just wanted to thank josh and Dave for making this script.

I've been looking for a script like this for ages and this does just the job I was looking for. Almost. In fact, I've set about making a couple of edits for it to fit my requirements more exactly.

One, I always set the sort artist tag to last name, first name so I want the artist in the sort album tag to reflect this. I am a scripting novice but I've just managed to get that one to work.

Two, I would like to check to see if this track is part of a compilation. If true, then set artist to Various Artists. Perhaps even preceed that with some char to force compilations to the bottom of the albums list.

Thanks again guys.



[ Reply to This | # ]
Properly sort 'Album by Year' in iTunes and devices
Authored by: Calion on Aug 01, '13 02:28:42PM

So post your modified version then! We want to see what fixes you made.

I agree though, the script is excellent. Thanks to Dave for his modified script.



[ Reply to This | # ]