iTunes DJ AppleScript: Fade to Next Track

By David Battino
May 29, 2009 | Comments: 16

A radio DJ who runs her set from iTunes asked me if I could modify one of my fade-out AppleScripts. She wanted to end a song on demand and make iTunes crossfade into the next song on the playlist.

iTunes-Fade-Advance.jpg

I don't think there's a way to trigger a crossfade in iTunes, though there are DJ programs that can do that. However, I did find a way to fade the current song and then immediately start the next. While I was at it, I added Growl notification to display the title of the new song. One interesting glitch I discovered is that fading iTunes all the way to 0 creates a click, so I fade it to 1 instead. It's amazing how this simple fading technique sounds so much better than snapping to the next song.

Here's the code. Because the blog template may mess up the formatting, here's a downloadable zip version as well. See the Growl site for information on configuring that section.


(*
db iTunes Fade-and-Advance
By David Battino, Batmosphere.com
Version 2009-03-03JJ
Based on ideas from Doug's AppleScripts and Mac OS Hints

See www.oreillynet.com/mac/blog/2007/03/itunes_fadeout_script.html for background

This script fades out iTunes if it's playing and then plays the next song.
If iTunes is paused, the script will start playback.
*)

global okflag
set okflag to false
set front_app to (path to frontmost application as Unicode text) -- So we can switch back to this after running the fade


-- Growl config; see http://growl.info
tell application "System Events"
set isRunning to ¬
(count of (every process whose name is "GrowlHelperApp")) > 0
end tell

if isRunning then
tell application "GrowlHelperApp"
-- Make a list of all the notification types that this script will ever send:
set the allNotificationsList to ¬
{"Song Notification"}

-- Make a list of the notifications that will be enabled by default.
-- Those not enabled by default can be enabled later
-- in the 'Applications' tab of the growl prefpane.
set the enabledNotificationsList to ¬
{"Song Notification"}

-- Register our script with growl.
-- You can optionally (as here) set a default icon
-- for this script's notifications.
register as application ¬
"Growl Song ID" all notifications allNotificationsList ¬
default notifications enabledNotificationsList ¬
icon of application "iTunes"
end tell
end if

-- check if iTunes is running
tell application "System Events"
if process "iTunes" exists then
set okflag to true --iTunes is running
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
--Fade down (fading all the way to 0 causes a click, so fade to 1)
repeat with i from currentvolume to 1 by -1 -- Adjust to taste; bigger numbers fade faster
set the sound volume to i
delay 0.01 -- Adjust this to change fadeout duration (delete this line on slower Macs)
end repeat

delay 0.5 -- Silence between tracks; adjust to taste

next track

--Fade up new track
repeat with i from 1 to currentvolume by 2 -- Adjust to taste; bigger numbers fade faster
set the sound volume to i
delay 0.01 -- Adjust this to change fadeout duration (delete this line on slower Macs)
end repeat

-- Get track name
set theCurrentTrack to current track
set theBand to artist of theCurrentTrack as string
set theSong to name of theCurrentTrack as string
--Where is the track name stored on URL tracks?!
--set theRadioSong to the comment of theCurrentTrack as string
if (get class of theCurrentTrack) is URL track then
set theSong to genre of theCurrentTrack
set theBand to name of theCurrentTrack
end if
if isRunning then
tell application "GrowlHelperApp"

notify with name ¬
"Song Notification" title ¬
"Now Playing . . ." description """ & theSong & """ & " by " & theBand ¬
application name "Growl Song ID"

end tell
end if
exit repeat
end repeat
else
set the sound volume to 0
play
repeat with j from 0 to currentvolume by 2 --try by 4 on slower Macs
set the sound volume to j
end repeat
end if
end tell
tell application front_app
activate
end tell
end if


You might also be interested in:

16 Comments

nice...just reset apple-right arrow to use this in place of the built-in next track.

How do I reset my right arrow to run this script on my mac?
Thanks

@Troy: Many people use applications like Quicksilver (http://code.google.com/p/blacktree-alchemy) to configure key commands. You can also try opening System Preferences, selecting "Keyboard and Mouse," selecting "Keyboard Shortcuts," and then assigning the keystroke you want.

Thanks alot, this is a kind of fun. Waiting to see if the next track mixes right is funny, although it can surprise you.

Thank you for this. Elegant and useful!

Thanks! It was also easy to modify this script to pause/play. Now I have the fade in/fade out I love so much in Foobar on my Windows box. Thanks again!

This is wonderful!

I've been trying to figure how to do a presentation with separate controls for audio tracks and slideshow visuals. This will let me use iTunes for the audio, and I can talk as long as I want and then fade down to the next track; meanwhile I'll control the slides over top! That's pretty easy.

Thank you! Hurray for Applescript!

this is a Great script! i'm using it because i have no DJ skills. is there any way that it can be integrated into iTunes or Automator and for my MacBook's Apple Remote (IR) to control the skip from track to track?

Absolutely amazing!

This will help me immensely!

Digging it, great for my extra mouse button!! cherios

Hi

I'm completely new to AppleScript but I have been searching desperately for away to fade out of songs on iTunes, so this fix looks perfect.

I placed the script exactly as it was downloaded ( db iTunes-fade-out-in.scpt ) in Library > Scripts > Applications > iTunes

Now I'm stumped. I closed and reopened iTunes and skipped through a few tracks but couldn't notice any fade out. Do I need to alter the settings in the script so that the fade out is more pronounced (i.e. longer in duration, or louder in volume) or do I need to install this Growlr program?? Or, am I missing some other step?

Thanks so much in advance for any help!

Amazing Blog !!!

how can i modify it so that, the track playing gets faded out, but the next song coming in, plays at full volume, rather than fading in?

Thanks David !! I've wanted this for so long!

@ Alex - this script works systemwide, - you don't need to be in itunes, try my setup below (download spark).
@ Loius - I agree - check my mod below..

I've adapted your script to use like so:
I've downloaded the free little app Spark and set a global hotkey to Cmd+Alt+Right Arrow to trigger the script. This compliments my shortcuts in Sizzling Keys for iTunes. SK handles the growl business for me, so I've edited out the notification from your script, and have set the next track to play at current_volume immediately, no fade in.

(grab whats between the [code] tags and save as a script as per davids instructions.. not sure if its right, but it seems to work for me :)

[code]

(*
db iTunes Fade-and-Advance
By David Battino, Batmosphere.com
Version 2009-03-03JJ
Based on ideas from Doug's AppleScripts and Mac OS Hints
Crudely hacked at by Tim - not sure if growl functions work - haven't tested that

See www.oreillynet.com/mac/blog/2007/03/itunes_fadeout_script.html for background

This script fades out iTunes if it's playing and then plays the next song.
If iTunes is paused, the script will start playback.
*)

global okflag
set okflag to false
set front_app to (path to frontmost application as Unicode text) -- So we can switch back to this after running the fade
set isRunning to false

-- you can change these values to taste
set use_growl to false
set fade_up to false

-- Growl config; see http://growl.info
if use_growl then
tell application "System Events"
set isRunning to ¬
(count of (every process whose name is "GrowlHelperApp")) > 0
end tell

if isRunning then
tell application "GrowlHelperApp"
-- Make a list of all the notification types that this script will ever send:
set the allNotificationsList to ¬
{"Song Notification"}

-- Make a list of the notifications that will be enabled by default.
-- Those not enabled by default can be enabled later
-- in the 'Applications' tab of the growl prefpane.
set the enabledNotificationsList to ¬
{"Song Notification"}

-- Register our script with growl.
-- You can optionally (as here) set a default icon
-- for this script's notifications.
register as application ¬
"Growl Song ID" all notifications allNotificationsList ¬
default notifications enabledNotificationsList ¬
icon of application "iTunes"
end tell
end if
end if

-- check if iTunes is running
tell application "System Events"
if process "iTunes" exists then
set okflag to true --iTunes is running
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
--Fade down (fading all the way to 0 causes a click, so fade to 1)
repeat with i from currentvolume to 1 by -1 -- Adjust to taste; bigger numbers fade faster
set the sound volume to i
delay 0.01 -- Adjust this to change fadeout duration (delete this line on slower Macs)
end repeat

delay 0.5 -- Silence between tracks; adjust to taste

next track

--Fade up new track
if fade_up then
repeat with i from 1 to currentvolume by 2 -- Adjust to taste; bigger numbers fade faster
set the sound volume to i
delay 0.01 -- Adjust this to change fadeout duration (delete this line on slower Macs)
end repeat
else
set the sound volume to currentvolume
end if

-- Get track name
set theCurrentTrack to current track
set theBand to artist of theCurrentTrack as string
set theSong to name of theCurrentTrack as string
--Where is the track name stored on URL tracks?!
--set theRadioSong to the comment of theCurrentTrack as string
if (get class of theCurrentTrack) is URL track then
set theSong to genre of theCurrentTrack
set theBand to name of theCurrentTrack
end if
if isRunning then
tell application "GrowlHelperApp"

notify with name ¬
"Song Notification" title ¬
"Now Playing . . ." description "“" & theSong & "”" & " by " & theBand ¬
application name "Growl Song ID"

end tell
end if
exit repeat
end repeat
else
if fade_up then
set the sound volume to 0
play
repeat with j from 0 to currentvolume by 2 --try by 4 on slower Macs
set the sound volume to j
end repeat
else
play
end if
end if
end tell
tell application front_app
activate
end tell
end if
[/code]

is that so hard to show people the damn way to use your script?

News Topics

Recommended for You

Got a Question?