Last active
June 27, 2020 02:49
-
-
Save kevinjalbert/e39dca94b0e0eb63207479519fdfcd65 to your computer and use it in GitHub Desktop.
AppleScript to connect bluetooth headphones and show its battery level
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
activate application "SystemUIServer" | |
set deviceName to "Kevin Jalbert's BeatsX" | |
tell application "System Events" | |
tell process "SystemUIServer" | |
set bluetoothMenu to (menu bar item 1 of menu bar 1 whose description contains "bluetooth") | |
tell bluetoothMenu | |
click | |
set deviceMenuItem to (menu item deviceName of menu 1) | |
tell deviceMenuItem | |
click | |
if exists menu item "Connect" of menu 1 then | |
click menu item "Connect" of menu 1 | |
return "Connecting..." | |
else | |
set batteryLevelMenuItem to (menu item 3 of menu 1) | |
tell batteryLevelMenuItem | |
set batteryLevelText to title of batteryLevelMenuItem | |
end tell | |
key code 53 -- esc key | |
return batteryLevelText | |
end if | |
end tell | |
end tell | |
end tell | |
end tell |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No worries! Glad you got use out of it.