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

时间:2013-07-08 05:41:55

标签: iphone macos shell bluetooth

我正试图找出一种通过蓝牙与shell脚本连接到我的iPhone的方法。我目前正在使用一个基本上通过UIElements执行此操作的AppleScript,但我想知道是否可以使用命令行实用程序a.l.a来完成此操作。 blueutil但是能够连接到设备而不仅仅是打开/关闭蓝牙?谢谢你的考虑。

-Afshin

8 个答案:

答案 0 :(得分:9)

这个答案与@Wolph的答案非常相似;然而,在与其他问题作斗争之后,这就是我想出来的。我更喜欢它有两个原因:   #如果设备已经连接,它将不会断开连接   #osascript

的输出不错

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

截至2014年4月11日,这是在OSX Mavericks 10.9.2上工作,尽管您可能需要在安全性偏好设置面板中授予对用于运行此操作的任何方法的访问权限。请参阅此Apple 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

答案 1 :(得分:6)

我必须改变一点才能让Andrew Burns's answer在约塞米蒂为我工作;他的代码一直在给我

  

connect-mouse.scpt:509:514:执行错误:系统事件出错:无法获取应用程序“SystemUIServer”的菜单栏1的菜单栏项目3的菜单1。索引无效。 (-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

我不知道(first menu bar item whose description is "bluetooth") of menu bar 1(menu bar item 1 of menu bar 1 where description is "bluetooth")之间的区别,但......

答案 2 :(得分: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

答案 3 :(得分:3)

根据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

变更:

  • 点击蓝牙图标不再关闭菜单。通过点击退出键解决,按this answer并在this page
  • 上确认
  • 检查设备是否列在蓝牙菜单中,以检测错误的名称。 (我花了很长时间调试,只是意识到'不一样......)

希望这有助于其他人,而不是试图窃取业力!

答案 4 :(得分:2)

这个工具1允许我从命令行连接到蓝牙耳机。

答案 5 :(得分:0)

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

答案 6 :(得分:0)

Dougal的回复对我有用。 您还可以使用Automator创建可以从任何地方运行的服务,并在键盘快捷方式首选项中为其指定键盘快捷键,以实现更快的工作流程。

答案 7 :(得分:0)

我有多个蓝牙音频设备。所以Andrew的脚本非常有用。这是我对他的剧本的修改。我不是程序员/ 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