【问题标题】:AppleScript 在 Monterey (12.0.1) 中连接蓝牙设备
【发布时间】:2022-01-16 00:10:13
【问题描述】:

我想编写一个简单的脚本来将我的 MacBook 连接到客厅的蓝牙扬声器,但我不知道最后的点击。

-- Turn Bluetooth on
tell application "System Preferences"
    activate
    reveal pane "com.apple.preferences.Bluetooth"
end tell

delay 1

tell application "System Events"
    tell process "System Preferences"
        if button "Turn Bluetooth On" of window "Bluetooth" exists then
            click button "Turn Bluetooth On" of window "Bluetooth"
        end if
    end tell
end tell

-- Connect Living Room speakers
tell application "System Events"
    tell process "ControlCenter"
        
        set BluetoothButton to menu bar item "Bluetooth" of menu bar 1
        click BluetoothButton --This works and makes the dialog pop up
        
        delay 3
        
        set TheCheckbox to button "Living Room" of scroll area 1 of window "Control Centre"

        
        if TheCheckbox exists then return "Yay"
        
        if value of TheCheckbox is 0 then click TheCheckbox
    end tell
end tell

-- This is how to do it through the System Preferences
-- But this doesn't work because there's no way to double click the device in the bluetooth menu
(*set thePathPref to (path to library folder from system domain as text) & "PreferencePanes:"
tell application "System Preferences"
    activate
    reveal pane "com.apple.preferences.Bluetooth"
end tell

delay 1

tell application "System Events"
    tell process "System Preferences"
        if button "Turn Bluetooth On" of window "Bluetooth" exists then
            click button "Turn Bluetooth On" of window "Bluetooth"
        end if
        
        delay 1
        
        set LivingRoomButton to UI element "Living Room" of row 3 of table 1 of scroll area 1 of window "Bluetooth" of application process "System Preferences" of application "System Events"
        --if LivingRoomButton exists then
        click LivingRoomButton -- Needs to be a double click but that's impossible
        --end if
    end tell
end tell
*)

如果我

return every UI element of window "Control Centre"

我返回 {},这让我认为窗口“控制中心”不是寻找按钮的正确位置,但使用名为 UI 浏览器的应用程序,这绝对是它所在的位置。所以我不知道该怎么办。

就目前而言,我收到错误“系统事件出错:无法获取进程“控制中心”的窗口“控制中心”的滚动区域 1。索引无效。”就行了

set TheCheckbox to button "Living Room" of scroll area 1 of window "Control Centre"

【问题讨论】:

    标签: macos bluetooth applescript macos-monterey


    【解决方案1】:

    我没有蓝牙扬声器,但我使用以下脚本连接到我的 HomePod (Airplay)。 HomePod 显示在System Preferences -> Sound 下的列表中。如果您的蓝牙扬声器也出现在该列表中,则该脚本应该可以在 macOS 12 Monterey 上运行:

    set outPutSrc to "Living Room"
    
    tell application "System Preferences"
        
        reveal anchor "output" of pane id "com.apple.preference.sound"
        activate
        
        tell application "System Events"
            tell process "System Preferences"
                try
                    delay 1
                    select (row 1 of table 1 of scroll area 1 of tab group 1 of window "Sound" whose value of text field 1 is outPutSrc)
                end try
            end tell
        end tell
        
        quit
        
    end tell
    

    【讨论】:

    • 开箱即用的好想法。不幸的是,与 HomePods 不同的是,普通蓝牙扬声器在首次通过蓝牙连接之前不会出现在声音部分。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-08
    • 2020-01-27
    • 2016-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多