Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Finish current track in iTunes playlist and pause' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Finish current track in iTunes playlist and pause
Authored by: Solarusdude on Oct 18, '10 12:46:48PM
This is a great post. I've been meaning to write my own script for the past year or so that would build this feature into iTunes. Unfortunately, I finally got around to it a week BEFORE this posting rather than waiting another week. While I think there are more experienced programmers here with more efficient algorithms to achieving the same end, I'll include my script too in case there are users having trouble with the other scripts.

property my_title : "Stop at End of Current Track"
property interval : 3 --Duration (in seconds) of delay before loop repeats.
property stop_offset : 1 --Expected latency between when script stops playback and when playback actually stops.

tell application "iTunes"
	-- Get track fingerprint and finish time. If the player is stopped, the script quits immediately with an alert message.
	if player state is stopped then
		display dialog "No track is playing." buttons {"OK"} default button 1
		return 0
	end if
	try
		set end_time to (get finish of current track) as real
		copy (get database ID of current track) to Last_Song
	on error
		display dialog "No track is playing." buttons {"OK"} default button 1
		return 0
	end try
	
	-- Script then repeats every interval until track ends or status changes.
	repeat
		set time_left to (end_time - player position) as real
		copy (get database ID of current track) to Current_Song
		if player state is not playing then exit repeat -- Script quits if playback stopped already.
		if Current_Song is not Last_Song then -- Script quits if user changed song.
			stop
			exit repeat
		end if
		if time_left < interval then
			delay (time_left - stop_offset) -- Short interval
			try
				tell current track
					set played count to (get played count) + 1
					set played date to (get current date)
				end tell
			end try
			stop
			exit repeat
		else
			delay (interval) -- Wait one full interval
		end if
	end repeat
end tell


[ Reply to This | # ]