9

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

-阿夫辛

4

8 回答 8

10

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

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

截至 2014 年 4 月 11 日,这适用于 OSX Mavericks 10.9.2,尽管您可能需要在安全首选项面板中授予对用于运行此功能的任何方法的访问权限。请参阅此 Apple 知识库: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
于 2014-04-11T17:23:09.767 回答
6

我必须稍作改动才能让Andrew Burns 的答案在优胜美地为我工作;他的代码不断给我

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")但是......

于 2015-04-28T21:23:33.043 回答
5
于 2018-01-01T02:29:14.207 回答
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
于 2014-02-19T17:41:14.710 回答
2

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

于 2018-01-04T09:58:23.567 回答
0

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

于 2013-07-08T06:17:28.617 回答
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
于 2017-01-08T13:41:54.560 回答
0

道格尔的回答对我有用。您还可以使用 Automator 创建可以在任何地方运行的服务,并在键盘快捷键首选项中为其分配键盘快捷键,以实现更快的工作流程。

于 2015-07-08T21:09:23.070 回答