Control Volume of Airplay Speakers

Sets volume of each speaker individually

-- Written by Gwen Baker 26/11/2016, updated 16/10/2018

-- http://www.baker-net.com

-- Selects AirPlay speakers and sets volume.

-- Variable apsp is the Speaker's name as it appears on AirPlay list in iTunes

-- Variable sndLevel:

-- "1" = Apple TV selected but Volume Off (I can use Apple TVs remote if I need to pause)

-- "6" = Highest, "2" = Lowest

-- "0" will deselect unwanted speaker or TV, in case device is already selected in iTunes.

 

tell application "iTunes"

set shuffle enabled to false

-- Setting Computer volume to 0 instead of deselecting.  If no other speakers are selected, deselect option could fail.

set sound volume of AirPlay device "Computer" to 0

end tell

 

-- Set Variables

set curTime to time string of (current date) as Unicode text

log curTime

-- Kitchen speaker

set apsp to "AQ"

set sndLevel to "5"

my APVolume(apsp, sndLevel)

 

-- Kitchen Apple TV

set apsp to "Kitchen TV"

set sndLevel to "1"

my APVolume(apsp, sndLevel)

 

-- Dressing room

set apsp to "AQ2"

set sndLevel to "5"

my APVolume(apsp, sndLevel)

 

-- Master Bath

set apsp to "AQ3"

set sndLevel to "5"

my APVolume(apsp, sndLevel)

 

-- Bedroom Sonos

set apsp to "Bedroom"

set sndLevel to "4"

my APVolume(apsp, sndLevel)

 

-- Bedroom Apple TV if not too early in the morning

set apsp to "Bedroom TV"

if curTime > "09:00:00" then

set sndLevel to "1"

else

-- I don't want my BR TV to light up in the morning. This will deselect it in iTunes.

set sndLevel to "0"

end if

my APVolume(apsp, sndLevel)

 

-- Sonos Surround system

set apsp to "Media Room"

set sndLevel to "5"

my APVolume(apsp, sndLevel)

 

-- SR Apple TV

set apsp to "Sitting room"

set sndLevel to "1"

my APVolume(apsp, sndLevel)

 

-- Upstairs Landing

set apsp to "SRS"

set sndLevel to "6"

my APVolume(apsp, sndLevel)

 

-- Dining Room Surround system

set apsp to "Surround"

set sndLevel to "4"

my APVolume(apsp, sndLevel)

 

-- DR Apple TV

set apsp to "Dining"

set sndLevel to "1"

my APVolume(apsp, sndLevel)

 

--log current volume and reset. This helps if I need to adjust the volume settings on individual speakers. (View Log, Messages tab)

--Using Try statements so that code continues if a speaker is offline; otherwise it would error and stop.

 

on APVolume(apsp, sndLevel)

try

tell application "iTunes"

if AirPlay device apsp is available then

set apname to (get name of AirPlay device apsp)

set apvol to (get sound volume of AirPlay device apsp)

set aplev to sndLevel

set apinfo to apname & "     " & apvol

log apinfo

set selected of AirPlay device apsp to true

if sndLevel = "6" then

set sound volume of AirPlay device apsp to 25

else if sndLevel = "5" then

set sound volume of AirPlay device apsp to 15

else if sndLevel = "4" then

set sound volume of AirPlay device apsp to 10

else if sndLevel = "3" then

set sound volume of AirPlay device apsp to 7

else if sndLevel = "2" then

set sound volume of AirPlay device apsp to 4

else if sndLevel = "1" then

set sound volume of AirPlay device apsp to 0

else if sndLevel = "0" then

set selected of AirPlay device apsp to false

end if

end if

end tell

end try

end APVolume