Applescript 连接蓝牙设备

Applescript 连接蓝牙设备

我正在尝试创建一个 applescript 以便我通过其蓝牙 ID 连接到蓝牙设备。

到目前为止,我已经设法让 AppleScript 在蓝牙关闭的情况下将其打开。代码如下:

# This is only necessary, if AppleScripts are not yet allowed to change checkboxes
tell application "System Events" to set UI elements enabled to true
# Now change the bluetooth status
  tell application "System Preferences"
    set current pane to pane id "com.apple.preferences.bluetooth"
      tell application "System Events"
        tell process "System Preferences"
        # Enabled is checkbox number 2
        if value of checkbox 2 of window "Bluetooth" is 0 then
            click checkbox 2 of window "Bluetooth"
        end if
    end tell
end tell
quit
end tell

有人知道是否以及如何设置新的蓝牙设备,以及是否可以根据设备名称/设备蓝牙 ID 连接到设备吗?

我也尝试过在 Automator 中记录该操作,但对于“设置新设备”选项,Automator 只告诉我:“单击“”按钮”。谢谢

答案1

我之所以能做到,是因为此链接由 @mu3 在评论中提供。以下是 Apple 脚本:

activate application "SystemUIServer"
tell application "System Events"
    tell process "SystemUIServer"
        -- Working CONNECT Script.  Goes through the following:
        -- Clicks on Bluetooth Menu (OSX Top Menu Bar)
        --    => Clicks on device Item
        --      => Clicks on Connect Item
        set btMenu to (menu bar item 1 of menu bar 1 whose description contains "bluetooth")
        tell btMenu
            click
            tell (menu item "Beats Solo³ de Anthonin" of menu 1)
                click
                if exists menu item "Connect" of menu 1 then
                    click menu item "Connect" of menu 1
                    return "Connecting..."
                else
                    key code 53 -- Close main BT drop down if Connect wasn't present
                    return "Connect menu was not found, are you already connected?"
                end if
            end tell
        end tell
    end tell
end tell

您所要做的就是将“Beats Solo³ de Anthonin”替换为您的设备名称,如果您的计算机不是英文的,请将“Connect”替换为您的语言的翻译。

希望这可以帮助 :)

相关内容