0

I'm trying to retrieve a list of available AirPlay output audio devices, I don't have a preference on which language to use.

Before Ventura I used the following Apple Script:

set devices to {}

    tell application "System Preferences"
      reveal pane id "com.apple.preference.sound"
    end tell
    tell application "System Events"
      tell application process "System Preferences"
        repeat until exists tab group 1 of window "Sound"
        end repeat
        tell tab group 1 of window "Sound"
          click radio button "Output"
          tell table 1 of scroll area 1
            set selected_row to (first UI element whose selected is true)
            set currentOutput to value of text field 1 of selected_row as text
            
            repeat with r in rows
              try
                set deviceName to value of text field 1 of r as text
                set deviceType to value of text field 2 of r as text
                set end of devices to { deviceName, deviceType }
              end try
            end repeat
          end tell
        end tell
      end tell
    end tell

    if application "System Preferences" is running then
      tell application "System Preferences" to quit
    end if
    
    return [ devices, "currentOutput", currentOutput ]

But since the Ventura update this broken and it seems like there's no way to adapt it to work with Apple Script anymore.

Does anyone know how to update it to work with Ventura or could point me to the documentation of either Swift or ObjC to retrieve this list?

Fez Vrasta
  • 14,110
  • 21
  • 98
  • 160
  • I have only ever used it on **debian**, never **macOS** and have absolutely no idea if it will work for you or if it is even in the right direction, but my SONOS speakers show up as AirPlay devices if I use `avahi` mDNS service discovery. It appears to be on macPorts https://ports.macports.org/port/avahi/ YMMV, just trying to help. – Mark Setchell Nov 17 '22 at 17:25
  • Oh, on seeing Robert's answer I think you are looking for internal devices rather than network-attached stuff, so probably disregard my last. – Mark Setchell Nov 17 '22 at 17:30

1 Answers1

1

You can get system information for audio devices in the XML form, then parse it someway for audio devices names. I don't think following parsing is very good, but it works for me at least:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set theXML to do shell script "system_profiler SPAudioDataType -xml"

set {theXMLDoc, theError} to current application's NSXMLDocument's alloc()'s initWithXMLString:theXML options:(current application's NSXMLDocumentTidyHTML) |error|:(reference)
set {theMatches, theError} to (theXMLDoc's nodesForXPath:("//array") |error|:(reference))
if theMatches = missing value then error (theError's localizedDescription() as text)
set temp to (theMatches's firstObject()'s valueForKey:"stringValue") as text

set ATID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"_name", "coreaudio", "_properties"}
set tempItems to text items of temp
set AppleScript's text item delimiters to ATID

set audioDevicesNames to {}
repeat with anItem in tempItems
    set anItem to contents of anItem
    if anItem is "" or anItem begins with "_" then
        -- do nothing
    else
        set end of audioDevicesNames to anItem
    end if
end repeat
audioDevicesNames 
Robert Kniazidis
  • 1,760
  • 1
  • 7
  • 8