Skip to content

Instantly share code, notes, and snippets.

@CloudCray
Created March 14, 2015 23:30
Show Gist options
  • Save CloudCray/bc4db4db06aaa6e7576c to your computer and use it in GitHub Desktop.
Save CloudCray/bc4db4db06aaa6e7576c to your computer and use it in GitHub Desktop.
Remove broken iTunes links
import win32com.client
iTunes = win32com.client.gencache.EnsureDispatch("iTunes.Application")
lpl = iTunes.LibraryPlaylist
track_indices = []
for i in range(lpl.Tracks.Count):
trk = lpl.Tracks(i+1) # iTunes has a 1-based index
if trk.Kind == 1:
tc = win32com.client.CastTo(trk, "IITFileOrCDTrack")
if tc.Location.strip() == "": # No location means it can't find the song
track_indices.append(i)
# Make sure you go back to front
# otherwise, deleting a track changes the index of all the songs after it
for i in reversed(track_indices):
trk = lpl.Tracks(i+1) # iTunes has a 1-based index
trk.Delete() # Some songs will give you a popup, FYI
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment