(*
"Super Remove Dead Tracks" for iTunes
written by Doug Adams
dougadams@mac.com
v1.5 october 26 2005
-- prevents error when trying to delete empty Smart Playlists and/or playlist that can't be deleted (Party Shuffle, Podcasts, etc)
-- code tweaks
v1.0 sep 25 2003
-- initial release
Get more free AppleScripts and info on writing your own
at Doug's AppleScripts for iTunes
http://www.dougscripts.com/itunes/
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
Get a copy of the GNU General Public License by writing to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
or visit http://www.gnu.org/copyleft/gpl.html
*)
-------------------------------------
(* the number below is the number of tracks
to count before reporting progress -
you can change this number to a larger or smaller number.
If you set it to zero, you will not see the progress message *)
property progress_factor : 500
tell application "iTunes"
display dialog "Super Remove Dead Tracks" & return & return & ¬
"Removes tracks whose files are missing or deleted, so-called" & ¬
"\"dead\" tracks designated with (!) next to the track name." buttons ¬
{"Cancel", "Proceed..."} default button 2
set mainLibrary to library playlist 1
set totalTracks to count of file tracks of mainLibrary
set all_playlists to user playlists whose smart is false -- don't delete Smart Playlists later
set deleted_tracks to 0
set all_checked_tracks to 0
set countem to ""
set oldfi to fixed indexing
set fixed indexing to true
repeat with t from totalTracks to 1 by -1
try
set this_track to file track t of mainLibrary
if this_track's location is missing value then
delete this_track
set deleted_tracks to deleted_tracks + 1
end if
set all_checked_tracks to all_checked_tracks + 1
if frontmost then
if (progress_factor is not 0) and (all_checked_tracks mod progress_factor) is 0 then
if deleted_tracks is greater than 0 then ¬
set countem to (deleted_tracks & " dead tracks removed so far...")
if frontmost is true then display dialog (all_checked_tracks as string) & ¬
" tracks checked..." & ¬
return & countem buttons {"Cancel", «data utxt266B»} giving up after 1
end if
end if
end try
end repeat
set fixed indexing to oldfi
repeat with this_playlist in all_playlists
if (get count of tracks of this_playlist) is 0 then
try
delete playlist this_playlist
end try
end if
end repeat
if deleted_tracks is greater than 0 then
set ps to " was"
if deleted_tracks is not 1 then set ps to "s were"
display dialog "Finished removing \"dead\" tracks." & return & ¬
deleted_tracks & " track" & ps & ¬
" removed." buttons {"Thanks"} default button 1 with icon 1
else
if gave up of (display dialog "It was not necessary to remove any tracks." buttons {"Thanks"} ¬
default button 1 with icon 1 giving up after 15) is true then error number -128
end if
end tell