5

在 OS X 10.8 中,我有一个简洁的小 AppleScript,我用来快速切换蓝牙而不使用鼠标。

我更新到 10.9,向系统偏好设置添加了几个 UI 更改。除其他外,它取代了将蓝牙从复选框切换到按钮的元素。我的脚本现在坏了,因此我的工作流程也坏了。

问题是按钮的名称根据其状态从“打开蓝牙”更改为“关闭蓝牙”。我对 AppleScript 没有足够的了解来找出解决方法,并且想知道你们是否可以帮助我。

4

7 回答 7

8

这在 10.9 中对我有用:

tell application "System Preferences"
    reveal pane "com.apple.preferences.Bluetooth"
end tell
tell application "System Events" to tell process "System Preferences"
    click button 6 of window 1
end tell
quit application "System Preferences"

你也可以使用blueutil

/usr/local/bin/blueutil|grep -q 'Power: 1';/usr/local/bin/blueutil power $?
于 2013-11-03T14:12:17.590 回答
2

AppleScript 解决方案

脚步

  1. 打开Automater.app
  2. 在搜索栏中输入Run AppleScript并选择打开一个新的脚本窗口。
  3. 屏幕顶部的设置中设置工作流接收。no input any application
  4. 复制并粘贴下面的脚本并保存文件。
  5. 创建键盘快捷方式:系统偏好设置>键盘>快捷方式>服务> 新脚本显示在保存名称下的常规下。> 设置快捷方式。

macOS 大苏尔 11.5

蓝牙切换.workflow

tell application "System Events"
    tell process "ControlCenter"
        set BluetoothButton to menu bar item "Bluetooth" of menu bar 1
        click BluetoothButton
        delay 1
        set OnSwitch to checkbox "Bluetooth" of group 1 of window "Control Center"
        click OnSwitch
    end tell
    key code 53
end tell

请参阅:在 Big Sur 中使用 AppleScript 设置蓝牙- r/applescript,2020 年 12 月 1 日

macOS 蒙特雷 12.0.1

蓝牙切换.workflow

tell application "System Events"
    tell application process "Control Center"
        click menu bar item "Bluetooth" of menu bar 1
        tell window "Control Center"
            try
                click checkbox "Bluetooth"
            on error
                click checkbox "Bluetooth"
            end try
        end tell
    end tell
    key code 53 -- # escape key
end tell

请参阅:AppleScript 错误 - 无法获取窗口“控制中心”的第 1 组- StackExchange,21 年 11 月 10 日

第三者

对于 macOS Big Sur,Enrique Scherer 的回答不再有效。但是,blueutil 实用程序已经更新,例如可以从 homebrew 安装。

于 2021-07-06T10:22:55.143 回答
2

这在 10.15.6 中对我有用,我的解决方案可能过于复杂,即运行脚本 1(关闭蓝牙)然后运行脚本 2(打开蓝牙)。

脚本 1. 这是用于关闭蓝牙

tell application "System Events" to tell process "SystemUIServer"
    tell (menu bar item 1 of menu bar 1 where description is "bluetooth")
        click
        click menu item "Turn Bluetooth Off" of menu 1
    end tell
    tell window 1
        click button "Turn Bluetooth Off"
    end tell
end tell

脚本 2. 这是为了打开蓝牙

tell application "System Events" to tell process "SystemUIServer"
    tell (menu bar item 1 of menu bar 1 where description is "bluetooth")
        click
        click menu item "Turn Bluetooth On" of menu 1
    end tell
end tell

所以我执行一个命令,它将一个接一个地运行一个脚本,睡眠是为了让 UI 正确更新。

osascript bluetooth_off.scpt && sleep 3s && osascript bluetooth_on.scpt

您可以将命令保存在文件中并使用以下命令执行它:(它们必须位于同一目录中)。

~ bash <fileName>
于 2020-12-24T13:03:20.930 回答
1
tell application "System Preferences"
    reveal pane "com.apple.preferences.Bluetooth"
end tell
tell application "System Events" to tell process "System Preferences"
    repeat until exists window "Bluetooth"
    end repeat
    try
        click button "Turn Bluetooth Off" of window "Bluetooth"
        do shell script "networksetup -setairportpower airport off"
    on error
        click button "Turn Bluetooth On" of window "Bluetooth"
        do shell script "networksetup -setairportpower airport on"
    end try
end tell
tell application "System Preferences" to quit
于 2018-02-25T22:47:08.397 回答
0

为我工作,没有蓝色工具:

tell application "System Preferences"
    reveal pane id "com.apple.preferences.Bluetooth"
    -- activate

    set the current pane to pane id "com.apple.preferences.Bluetooth"

    try
        tell application "System Events" to tell process "System Preferences"
            click button "Turn Bluetooth Off" of window "Bluetooth"

            click button "Turn Bluetooth Off" of sheet 1 of window "Bluetooth" of application process "System Preferences" of application "System Events"
        end tell

        delay 1

    on error
        tell application "System Events" to tell process "System Preferences"
            click button "Turn Bluetooth On" of window "Bluetooth"
            quit
        end tell

    end try

end tell
于 2014-11-09T02:05:25.033 回答
0

这是一个适用于 macOS 12 的 GUI 脚本。它也与语言无关:

tell application "System Preferences"
    set current pane to pane "com.apple.preferences.Bluetooth"
    delay 0.5
end tell

tell application "System Events"
    tell application process "System Preferences"
        click button 1 of window "Bluetooth"
    end tell
end tell
于 2022-02-13T20:24:34.567 回答
0

有时我的 Mac 会断开与我的 Logitech MX Master 3 的连接,因此需要快速切换蓝牙。但是我在关闭笔记本电脑的情况下工作,所以一旦我关闭蓝牙,我就会失去对计算机的输入,直到我实际打开它

此脚本通过关闭蓝牙、等待 5 秒然后重新打开来解决问题

当我的鼠标连接断开,但我仍然可以使用键盘时,我导航到包含此脚本的目录,命令 + 向下箭头打开它,命令 + R 运行它。5 秒后,我的鼠标又回来了!

切换蓝牙.scpt:

display dialog "Toggle Bluetooth?"

-- turn bluetooth off
tell application "System Events" to tell process "SystemUIServer"
    tell (menu bar item 1 of menu bar 1 where description is "bluetooth")
        click
        click menu item "Turn Bluetooth Off" of menu 1
    end tell
end tell

-- wait 5 seconds
delay 5

-- turn bluetooth on
tell application "System Events" to tell process "SystemUIServer"
    tell (menu bar item 1 of menu bar 1 where description is "bluetooth")
        click
        click menu item "Turn Bluetooth On" of menu 1
    end tell
end tell

display dialog "Welcome Back!"
于 2021-05-12T00:49:47.693 回答