Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

iTunes fade in/out

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)

Posted on Dec 24, 2009 4:22 AM

Reply
7 replies

Dec 24, 2009 4:42 AM in response to Chongy

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

Dec 24, 2009 10:02 AM in response to Chongy

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

Jan 13, 2010 11:43 PM in response to Camelot

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

iTunes fade in/out

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.