Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
(* 
  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 to app (path to frontmost application as Unicode text)

global itunes_state_
set itunes_state_ to my itunes_state()

prop parent : app "System Events"
tell parent to activate

if itunes_state_ is "playing" then
    tell app "iTunes" to set current_volume to sound volume
    set heyyou to "iTunes is cranked to " & current_volume & "." & ¬
        return & "Set the new volume (0-100):" --dialog message
    repeat
        display dialog heyyou default answer "0"
        set volume_ to text returned of result as number
        if volume_ is "0" then
            my fade by 0.01 from current_volume to 0
            exit repeat
        else
            try
                my fade by 0.01 from current_volume to volume_
                exit repeat
            on error
                beep
                set heyyou to "Please enter a number between 0 and 100."
            end try
        end if
    end repeat
else if itunes_state_ is "stopped" then
    tell app "iTunes" to set current_volume to sound volume
    repeat
        display dialog "iTunes is stopped." & return & ¬
            "The current volume is " & current_volume & "." & ¬
            return & "Begin playing at volume (1-100):" ¬
            default answer "70"
        set volume_ to text returned of result as number
        try
            my fade by 0.01 from 0 to volume_
            exit repeat
        on error
            beep
        end try
    end repeat
else if itunes_state_ is "" then
    beep
end if

tell 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
    tell app "iTunes"
        
        -- set the step size based on whether start or end volume is bigger
        set step_ to ((start_volume < end_volume) as integer * 2 - 1)
        
        repeat with i from start_volume to end_volume by step_
            set the sound volume to i
            delay delay_
        end repeat
    end tell
end fade

-- returns "playing", "stopped", or ""
on itunes_state()
    tell application "System Events"
        if process "iTunes" exists then
            tell app "iTunes"
                if player state is playing then
                    return "playing"
                else
                    return "stopped"
                end if
            end tell
        else
            return ""
        end if
    end tell
end itunes_state