(*
db iTunes Vol, Play, or Fade
By David Battino, Batmosphere.com
Based on ideas from Doug's AppleScripts and Mac OS Hints
Code cleaned up and flattened by Jacob Rus
This script fades out iTunes if it's playing and fades it in if it's stopped.
*)-- So we can switch back to this after running the fade
global front_app
set front_app toapp(path to frontmost applicationasUnicode text)global itunes_state_
set itunes_state_ tomy itunes_state()prop parent :app"System Events"tell parent to activate
if itunes_state_ is"playing"thentellapp"iTunes"toset current_volume tosound volume
set heyyou to"iTunes is cranked to "& current_volume &"."&¬return&"Set the new volume (0-100):"--dialog message
repeatdisplay dialog heyyou default answer "0"set volume_ totext returned ofresultasnumberif volume_ is"0"thenmy fade by 0.01from current_volume to0exitrepeatelsetrymy fade by 0.01from current_volume to volume_
exitrepeaton errorbeepset heyyou to"Please enter a number between 0 and 100."end tryend ifend repeatelse if itunes_state_ is"stopped"thentellapp"iTunes"toset current_volume tosound volume
repeatdisplay dialog"iTunes is stopped."&return&¬"The current volume is "& current_volume &"."&¬return&"Begin playing at volume (1-100):"¬
default answer "70"set volume_ totext returned ofresultasnumbertrymy fade by 0.01from0to volume_
exitrepeaton errorbeepend tryend repeatelse if itunes_state_ is""thenbeepend iftell front_app to activate
-- fade iTunes to the end_volume, delaying "delay" seconds for each step
to fade by delay_ from start_volume to end_volume
tellapp"iTunes"-- set the step size based on whether start or end volume is bigger
set step_ to((start_volume < end_volume)asinteger*2-1)repeat with i from start_volume to end_volume by step_
set the sound volumeto i
delay delay_
end repeatend tellend fade
-- returns "playing", "stopped", or ""
onitunes_state()tellapplication"System Events"ifprocess"iTunes"existsthentellapp"iTunes"ifplayer stateis playing thenreturn"playing"elsereturn"stopped"end ifend tellelsereturn""end ifend tellenditunes_state