【问题标题】:How can I list the available AirPlay output devices on macOS?如何在 macOS 上列出可用的 AirPlay 输出设备?
【发布时间】:2022-11-18 01:25:59
【问题描述】:

我正在尝试检索可用 AirPlay 输出音频设备的列表,但我对使用哪种语言没有偏好。

在 Ventura 之前,我使用了以下 Apple 脚本:

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 ]

但自从 Ventura 更新后,这个问题似乎就没有办法让它适应 Apple Script 了。

有谁知道如何更新它以与 Ventura 一起使用,或者可以指出 Swift 或 ObjC 的文档来检索此列表?

【问题讨论】:

  • 我只用过它德比安, 绝不苹果系统并且完全不知道它是否适合你,或者它是否在正确的方向上,但如果我使用avahimDNS 服务发现,我的 SONOS 扬声器显示为 AirPlay 设备。它似乎在 macPorts ports.macports.org/port/avahi YMMV 上,只是想提供帮助。

标签: applescript macos-ventura


【解决方案1】:

您可以获取 XML 格式的音频设备的系统信息,然后以某种方式解析它以获得音频设备名称。我认为以下解析不是很好,但至少对我有用:

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 AppleScript's text item delimiters to {"_name", "coreaudio", "_properties"}

set audioDevicesNames to {}
repeat with anItem in (text items of temp)
    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

【讨论】:

    猜你喜欢
    • 2020-09-01
    • 2018-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-22
    • 1970-01-01
    相关资源
    最近更新 更多