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: blinkintosser on Oct 15, '10 11:07:26PM
It's neither short nor elegant, but here is a script I've used for a few years to sleep my Mac at the completion of a song. The only novel feature it has relative to the other submitted scripts is the warning dialog with cancel button. (This was not written for public consumption, so please forgive the ugliness of the nested "tells" and volume fading calculations that I had never cleaned up.)

--User preferences
property pIntFadeClipTime : 5 -- time, in seconds, before end-of-track to complete fade-out
property pIntFadeLeadTime : 10 --time, in seconds, before clip-time to begin fade-out
property pIntFadeSteps : 10 --number of volume reduction steps to make (higher=smoother, smaller=greater completion time accuracy)
property pIntFadeAcceleration : 1.25 --speedup factor for fade, e.g. 1=normal, 2=full fadeout in 1⁄2 of lead time (can compensate for applescript’s sluggishness)
property pStrItunesSig : "" --bundle id ("") or creator-type ("hook") of iTunes, either should work

on run
end run

on main()
  tell application "iTunes"
    if player state is playing then --iTunes must be playing a track
      --locate iTunes icon for use in our dialog
      tell application "Finder" to copy (file "Contents:Resources:iTunes.icns" of application file id pStrItunesSig) to refItunesIcon
      tell me to copy refItunesIcon as alias to aliasItunesIcon --coerce outside of "Finder" tell-block to work around occasional errors
      --gather track name and remaining time
      copy name of current track to strTrackName
      copy (duration of current track) - (player position) to intSecondsRemain
      if intSecondsRemain > pIntFadeLeadTime then
        --employ user-defined values for fade out
        copy pIntFadeLeadTime to intFadeLeadTime
        copy pIntFadeSteps to intFadeSteps
        copy pIntFadeAcceleration to intFadeAcceleration
        --show dialog to remind user of impending sleep and to allow for cancel
        copy ("This Macintosh will sleep upon completion of track “" & strTrackName & "”") to strDialogText
        copy (intSecondsRemain - pIntFadeLeadTime) as integer to intSecondsBeforeFade
        display dialog strDialogText buttons {"Cancel"} default button "Cancel" giving up after intSecondsBeforeFade with icon aliasItunesIcon
      else --not enough time for user-defined fade-out parameters
        --adjust fade-out according to remaining time
        copy intSecondsRemain to intFadeLeadTime --our complete fade-out takes place within remaining seconds
        copy (intSecondsRemain) as integer to intFadeSteps --1 fade step for each second remaining
        copy pIntFadeAcceleration to intFadeAcceleration --no adjustment for this
      end if
      --proceed to fade out
        tell me
          copy output volume of (get volume settings) to intVolumeOriginal
          repeat with intVolumeNew from (intVolumeOriginal * intFadeSteps) to 0 by -intVolumeOriginal
            set volume output volume (intVolumeNew / intFadeSteps)
            delay (intFadeLeadTime / intFadeSteps / intFadeAcceleration)
          end repeat
          tell application "iTunes" to stop --stop iTunes before restoring sound volume
          set volume output volume intVolumeOriginal --bring the sound volume back to normal before sleeping
        end tell
        tell application "System Events" to sleep
      on error --if there’s an error during the fade-out, let’s just sleep
        tell application "System Events" to sleep
      end try
    end if
  end tell
end main

[ Reply to This | # ]