我在想通過shell腳本通過藍牙連接到我的iPhone。我目前正在使用一個基本上通過UIElements做到這一點的applescript,但我想知道這是否可以通過命令行實用程序a.l.a來完成。 blueutil,但有能力連接到設備,而不僅僅是打開/關閉藍牙?感謝您的考慮。在MacOSX上通過命令行連接到藍牙設備(iPhone)
-Afshin
我在想通過shell腳本通過藍牙連接到我的iPhone。我目前正在使用一個基本上通過UIElements做到這一點的applescript,但我想知道這是否可以通過命令行實用程序a.l.a來完成。 blueutil,但有能力連接到設備,而不僅僅是打開/關閉藍牙?感謝您的考慮。在MacOSX上通過命令行連接到藍牙設備(iPhone)
-Afshin
擺弄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
這個答案和@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
我不得不改變一下讓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")
之間的區別,但是....
同上優勝美地。感謝Dougal :-) –
這對高Sierra我不適用。菜單打開,但不會進入任何設備。 – raine
杜格爾的回答爲我工作。 您還可以使用Automator創建一項服務,該服務可以從任何位置運行,並在鍵盤快捷鍵首選項中爲其分配鍵盤快捷鍵,以實現更快的工作流程。
我有多個藍牙音頻設備。所以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
更新爲High Sierra 10.13。2,根據Andrew Burns和dougal提供的答案。
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
變化:
'
不如’
一樣...)希望這會幫助別人,而不是試圖竊取噶!
此工具1允許我從命令行連接到藍牙耳機。
這是在小牛?在10.9.2中不起作用。我得到:'錯誤「系統事件出現錯誤:應用程序未運行。」號碼-600' –
@AndrewBurns:是的,這是在小牛隊。但是我應該注意到,這個腳本假設你在菜單欄中啓用了藍牙圖標。 – Wolph
事實證明,這是一些其他問題,重新啓動固定'600'錯誤。然而,我確實得到了一個與您的解決方案非常相似的解決方案(請參閱我的答案)。 –