Ah, interruptions. These days, when the phone rings or someone butts in, I hit the F13 key on my Mac. If music is playing, iTunes smoothly fades it down to silence and pauses itself. After the coast is clear, I hit F13 again and the Mac asks what volume I’d like playback to resume at. Hit Return and the music fades back up. The effect is more pleasing than simply stabbing the Mute key, and it means I don’t lose my place. Further, being able to enter a volume directly is often nicer than messing with the volume up/down keys.

itunes-stop-shadow.jpg

Launch the script when iTunes is playing, and you’re prompted to enter a new volume level. The default is 0, creating a fadeout.

It’s all done through this AppleScript I assembled from snippets at OSX Hints and Doug’s AppleScripts, plus some experimentation. I mapped it to the F13 key using the no-longer-available LumaCode Key Xing, but it should work fine from other AppleScript launchers.

itunes-start-shadow.jpg

Launch the script when iTunes is stopped, and you’re prompted to enter a new volume level; it defaults to the level it was at when you paused the music. iTunes will then fade in and start playing.

I’ve pasted the script below. Because it’s tough to display code examples in our narrow blog format, here’s a link to download it. Please let me know if you have suggestions.


(* 
  db iTunes Vol, Play, or Fade
  By David Battino, Batmosphere.com
  Based on ideas from Doug's AppleScripts and Mac OS Hints
  
  This script fades out iTunes if it's playing and fades it in if it's stopped.
*)

global okflag --Update 2007-06-07: changed from "property okflag : false"
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

-- 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"
		if (player state is playing) then
			set currentvolume to the sound volume
			set heyyou to "iTunes is cranked to " & currentvolume & "." & return & ¬
				"Set the new volume (0-100):" --dialog message
			repeat
				tell application "Finder"
					set dialogResult to display dialog heyyou default answer "0" with icon 1
				end tell
				if the text returned of dialogResult is "0" then
					set savedVolume to the sound volume
					--Fade down	
					repeat with i from savedVolume to 0 by -1 --try by -4 on slower Macs
						set the sound volume to i
						delay 0.01 -- Adjust this to change fadeout duration (delete this line on slower Macs)
					end repeat
					pause
					--Restore original volume
					set the sound volume to savedVolume
					exit repeat
				else
					try
						set the sound volume to text returned of dialogResult as number
						exit repeat
					on error
						beep
						set heyyou to "Please enter a number between 0 and 100."
					end try
				end if
			end repeat
		else
			repeat
				set dialogResult to display dialog "iTunes is stopped." & return & "The current volume is " & the sound volume & "." & return & ¬
					"Begin playing at volume (1-100):" default answer "70" with icon 1
				try
					set the sound volume to text returned of dialogResult as number
					play
					exit repeat
				on error
					beep
				end try
			end repeat
		end if
	end tell
	tell application front_app
		activate
	end tell
end if

UPDATE, 2007-03-05:Thanks to Gavin’s request, here’s a version that fades in and fades between values:


(* 
  db iTunes Vol, Play, or Fade, version 2 -- now with fade-in
  By David Battino, Batmosphere.com
  Based on ideas from Doug's AppleScripts and Mac OS Hints
  
  This script fades out iTunes if it's playing and fades it in if it's stopped.
*)

global okflag --Update 2007-06-07: changed from "property okflag : false" 
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

-- 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"
		if (player state is playing) then
			set currentvolume to the sound volume
			set heyyou to "iTunes is cranked to " & currentvolume & "." & return & ¬
				"Set the new volume (0-100):" --dialog message
			repeat
				tell application "Finder"
					set dialogResult to display dialog heyyou default answer "0" with icon 1
				end tell
				if the text returned of dialogResult is "0" then
					set savedVolume to the sound volume
					--Fade down	
					repeat with i from savedVolume to 0 by -1 --try by -4 on slower Macs
						set the sound volume to i
						delay 0.01 -- Adjust this to change fadeout duration (delete this line on slower Macs)
					end repeat
					pause
					--Restore original volume
					set the sound volume to savedVolume
					exit repeat
				else
					try
						--make fade direction negative if target volume is lower than current volume
						set fadeDirection to 1 --try 4 on slower Macs
						set targetVolume to (text returned of dialogResult as number)
						if targetVolume < currentvolume then
							set fadeDirection to -1 --try -4 on slower Macs
						end if
						repeat with k from currentvolume to (text returned of dialogResult as number) by fadeDirection
							set the sound volume to k
							--delay 0.01 -- Adjust this to change fadeout duration (delete this line on slower Macs)
						end repeat
						exit repeat
					on error
						beep
						set heyyou to "Please enter a number between 0 and 100."
					end try
				end if
			end repeat
		else
			repeat
				set dialogResult to display dialog "iTunes is stopped." & return & "The current volume is " & the sound volume & "." & return & ¬
					"Begin playing at volume (1-100):" default answer "70" with icon 1
				try
					set the sound volume to 0 --2007-03-20 script update to fix startup glitch
					play
					repeat with j from 0 to (text returned of dialogResult as number) by 1 --try by 4 on slower Macs
						set the sound volume to j
						--delay 0.01 -- Adjust this to change fadeout duration (delete this line on slower Macs)
					end repeat
					
					exit repeat
				on error
					beep
				end try
			end repeat
		end if
	end tell
	tell application front_app
		activate
	end tell
end if

And now what Gavin really wanted, here’s a version that simply fades in or fades out with no dialogs:


(* 
  db iTunes Fade-in/out
  By David Battino, Batmosphere.com
  Based on ideas from Doug's AppleScripts and Mac OS Hints
  
  This script fades out iTunes if it's playing and fades it in if it's stopped.
*)

global okflag --Update 2007-06-07: changed from "property okflag : false"
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

-- 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	
				repeat with i from currentvolume to 0 by -1 --try by -4 on slower Macs
					set the sound volume to i
					delay 0.01 -- Adjust this to change fadeout duration (delete this line on slower Macs)
				end repeat
				pause
				--Restore original volume
				set the sound volume to currentvolume
				exit repeat
			end repeat
		else
                        set the sound volume to 0 --2007-03-20 script update to fix startup glitch
			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