-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
iTunes.applescript
101 lines (93 loc) · 2.44 KB
/
iTunes.applescript
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
94
95
96
97
98
99
100
101
# homebridge-music/scripts/iTunes.applescript
# Copyright © 2016-2018 Erik Baauw. All rights reserved.
#
# Homebridge plugin for iTunes with Airplay speakers.
#
# Player: iTunes
# Speakers: iTunes Airplay devices
on getState()
set sp to getSpeakerStates()
tell application "iTunes"
if player state is playing then
set t to name of current track
else
set t to ""
end if
get "{\"on\":" & (player state is playing) & ",\"volume\":" & sound volume & ",\"track\":\"" & t & "\",\"speakers\":" & sp & "}"
end tell
end getState
on setPlayerOn(o, t)
tell application "iTunes"
if o then
play track t
set mute to false
else
stop
end if
if player state is playing then
set t to name of current track
else
# tell me to setAllSpeakersOff()
set t to ""
end if
get "{\"on\":" & (player state is playing) & ",\"track\":\"" & t & "\"}"
end tell
end setPlayerOn
on changeTrack(n)
tell application "iTunes"
if player state is playing then
if n then
next track
else
previous track
end if
end if
if player state is playing then
set t to name of current track
else
# tell me to setAllSpeakersOff()
set t to ""
end if
get "{\"on\":" & (player state is playing) & ",\"track\":\"" & t & "\"}"
end tell
end changeTrack
on setPlayerVolume(v)
tell application "iTunes"
set sound volume to v
get "{\"volume\":" & sound volume & "}"
end tell
end setPlayerVolume
on setSpeakerOn(sp, o)
tell application "iTunes"
set d to first AirPlay device whose name is sp
if o then
set selected of d to true
delay 1.0
set selected of first AirPlay device whose name is "Computer" to false
else
set selected of d to false
end if
get "{\"on\":" & selected of d & "}"
end tell
end setSpeakerOn
on setSpeakerVolume(sp, v)
tell application "iTunes"
set d to first AirPlay device whose name is sp
set sound volume of d to v
get "{\"volume\":" & sound volume of d & "}"
end tell
end setSpeakerVolume
on getSpeakerStates()
set text item delimiters to ","
tell application "iTunes"
set sp to {}
repeat with d in AirPlay devices
# Don't use id of Airplay device, as this changes when iTunes is restarted
copy "\"" & name of d & "\":{\"on\":" & active of d & ",\"volume\":" & sound volume of d & "}" to end of sp
end repeat
get "{" & sp & "}"
end tell
end getSpeakerStates
# on setAllSpeakersOff()
# tell application "iTunes" to set selected of every AirPlay device to false
# end setAllSpeakersOff