7 Replies Latest reply: Jan 28, 2010 9:59 PM by Agroking
Chongy Level 1 Level 1 (15 points)
Hi,

I'm trying to have iTunes fade out of one track, then fade into the next track in a playlist.

The code below fades out the current track, then starts playing the next song, but without fading back in again.

Any help is much appreciated.

Thank you.

tell application "iTunes"
set currentvolume to the sound volume
if (player state is playing) then
repeat
repeat with i from currentvolume to 0 by -2
set the sound volume to i
delay 0.01
end repeat
next track
set the sound volume to currentvolume
exit repeat
end repeat
else
set the sound volume to 0
play
repeat with j from 0 to currentvolume by 2
set the sound volume to j
delay 0.02
end repeat
end if
end tell

PowerBook G4 15", Mac OS X (10.5.8)
  • Chongy Level 1 Level 1 (15 points)
    This is a similar script, but this only fades one song out, then back in again after pausing the track.

    global okflag
    set okflag to false
    set front_app to (path to frontmost application as Unicode text)

    tell application "System Events"
    if process "iTunes" exists then
    set okflag to true
    end if
    end tell

    if okflag is true then
    tell application "iTunes"
    set currentvolume to the sound volume
    if (player state is playing) then
    repeat
    repeat with i from currentvolume to 0 by -2
    set the sound volume to i
    delay 0.01
    end repeat
    pause
    set the sound volume to currentvolume
    exit repeat
    end repeat
    else
    set the sound volume to 0
    play
    repeat with j from 0 to currentvolume by 2
    set the sound volume to j
    delay 0.02
    end repeat
    end if
    end tell
    tell application front_app
    activate
    end tell
    end if
  • Camelot Level 8 Level 8 (46,080 points)
    The code below fades out the current track, then starts playing the next song, but without fading back in again.


    Sure, that's what you tell it to do:

    next track
    set the sound volume to currentvolume


    You specifically tell it to go to the next track then reset the volume to the original volume level.

    Looking at the code I think the problem is one of flow - your code has an if statement:

    if (player state is playing) then


    then you take one of two options - if the player is playing you fade down (and then play the next track).
    If the player isn't playing, you start a new track and fade up.

    Nowhere in your code do you fade up if you're in the 'already playing' state.

    So the fix is simple - replace the 'else' statement with an 'end if'. The reality is that if you're already playing a track you want to fade it out, then you want to fade in the next track regardless of whether you just faded out or not.

    Here's your fixed code:

    tell application "iTunes"
      -- get the initial volume
      set currentvolume to the sound volume
      -- are we playing?
      if (player state is playing) then
        -- if so, fade out the current track
        repeat with i from currentvolume to 0 by -2
          set the sound volume to i
          delay 0.01
        end repeat
        next track
      end if

      -- now, regardless of whether we were playing or not, fade in the next track
      set the sound volume to 0
      play
      repeat with j from 0 to currentvolume by 2
        set the sound volume to j
        delay 0.02
      end repeat
    end tell
  • Chongy Level 1 Level 1 (15 points)
    Thank you so much for your help! Script works perfectly

    Do you know if it is possible to apply a similar script to work with DVD Player?

    Cheers
  • Chongy Level 1 Level 1 (15 points)
    I would like to request more information.
  • Chongy Level 1 Level 1 (15 points)
    Hi Camelot,

    Could you please tell me if it's possible to automate an AppleScript within iTunes, so the code you've given me will work automatically when leaving one track and beginning the next, without me being required to run the Script manually?

    Thank you for your help, and I hope to hear back from you soon
  • Agroking Level 1 Level 1 (0 points)
    You should use this plugin, it is exactly what you need! I use it all the time.
  • Agroking Level 1 Level 1 (0 points)
    Whoops, forgot to post it:
    http://www.wildbits.com/accubeatmix/