Finish current track in iTunes playlist and pause

Oct 14, '10 07:30:00AM

Contributed by: david-bo

This is an AppleScript that, when run, pauses iTunes when the current song is finished. I use it all the time when I want to listen to a track and then do something else.

To use this, copy and paste the code into the AppleScript Editor, and save it as a Stay Open Application.

Just play a Playlist, launch the script and leave the computer and it pauses the track at the end of it. Quit the script from its Application menu like any other program when finished.

global stopTime
global curTrack
global remTime
global firstloop

on idle
  if firstloop then
    set firstloop to false
    return remTime - 5
  else
    if ((current date) > stopTime) then
      finish()
      quit
    end if
  end if
  return 1
end idle

on run
  set firstloop to true
  tell application "iTunes"
    set d to duration of current track
    set p to player position
    set remTime to d - p
    set stopTime to (current date) + remTime + 1
    set curTrack to current track
  end tell
end run

on finish()
  tell application "iTunes"
    reveal current track
    pause
  end tell
end finish
[crarko adds: I tested this in iTunes 10.0.1 and SL 10.6.4, and it will work if run in the sequence given above. I got 'variable undefined' messages from the script if I ran it before starting a Playlist in iTunes. The script (as a compiled Stay Open Application) is mirrored here.]

Comments (23)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20101008060926278