AppleScript for connecting to Airplay devices

Hi there

Im struggling to make a script to auto connect music app to my airplay speakers

could anyone help

Many thanks

Matt

My preset contains such a group.
Code in the next comment.

To display the currently used Device (up to two):

if application "Music" is running then
	tell application "Music"
		try
			set AirPlayName1 to name of the first AirPlay device whose selected is true
		end try
		try
			set AirPlayName2 to name of the second AirPlay device whose selected is true
		on error
			set AirPlayName2 to ""
		end try
	end tell
	set AirPlayDevices to AirPlayName1 & "
" & AirPlayName2
	
else
	set AirPlayDevices to "AirPlay" & "
" & "Devices"
end if

return AirPlayDevices
--code by @Caliguvara

To get a list of all speakers you need several widgets (I use the Touch Bar). Each widget should display this script:

if application "Music" is running then
	tell application "Music"
		set listOfAirPlayNames to (get name of every AirPlay device)
		set AirPlayDevice to the first item of listOfAirPlayNames
		return AirPlayDevice
	end tell
else
	return ""
end if
--code by @Caliguvara

The executable script of the widget would be

tell application "Music"
	set listOfAirPlayNames to (get name of every AirPlay device)
	set chosenAirPlayName to the first item of listOfAirPlayNames
	set current AirPlay devices to AirPlay device chosenAirPlayName
end tell
--code by @Caliguvara

Make as many widgets as you have receivers. Replace "first" with "second", "third",… in each script. Et voilà! Hope it helps.

thank you for the reply

it works really well thanks!

hi there one issue I've ran into is that when the music app connects to my home pod, the rest of the buttons don't work, is there a way for it to deselect the HomePod or anything you've com across?

Thanks

What do you mean?

so ive got three buttons: Mac, bedroom (airplay 2 speaker) and HomePod

once I select my bedroom, I can't switch back to my Mac speakers but can to the homepod

if I select the HomePod I can't switch back

This is weird. I just tried it here and can switch with ease through all my devices using the group. :man_shrugging:t4:

right, so I haven't use ive only used some of the code, ill download the preset and try and extract it :slight_smile:
thanks for help :smiley:

this stuff is really good, how do you do the buttons? like the app drawer working as on/off and being dynamic, I'm fairly new to all this stuff and would really appreciate it

The most important question first - does the AirPlay Group work as desired? :smiley:


When you say "App Drawer", what are you referring to? :thinking:

it should, just gonna copy and paste into my preset!

on the right most side with the 4 icons, when you tap one it opens X, inverts its colours and upon a second tap it closes. is it just opening itself with an inverted icon or something clever going on?

Rather use the "Move to Preset" Menu Bar function!

Oh yeah, on the right I still used the old way of functioning. Maybe I should launch an update… The buttons open hidden groups, I placed them in each and every group I created (If you want to check they way of working you can find them at the button of the list of actions in the All Apps section, right above the Finger Swipes). F.eg. the calendar group contains a button that does not open the Calendar group, but closes the currently open Touch Bar. I basically just copied the standard functions, and replaced the color and the function just in that group. Also, if you use totally black icons you don't even need the change their color, BTT will switch it all on its own.


In the version I'm running for me personally I created groups that are visible in each Subgroup but themselves (makes it easier when I proceed on changes of the design or content of these groups).



perfect! thank you, I can't seem to get the AirPods to connect using your version?

the airplay button is great!

Right so, my music app Re-arranges the order for the output options upon selecting one of them. do you have a work around or..?

Yes and no. The rearrangement comes because Music rearranges the groups. I coded something earlier today, gonna post the code tomorrow :+1:t4:

Insomnia, hello.

So, what I did is: I named the widgets just with the AirPlay devices Name, just what I wanted to see. Then, I replaced the code I posted with this one:

if application "Music" is running then
	tell application "Music"
		try
			set listOfAirPlayNames to (get name of every AirPlay device whose selected is true)
		on error
			return "{\"background_color\": \"75,75,75,255\"}"
		end try
		if listOfAirPlayNames contains "Computer" then
			return "{\"background_color\": \"145,145,145,255\"}"
		else
			return "{\"background_color\": \"75,75,75,255\"}"
		end if
	end tell
else
	return ""
end if
--code by @Caliguvara

Replace "Computer" in the middle with the exact name of one of your AirPlay Receivers. Change it for each widget. Also, I execute this widget every 2 seconds as soon as it becomes visible, because it gets a bit highlighted to show which Devices are currently used (the HomePod in the Example).

The associated action:

tell application "Music"
	set current AirPlay devices to AirPlay device "Computer"
end tell
--code by @Caliguvara

Replacing, once again, "Computer" with the corresponding AirPlay device.

Et voilà!

Is there a possibility to set the airplay target for the whole Mac, instead of just for the Music app?