Cleaning iTunes Pt. 2: Title Case


This is the second in a series of posts on cleaning up my library in iTunes. Last time we discussed using Dupin to find duplicates. This week I want to focus on the problem of capitalization of song names. If you are like me you have a few songs in your library where the case is screwed up. (Sometimes even the database of CD song names has some mistakes – most of my bad titles were from ripped CDs) What I want to do is use TitleCase (discussed last week) to check to see if the titles are correct. Because some titles are supposed to be weird rather than just automatically fixing things I just add the files to a special playlist. To keep my “fix it” playlists separate from my regular ones I always prepend them with two underscore characters. (I usually then delete them when I’m done — although you don’t have to)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/python
 
 
## Requires title case
## To install title case do:
##		easy_install titlecase
 
import sys
from titlecase import titlecase
from appscript import *		
 
def makeplaylist(playlist_name=u"__Bad Title"):
	# makes a playlist named by default "__Bad Title" to add the bad titled tracks to
 
	iT = app(u'/Applications/iTunes.app')
 
	try:
		# try and get the desired name
		playlist = iT.playlists[playlist_name].get()
 
	except:
		# playlist doesn't exist -- create it
		print "Creating playlist '" + playlist_name
		iT.make(new=k.playlist, with_properties={k.name: playlist_name})
		playlist = iT.playlists[playlist_name].get()
 
	return playlist
 
def check_selection():
 
 
	iT = app(u'/Applications/iTunes.app')
	songs = iT.selection.get()				# get list of all selected songs
 
	playlist = makeplaylist()
 
	print "Moving to __Bad Title"
	print "---------------------"
 
	for song in songs:
		name = song.name.get()
		titled_name = titlecase(name)
 
		if titled_name != name:
			print name
			song.duplicate(to=iT.playlists[u'__Bad Title'])
 
def fix_bad_titles():
 
	playlist = makeplaylist()
	tracks = playlist.tracks()
 
	for song in tracks:
		name = song.name.get()
		print "Original: " + name
		new_name = titlecase( name )
		print "New Name: " + new_name
		print
 
		song.name.set( new_name )
 
 
if __name__ == '__main__':
 
	#check_selection()
 
	fix_bad_titles()
 
 
    # change to 0 for success, 1 for (partial) failure
	sys.exit(0)

The only real tricky bit in the above is the makeplaylist function. Basically I try to get a playlist by name which throws an exception if it doesn’t exist. If it doesn’t exist I create it. By default the title is “__Bad Title” although you could set it to anything (and the function takes a name as an argument). It then returns the playlist object.

I break the program into two parts. You could simply make this two scripts. But honestly I tend to run most of my Python scripts from within TextMate (with the old ⌘r technique). So to me it’s easier to just comment out the line I don’t want to run.

The first function, check_selection, just gets all the selected songs and compares their names to a case corrected name. If they don’t match it adds them to the playlist returned by makeplaylist. (In this case “__Bad Title”)

Usually I run just check_selection and then check the playlist by hand to ensure there’s nothing in there that shouldn’t be.

The second function, fix_bad_titles, gets all the songs in the playlist and simply resets the name to the case corrected version.

Obviously one thing you could do if you want is take out all the print statements. But I like to see what is going on while I run these scripts.

Related posts:

  1. Cleaning iTunes Pt. 4: Artist Names
  2. Cleaning iTunes Pt. 3: Album Names
  3. Cleaning iTunes Pt. 1
  4. Fix iTunes Names
  5. The Future of iTunes?
  6. Titlecase for Python
  7. Python & Appscript Tools
  8. Fixing iTunes 9′s Appearance
  1. No comments yet.

Comments are closed.