anything to pass the time
and keep that song out of your mind— "Clementine", Elliott Smith.

Ever since I switched to a Mac, I've been wishing that iTunes had a 'stop after this song' functionality, similar to the 'Ctrl-v' or 'Shift-v' functions in Winamp (the latter does a fadeout). Looks like I am not alone, so allow me to bore you with the details.
Firstly, it might seem to be a strange requirement, but it's because I'm one of those odd people who have difficulty stopping a song in the middle; it jars my senses and increases my grump level. No kidding! I experience physical shock if music stops abruptly before its proper conclusion. If I had to get off the computer to get a coffee or a drink of water, or to collect some printouts, I'd usuallly try to time it so I detach myself from my headphones at the end of a song.
Short of pressing 'stop' at the right time, I've found a couple of ways to achieve more or less the same effect: if I'm in a playlist, I can uncheck the remaining songs so they don't play; secondly, I can switch the focus to another playlist or album. Needless to say, option 1 is tiresome, and option 2 means if I want to continue with what I was listening to, I'd have to navigate back to the playlist or album I was on, and find the next unplayed song in the list. Too much hassle, honestly.
Both ways require that I scramble under my stack of windows for iTunes. (If you've ever seen my screen, you will understand my dilemma.) Even with the whooshing windows of Exposé, 2 seconds to realising that the current song is finishing, I simply can't navigate fast enough with the trackpad, seeing as I never got around to replacing my mouse ever since some idiots decided to steal it along with the other laptop. In any case, I don't want to be watching iTunes while the song finishes up, I want to continue doing whatever else I might be working on ...
So, I'd finally gotten around to toying with Applescript, and after some fumbling around with trying decide what's a workable algorithm for detecting when to get iTunes to stop, I ended up with this:
tell application "iTunes"
if player state is playing then
set playingTrackName to current track's name
repeat
if (playingTrackName is not current track's name) then
stop
exit repeat
end if
end repeat
end if
end tell
Basically, we grab the name of the song that is currently playing, we watch iTunes until the song switches (i.e. when the song name changes), at which point we tell iTunes to stop.
Originally, I set about calculating the song's remaining time and comparing it to the clock, but it was a bothersome and calculation intensive method. Besides, it wouldn't work if someone decided to fast forward the song in the middle (god forbid why they would!) after having begun the script.
Not really the best script, I'm afraid; as it does a continual poll, it puts a bit of grind on the CPU, though not too badly. The worst side effect is that it seems to confuse Finder. However, it works a good deal of the time. In the remaining cases, it misses the start of the next song by a fraction of a second — presumably because the processor is busy doing something else and isn't paying attention at the right time — an effect which I originally thought can probably be minimised by making the script fade iTunes' volume very quickly. Having given this a shot though, I'm no longer convinced it's a good solution as the efficiency of the original script is much reduced by extra code, which of course munches up more CPU time. Anyhow, the crunch only happens with particular songs which start on the first second, so I guess I will just have to compromise.
Oh, the icons you see here in my dock are courtesy of the icon factory. Cute, aren't they?
Posted by sniffles at February 24, 2004 01:08 PM