Delete and Trash Currently Playing iTunes Song ... But Then Play Next?

Any thoughts on how one might modify Whack Current Track (from Doug Adams’ iTunes site) so that it will go on to play the next song in the library and/or playlist, as opposed to stopping playing altogether? Seems to be a popular request amongst the 'Net … me amongst the requesters. :wink: Any help quite sincerely appreciated!

global addenda
tell application "iTunes"
	activate
	if player state is playing then
		display dialog "Are you SURE you want to delete every copy of the currently playing track?" default button 2 with icon 1
		set ofi to fixed indexing
		set fixed indexing to true
		set tid to database ID of current track
		set floc to (get location of current track)
		delete (some track of library playlist 1 whose database ID is tid)
		repeat with p from 1 to count of every playlist
			try
				delete (some track of playlist p whose database ID is tid)
			end try
		end repeat
		set fixed indexing to ofi
		set addenda to " The file has been moved to the Trash."
		tell me to delete_the_file(floc)
		display dialog "Done." & addenda buttons {"Thanks"} default button 1 with icon 1
	end if
end tell

to delete_the_file(floc)
	try
		tell application "Finder" to delete floc
	on error
		set addenda to " However, the file could not be moved to the Trash."
		try
			tell application "Finder" to open container of floc
		end try
	end try
end delete_the_file

Hi Mike,

it’s very dangerous to test something unless you have a test user account :wink:
Actually you must only save playlist and track number of current track and start playing the same track number after deleting.
Because the original track has been deleted, the next track has now the number of the deleted one.

It’s not necessary to delete a track in all user playlists.
If you trash it in the library, it disappears everywhere.

This should work:

global addenda
tell application "iTunes"
	if player state is not stopped then
		display dialog "Are you SURE you want to delete every copy of the currently playing track and move its file to the Trash?" default button 1 with icon 1
		set ofi to fixed indexing
		set fixed indexing to true
		
		try
			set cpl to current playlist
			set {dbid, cla, tnr} to {database ID, class, track number} of current track
			try
				set floc to (get location of current track)
			end try
			try
				delete (some track of library playlist 1 whose database ID is dbid)
			end try
			set addenda to "Done. The track has been deleted."
			if cla is file track then
				set my addenda to "Done. The track has been deleted and its file has been moved to the Trash."
				my delete_the_file(floc)
			end if
		on error
			set addenda to "The track could not be deleted."
		end try
		
		set fixed indexing to ofi
		
		display dialog addenda buttons {"Thanks"} default button 1 with icon 1
		stop
		play track tnr of cpl
	end if
end tell

to delete_the_file(floc)
	try
		-- tell application "Finder" to delete floc
		do shell script "mv " & quoted form of POSIX path of (floc as string) & " " & quoted form of POSIX path of (path to trash as string)
	on error
		set addenda to "Done. However, the file could not be moved to the Trash."
	end try
end delete_the_file

It deletes it successfully, but does not continue playing — I receive an error: “Can’t make some data into the expected type.”

o.k. it was more difficult than I expected,
because you may reckon that some tracks have no track numbers :wink:

this works (at least on my machine after trashing a couple of copies)

global addenda
tell application "iTunes"
	if player state is not stopped then
		display dialog "Are you SURE you want to delete every copy of the currently playing track and move its file to the Trash?" default button 1 with icon 1
		set ofi to fixed indexing
		set fixed indexing to true
		
		try
			tell current playlist to set {cpl, countCpl} to {it, count tracks of it}
			set {dbid, cla} to {database ID, class} of current track
			repeat with tnr from 1 to countCpl
				if database ID of track tnr of cpl is dbid then
					exit repeat
				end if
			end repeat
			try
				set floc to (get location of current track)
			end try
			try
				delete (some track of library playlist 1 whose database ID is dbid)
			end try
			set addenda to "Done. The track has been deleted."
			if cla is file track then
				set my addenda to "Done. The track has been deleted and its file has been moved to the Trash."
				my delete_the_file(floc)
			end if
			
		on error
			set addenda to "The track could not be deleted."
		end try
		set fixed indexing to ofi
		
		display dialog addenda buttons {"Thanks"} default button 1 with icon 1
		stop
		play track tnr of cpl
	end if
end tell

to delete_the_file(floc)
	try
		-- tell application "Finder" to delete floc
		do shell script "mv " & quoted form of POSIX path of (floc as string) & " " & quoted form of POSIX path of (path to trash as string)
	on error
		set addenda to "Done. However, the file could not be moved to the Trash."
	end try
end delete_the_file

Absolutely no offense meant, but it seems like whatever tack you’re taking isn’t the trick. I deleted a file I was playing; it deleted it and then begun with an entirely new track in the Library. (Just to clarify, I’m working within the library, and not a specific playlist.)

Also, with a list of songs produced by the search box, it didn’t jump to the next song in the search results, but an entirely different song.

sorry, then I missunderstood the problem.
AFAIK you can access a search result only by selecting all with UI scripting and take the selection.
But If I do this for a huge library (mine is not really huge with 2700 tracks :wink: ) I get a
Size Check failed (memSCErr:-116) error in Script Debugger.

Gentlemen:

The solution (I hope) is simply to tell iTunes to advance to the next track BEFORE you delete, thus continuing on in the current playlist and then processing the deletion. I modified the first script listed (and removed the unnecessary repeat through the various albums), and I believe this is close to what you want:

global addenda
tell application "iTunes"
	activate
	if player state is playing then
		display dialog "Are you SURE you want to delete every copy of the currently playing track?" default button 2 with icon 1
		set ofi to fixed indexing
		set fixed indexing to true
		set tid to database ID of current track
		set floc to (get location of current track)
		next track --**************Just tell iTunes to go to the next track before you delete******************
		delete (some track of library playlist 1 whose database ID is tid)
		set fixed indexing to ofi
		set addenda to " The file has been moved to the Trash."
		tell me to delete_the_file(floc)
		display dialog "Done." & addenda buttons {"Thanks"} default button 1 with icon 1
	end if
end tell

to delete_the_file(floc)
	try
		tell application "Finder" to delete floc
	on error
		set addenda to " However, the file could not be moved to the Trash."
		try
			tell application "Finder" to open container of floc
		end try
	end try
end delete_the_file

Good luck,

Craig, that one worked like a charm. Thank you very much. (And thank you, Stefan, as well, for the attempts!)

Well, Craig, sometimes the solution can be so simple, thanks :smiley:

Thank you for the kind words, guys.