Help getting/setting current bluetooth sound/output device
Posted: Thu Jan 23, 2020 12:04 am
Have 2 scripts I need help converting. One gets the current bluetooth sound output device, the other sets it. This worked for me for a long time and now doesn't. Any suggestions on where to start? I'm decent w/ Python.
- Code: Select all
####### this one would get the current output device
# Indigo no longer supports execution of embedded AppleScripts. For help
# translating AppleScripts to python see: http://indigodomo.com/applescript
#
# The original AppleScript was:
#
# tell application "System Preferences"
# #reveal pane id "com.apple.preference.sound"
# #activate
# activate
# set current pane to pane "com.apple.preference.sound"
# end tell
#
# tell application "System Events"
# tell application process "System Preferences"
# #delay 1
#
# #wait for tab group 1 to open
# repeat until exists tab group 1 of window "Sound"
# end repeat
#
# tell tab group 1 of window "Sound"
#
# #delay 1
# tell table 1 of scroll area 1
# #delay 1
# set selected_row to (first UI element whose selected is true)
# set currentOutput to value of text field 1 of selected_row as text
# #display dialog currentOutput
# tell application "IndigoServer"
# log "current_output: " & currentOutput
# set value of variable "sound_current_output" to currentOutput
# end tell
# end tell
# end tell
# end tell
# end tell
#
#
####### this external script would set it #######
with timeout of 60 seconds
tell application "IndigoServer"
set value of variable "bt_associate_scpt_running" to "True"
set the_audio_dev to value of variable named "audio_dev"
log "bt_associate_n_select.scpt: bt associating to: " & the_audio_dev
end tell
#escape
tell application "System Events"
key code 53
end tell
delay 0.2
#escape
tell application "System Events"
key code 53
end tell
delay 0.2
#escape
tell application "System Events"
key code 53
end tell
tell application "System Events" to tell process "SystemUIServer"
set bt to (first menu bar item whose description is "bluetooth") of menu bar 1
delay 1
click bt
delay 2 # 1 fails sometimes
tell (first menu item whose title contains the_audio_dev) of menu of bt
click
# wait a while for menu to refresh, sometime updates slow
delay 4
tell menu 1
delay 3
#if exists menu item whose title contains "isconnect" then
if exists menu item "Disconnect" then
tell application "IndigoServer"
log "bt_associate_n_select.scpt: already assocated! Will try to select it"
end tell
#escape
tell application "System Events"
key code 53
end tell
delay 0.2
tell application "System Events"
key code 53
end tell
delay 0.2
tell application "System Events" to tell process "SystemUIServer"
set bt to (first menu bar item whose description contains "volume") of menu bar 1
delay 1
click bt
delay 2 # 1 fails sometimes
tell (first menu item whose title contains the_audio_dev) of menu of bt
click
end tell
end tell
else if exists menu item "Connect" then
#else if exists menu item whose title contains "Connect" then
tell application "IndigoServer"
log "bt_associate_n_select.scpt: Connecting (associating)..."
end tell
# hope one of these select the Connect item
click
click menu item "Connect"
#select return key
tell application "System Events"
key code 36
end tell
tell application "System Events" to tell process "SystemUIServer"
set bt to (first menu bar item whose description contains "volume") of menu bar 1
delay 1
click bt
delay 2 # 1 fails sometimes
tell (first menu item whose title contains the_audio_dev) of menu of bt
click
end tell
end tell
tell application "IndigoServer"
log "bt_associate_n_select.scpt: after Connect, if this fails cycle BT"
end tell
else
tell application "IndigoServer"
log "bt_associate_n_select.scpt: didn't find connect or disconnect"
end tell
end if
end tell
end tell
end tell
end timeout
tell application "IndigoServer"
set value of variable "bt_associate_scpt_running" to "False"
log "bt_associate.scpt: bt associating end: " & the_audio_dev
end tell