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: tedw on Oct 14, '10 10:18:27AM
Here's it of a different way of handling this, that (maybe) isn't quite so hackish. This script disables all of the enabled tracks in the current playlist, idles for the remaining length of the song, and then re-enables the tracks. This will cause iTunes to stop naturally at the end of the song.
global curTrack
global enabledTracks

on run
	tell application "iTunes"
		-- cancel if not running
		if player state is stopped then my quitter()

		set curTrack to current track
		
		-- disable all tracks; current track will continue playing
		set enabledTracks to every track of current playlist whose enabled is true
		repeat with thisTrack in enabledTracks
			set enabled of thisTrack to false
		end repeat
	end tell
end run

on idle
	set remTime to 5 -- 5 second default idle
	tell application "iTunes"
		if player state is stopped then
			-- track has finished; re-enable all tracks and quit
			repeat with thisTrack in enabledTracks
				set enabled of thisTrack to true
			end repeat
			reveal curTrack
			my quitter()
		else
			-- track still running; guess at end time
			set remTime to round ((duration of current track) - (player position)) rounding up
		end if
	end tell

	-- idle for a reasonable time
	return remTime
end idle

on quitter()
	-- quit in handler so it works on app not on iTunes
	quit
end quitter
The one potential downside (obviously) is that if the app quits or crashes before the song is over, all the tracks will be left in the disabled state. If that's a worry, you can set enabledTracks as property rather than a global (properties are saved at quit, so it would be possible to write a little extra code to re-enable the disabled track).
Edited on Oct 14, '10 10:33:59AM by tedw


[ Reply to This | # ]