【问题标题】:AppleScript to toggle BluetoothAppleScript 切换蓝牙
【发布时间】:2013-11-14 04:04:02
【问题描述】:

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

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

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

【问题讨论】:

    标签: macos bluetooth applescript osx-mavericks


    【解决方案1】:

    这在 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 $?
    

    【讨论】:

      【解决方案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>
      

      【讨论】:

        【解决方案3】:

        AppleScript 解决方案

        步骤

        1. 打开Automater.app
        2. 在搜索栏中输入 Run AppleScript 并选择打开一个新的脚本窗口。
        3. 在屏幕顶部的设置中将工作流接收设置为no inputinany application
        4. 复制并粘贴下面的脚本并保存文件。
        5. 创建键盘快捷键:System Preferences > Keyboard > Shortcuts > Services > 新脚本显示在General 在已保存的名称下。 > 设置快捷方式。

        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
        
        

        参见:Setting Bluetooth with AppleScript in Big Sur - r/applescript,12/1/20

        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 Error - Can’t get group 1 of window "Control Center" - StackExchange,21 年 11 月 10 日

        第三方

        对于 macOS Big Sur,Enrique Scherer 的回答不再有效。但是,blueutil 实用程序 has been updated 可以从自制软件中安装。

        【讨论】:

          【解决方案4】:
          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
          

          【讨论】:

          • 包括 wifi 切换 - 在高山上测试
          • 我需要运行此脚本 2 次才能在 macOS Mojave 10.14.5 上打开蓝牙
          【解决方案5】:

          为我工作,没有蓝色实用程序:

          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
          

          【讨论】:

            【解决方案6】:

            有时我的 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!"
            

            【讨论】:

              【解决方案7】:

              这是一个适用于 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
              

              【讨论】:

                猜你喜欢
                • 2012-05-03
                • 1970-01-01
                • 1970-01-01
                • 2016-09-09
                • 2012-07-02
                • 2020-07-25
                • 1970-01-01
                • 1970-01-01
                • 2022-01-16
                相关资源
                最近更新 更多