I’d like to delete a track from my iTunes library (and any playlist that might contain it) using Hazel. Hazel provides me a file path, but I suspect I’ll need to convert this to a track in iTunes. I’m hoping to modify the following applescript. Anyone have any ideas?
Thanks,
Desh
on hazelProcessFile(infile)
tell application "iTunes"
try
delete infile
on error
delete (first track of library playlist 1 whose database ID is (get database ID of infile))
end try
end tell
end hazelProcessFile
on hazelProcessFile(infile)
tell application "iTunes"
try
delete infile
on error
delete (first file track of library playlist 1 whose location is anAppleScriptAliasToTheFile)
end try
end tell
end hazelProcessFile
what’s about deleting the track directly by the path (property location)
on hazelProcessFile(infile)
tell application "iTunes"
try
delete (first track of library playlist 1 whose location is (infile as alias))
return true
on error
return false
end try
end tell
end hazelProcessFile
on hazelProcessFile(infile)
tell application "iTunes"
try
delete (first track of library playlist 1 whose location is (POSIX file infile as alias))
return true
on error
return false
end try
end tell
end hazelProcessFile
Thanks for the quick response. I’ve investigated further and it appears Hazel is giving me an alias to the file, not a path as string (my bad.) I’ve tried modifying your script to:
on hazelProcessFile(infile)
tell application "iTunes"
try
delete (first track of library playlist 1 whose location is (infile))
return true
on error
return false
end try
end tell
end hazelProcessFile
but it’s still throwing and returning false. I also tried your script as is, and again that returns false.
obviously it’s not possible to filter tracks by location. I regard this as a bug.
This is a quite slow solution, but it should work:
on hazelProcessFile(infile)
script o
property trackList : {}
end script
tell application "iTunes"
set o's trackList to tracks of library playlist 1
repeat with oneTrack in o's trackList
if location of oneTrack is infile then
delete oneTrack
return true
end if
end repeat
return false
end tell
end hazelProcessFile
I guess, there’s probably a way to make it faster with two lists (tracks and location) and a binary search looking for the index number
PS: this is an approach to accellerate the script. To avoid reading the iTunes library again and again for each track,
collect the files into a list and call the script
on hazelProcessFile(FilesToDelete) -- FilesToDelete must be a list of aliases
script t
property trackList : {}
end script
set deleteList to {}
tell application "iTunes" to tell tracks of library playlist 1 to set {t's trackList, locationList} to {it, location of it}
repeat with oneTrack in FilesToDelete
BinarySearch(locationList, contents of oneTrack)
if result is not false then set end of deleteList to item result of t's trackList
end repeat
tell application "iTunes" to delete deleteList -- if deleting a list doesn't work, use a repeat loop
end hazelProcessFile
on BinarySearch(theList, value)
local low, mid, high
script o
property l : theList
end script
set low to 1
set high to (count theList)
repeat while (low ≤ high)
set mid to (low + high) div 2
if ({value} is in items low thru mid of o's l) then
set high to mid - 1
else
set low to mid + 1
end if
end repeat
if (item low of o's l is value) then return low
return false
end BinarySearch