Deleting in iTunes using a file path

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

Sorry for the duplicate posts, I’m unable to delete 2 of them since they don’t appear to have any content.

Moderator: Deleted.

What does the path from Hazel look like?

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

Hi,

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

Edit: you got the point, Bruce :slight_smile:

The error I’m getting back is:

2007-10-05 12:02:08.703 hazelfolderwatch[926] 04 Len Tillem (Ep. 4).m4a: Rule Delete Old Radio Programming matched.
2007-10-05 12:02:08.716 hazelfolderwatch[926] [Error] AppleScript failed: Error executing AppleScript /Users/Paula/Documents/Delete From iTunes (Hazel).scpt on file /Users/Paula/Music/iTunes/iTunes Music/Len Tillem/Unknown Album/04 Len Tillem (Ep. 4).m4a.
2007-10-05 12:02:08.716 hazelfolderwatch[926] AppleScript error: {
NSAppleScriptErrorBriefMessage = “Can’t get database ID of alias "Macintosh HD:Users:Paula:Music:iTunes:iTunes Music:Len Tillem:Unknown Album:04 Len Tillem (Ep. 4).m4a".”;
NSAppleScriptErrorMessage = “iTunes got an error: Can’t get database ID of alias "Macintosh HD:Users:Paula:Music:iTunes:iTunes Music:Len Tillem:Unknown Album:04 Len Tillem (Ep. 4).m4a".”;
NSAppleScriptErrorNumber = -1728;
NSAppleScriptErrorRange = <00000000 00000000 >;

I think I’m getting back a path with a slash separator, does applescript convert that automatically to a colon?

Desh

Automatically not, but try this

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.

Hi,

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

Thank you Stefan! The brute for method worked as expected, but the optimized version gives me the following error:

2007-10-06 15:12:29.262 hazelfolderwatch[3982] 04 Len Tillem (Ep. 4).m4a: Rule Delete Old Radio Programming matched.
2007-10-06 15:12:32.362 hazelfolderwatch[3982] [Error] AppleScript failed: Error executing AppleScript /Users/Paula/Documents/Delete From iTunes (Hazel).scpt on file /Users/Paula/Music/iTunes/iTunes Music/Len Tillem/Unknown Album/04 Len Tillem (Ep. 4).m4a.
2007-10-06 15:12:32.363 hazelfolderwatch[3982] AppleScript error: {
NSAppleScriptErrorBriefMessage = “alias "Macintosh HD:Users:Paula:Music:iTunes:iTunes Music:Len Tillem:Unknown Album:04 Len Tillem (Ep. 4).m4a" doesn’t understand the count message.”;
NSAppleScriptErrorMessage = “alias "Macintosh HD:Users:Paula:Music:iTunes:iTunes Music:Len Tillem:Unknown Album:04 Len Tillem (Ep. 4).m4a" doesn’t understand the count message.”;
NSAppleScriptErrorNumber = -1708;
NSAppleScriptErrorRange = <00000000 00000000 >;
}

Is there an easy way to step through the lines to see where it’s failing? If not, what’s the best way to figure out what line it’s failing on?

Thanks,
Desh