2013-07-08 164 views
6

我在想通过shell脚本通过蓝牙连接到我的iPhone。我目前正在使用一个基本上通过UIElements做到这一点的applescript,但我想知道这是否可以通过命令行实用程序a.l.a来完成。 blueutil,但有能力连接到设备,而不仅仅是打开/关闭蓝牙?感谢您的考虑。在MacOSX上通过命令行连接到蓝牙设备(iPhone)

-Afshin

回答

0

据我所知,你只能开启/关闭蓝牙,检查在命令行状态(使用blueutillaunchctl操作)。但是你可以在shell中通过osascript使用你的applescript例程。

4

摆弄AppleScript的一点后,我写了AppleScript的这一点点连接:

注意“显示Bluetooth菜单栏”复选框需要,因为它是有效的只是使用菜单要在此。

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 
+0

这是在小牛?在10.9.2中不起作用。我得到:'错误“系统事件出现错误:应用程序未运行。”号码-600' –

+0

@AndrewBurns:是的,这是在小牛队。但是我应该注意到,这个脚本假设你在菜单栏中启用了蓝牙图标。 – Wolph

+0

事实证明,这是一些其他问题,重新启动固定'600'错误。然而,我确实得到了一个与您的解决方案非常相似的解决方案(请参阅我的答案)。 –

8

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

#尼斯输出只要将它保存在一个文件中,并调用osascript path/to/file.applescript

这是工作在OSX小牛10.9.2截至2014年4月11日,尽管您可能需要授予对用于在安全性首选项面板中运行此操作的任何方法的访问权限。看到这个苹果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's answer在优胜美地为我工作;他的代码一直给我

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 

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

+0

同上优胜美地。感谢Dougal :-) –

+0

这对高Sierra我不适用。菜单打开,但不会进入任何设备。 – raine

0

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

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 
2

更新为High Sierra 10.13。2,根据Andrew Burnsdougal提供的答案。

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键解决,按this answer确认并在this page确认
  • 检查以查看设备是否在Bluetooth菜单中列出,以检测不正确的名称。 (我花了很长一段时间的调试,才知道'不如一样...)

希望这会帮助别人,而不是试图窃取噶!

0

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

相关问题