-Afshin
这个答案与@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
我不得不做一些更改才能使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的第一项菜单栏项
之间的区别,但是...
根据Andrew Burns和dougal提供的答案,更新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
更新内容:
'
并不等同于’
......)希望这可以帮助其他人,没有意图抢夺他人运气!
在尝试了一些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
error "System Events got an error: Application isn’t running." number -600
。 - Andrew Burnsblueutil
启用BT(如果它关闭了),然后使用BluetoothConnector
连接到我的Airpods。 - Bert Zangleset 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
Dougal的答案对我很有用。 您还可以使用Automator创建一个服务,该服务可以从任何地方运行,并在键盘快捷方式首选项中分配一个键盘快捷方式,以实现更快的工作流程。