Created
March 14, 2015 23:30
-
-
Save CloudCray/bc4db4db06aaa6e7576c to your computer and use it in GitHub Desktop.
Remove broken iTunes links
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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