在MacOSX上通过命令行连接蓝牙设备(iPhone)

9
我正在试图通过一个shell脚本连接我的iPhone蓝牙。我目前使用的是AppleScript,它基本上通过UIElements实现了这一点,但我想知道是否可以使用命令行实用程序来完成,类似于blueutil,但具有连接设备而不仅仅是打开/关闭蓝牙的功能?感谢您的考虑。
-Afshin
8个回答

11

这个答案与@Wolph的答案非常相似,但在解决其他问题后,我得出了以下解决方法。 我喜欢它有两个原因: # 如果设备已经连接,它不会断开设备的连接 # osascript 输出的结果很好

只需将其保存在文件中,然后调用osascript path/to/file.applescript即可。

截至2014年4月11日,这在OSX Mavericks 10.9.2上有效,尽管您可能需要在安全性偏好设置面板中授权使用此方法。请参阅此苹果KB:http://support.apple.com/kb/HT5914

你只需要更改"LG HBS730"字符串以匹配你的设备名称,然后你就可以设置好了。这里添加了返回语句,所以你可以从osascript获取详细的输出结果。

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 LG HBS730 Item
    --      => Clicks on Connect Item
    set btMenu to (menu bar item 1 of menu bar 1 where description is "bluetooth")
    tell btMenu
      click
      tell (menu item "LG HBS730" of menu 1)
        click
        if exists menu item "Connect" of menu 1
          click menu item "Connect" of menu 1
          return "Connecting..."
        else
          click btMenu -- 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

6

我不得不做一些更改才能使Andrew Burns的答案在Yosemite上运行;他的代码一直给我报错:

connect-mouse.scpt:509:514: execution error: System Events got an error: Can’t get menu 1 of menu bar item 3 of menu bar 1 of application process "SystemUIServer". Invalid index. (-1719)

似乎用那种方式选择菜单栏项目可以让你点击它,但无法进入菜单,由于某些难以理解的AppleScript原因。这个非常相似的代码对我有效:

tell application "System Events" to tell process "SystemUIServer"
  set bt to (first menu bar item whose description is "bluetooth") of menu bar 1
  click bt
  tell (first menu item whose title is "The Device Name") of menu of bt
    click
    tell menu 1
      if exists menu item "Connect"
        click menu item "Connect"
        return "Connecting..."
      else
        click bt  -- close main dropdown to clean up after ourselves
        return "No connect button; is it already connected?"
      end if
    end tell
  end tell
end tell

我不知道菜单栏1中描述为“蓝牙”的第一菜单栏项描述为“蓝牙”的菜单栏1的第一项菜单栏项之间的区别,但是...


同样适用于Yosemite。谢谢Dougal :-) - Jamie Garroch - MVP
这在我的High Sierra上不起作用。菜单会打开,但它不会进入任何设备。 - raine
一个注意事项,如果您的设备名称中有撇号(例如,“Bob's Airpods”),则可能需要使用非标准字符。似乎在手机上配置的设备通常使用U+2019字符,而我们的IDE通常使用U+0027。我复制/粘贴了花式撇号,然后这个就可以正常工作了。我使用Alfred来触发它,只需要几分之一秒。 - TimSum

5

根据Andrew Burnsdougal提供的答案,更新High Sierra 10.13.2的内容。

set DeviceName to "LG HBS730"

tell application "System Events" to tell process "SystemUIServer"
    set bt to (first menu bar item whose description is "bluetooth") of menu bar 1
    click bt
    if exists menu item DeviceName of menu of bt then
        tell (first menu item whose title is DeviceName) of menu of bt
            click
            tell menu 1
                if exists menu item "Connect" then
                    click menu item "Connect"
                    return "Connecting..."
                else
                    key code 53 -- hit Escape to close BT menu
                    return "No connect button; is it already connected?"
                end if
            end tell
        end tell
    else
        key code 53 -- hit Escape to close BT menu
        return "Cannot find that device, check the name"
    end if
end tell

更新内容:

  • 点击蓝牙图标不再关闭菜单。根据这个答案,通过按下 Escape 键来解决,并在这个页面上得到确认。
  • 检查设备是否在蓝牙菜单中列出,以检测不正确的名称。(我花了相当长的时间进行调试,结果发现'并不等同于......)

希望这可以帮助其他人,没有意图抢夺他人运气!


4

在尝试了一些Applescript之后,我写了这个小小的Applescript来连接:

请注意,“在菜单栏中显示蓝牙”复选框需要打开,因为它实际上只是使用那个菜单。

tell application "System Events"
    tell process "SystemUIServer"
        tell (menu bar item 1 of menu bar 1 whose description is "bluetooth")
            click
            -- You can use your phone name as well over here if you have multiple devices
            -- tell (menu item "YOUR_PHONE_NAME_HERE" of menu 1)
            tell (menu item 1 of menu 1)
                click
                tell (menu item 1 of menu 1)
                    click
                end tell
            end tell
        end tell
    end tell
end tell

这是在Mavericks系统中吗?在我的10.9.2版本中无法工作。我收到了以下错误信息:error "System Events got an error: Application isn’t running." number -600 - Andrew Burns
@AndrewBurns:是的,它在Mavericks中。但我应该指出,这个脚本假定你已经在菜单栏中启用了蓝牙图标。 - Wolph
事实证明,这是另一个问题,重新启动解决了“600”错误。然而,我最终得到了一个与你的解决方案非常相似的解决方案(请参见我的答案)。 - Andrew Burns

2
这个1工具可以让我通过命令行连接蓝牙耳机。

我本来也要发布同样的链接(这将是重复的答案),因为你没有列出工具的名称。我在这里回答了一个类似的问题here。我确认BluetoothConnector对我也有效。 - luckman212
可以确认BluetoothConnector正在执行预期的任务。我的脚本(绑定到键盘快捷键)首先使用blueutil启用BT(如果它关闭了),然后使用BluetoothConnector连接到我的Airpods。 - Bert Zangle
请注意,blueutil可以通过Homebrew进行安装。 - Moshe Weitzman

0
我有多个蓝牙音频设备。所以安德鲁的脚本非常有帮助。这是我的修改后的脚本。我不是程序员/IT人员,只是从互联网上学了一些知识。
set btchoice to BT_Choice()

on BT_Choice()

display dialog "Choose the device of your choice" with title "Selecting Device" buttons {"Bluedio", "Reconnect S-TS", "Anker A7721"} default button "Reconnect S-TS"

set Ndialogresult to the result
set DNameSel to button returned of Ndialogresult

display dialog "Whether to Connect or Disconnect the Device" with title "Handling Bluetooth" buttons {"Connect", "Disconnect", "Cancel"} default button "Connect"

set Bdialogresult to the result
set Bbuttonsel to button returned of Bdialogresult

activate application "SystemUIServer"
tell application "System Events"
    tell process "SystemUIServer"

        set btMenu to (menu bar item 1 of menu bar 1 where description is "bluetooth")
        tell btMenu
            click
            tell (menu item DNameSel of menu 1)
                click
                if exists menu item Bbuttonsel of menu 1 then
                    click menu item Bbuttonsel of menu 1
                    return "Connecting..."
                else
                    click btMenu -- 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
end BT_Choice

0
据我所知,您只能在命令行中打开/关闭蓝牙并检查状态(使用blueutil或通过launchctl进行操作)。但是,您可以通过osascript在shell中使用您的AppleScript例程。

0

Dougal的答案对我很有用。 您还可以使用Automator创建一个服务,该服务可以从任何地方运行,并在键盘快捷方式首选项中分配一个键盘快捷方式,以实现更快的工作流程。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接