Page 1 of 1

Help getting/setting current bluetooth sound/output device

PostPosted: Thu Jan 23, 2020 12:04 am
by mediabox
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

Re: Help getting/setting current bluetooth sound/output devi

PostPosted: Thu Jan 23, 2020 7:44 am
by whmoorejr
Can the same be achieved through the built in Airfoil pro plugin?

Re: Help getting/setting current bluetooth sound/output devi

PostPosted: Thu Jan 23, 2020 10:09 am
by jay (support)
Before we spend a lot of time trying to convert this script, can we take a step back and get a more full/complete description of what problem this script is addressing? This is a good opportunity to describe the use-case fully and see if there's another approach to solving the issue.

Re: Help getting/setting current bluetooth sound/output devi

PostPosted: Sun Feb 02, 2020 1:09 pm
by mediabox
Hi. Have a few bluetooth devices that I would send audio to. I would default to the Amazon Echo in the kitchen so announcements like 'motion detected <location>, 'garage door opened', 'package outside by the front door', 'east/west gate opened', etc, can be heard. Also I could send music to another bluetooth device connected to the outside patio speakers. I have an elaborate music page that selects the sound output and streams youtube.com, tunein.com, or iTunes music (most of it doesn't work because they were written in AppleScript) . Also, in the early days I would send music to the Master Bed Echo and use it as an alarm clock and push music to it and ramp up the volume gradually. So my other script would do: is music streaming to the patio? if not the attach to the Kitchen echo. Would run this every hour.

I looked into Airfoil earlier but decided against it. Don't recall why now. Maybe it wasn't free?

For now I really need it to connect to the Kitchen Echo to get the announcements. If everything else worked the way it did before that is bonus. Usually(?) when the Mini boots it goes straight to "Internal Speakers", or if someone takes over the Echo in the kitchen because they want to share something on the phone, or sometime it's just on "Internal Speakers" for no apparent reason. For now I've got Airfoil (non pro) configured to attached to Kitchen Echo every hour. I think it's working... Thanks @whmoorejr for that tip. Airfoil says "Trial Mode" in the corner so I'm afraid it will stop working soon?

Re: Help getting/setting current bluetooth sound/output devi

PostPosted: Mon Feb 03, 2020 9:55 am
by whmoorejr
It's been a few years since I did the Airfoil trial.... I think when the trial is over, Airfoil continues to work, but it times out after a minute or something. So, it will still work for pushing out an announcement, but if you are using it to connect speakers for music, it will cut out mid song.

For announcements only, there are scriptable ways to push announcements directly to Alexa devices. I have an echo in the kitchen, 4 dots around the house and an echo flex in each kids bedroom. They announce when the doorbell rings, who enters their code on the keypad to get in, their chores for the day, etc. It works good, but there is a slight (30 second-ish) delay as the script has to run and communicate with mamma amazon.... Credit autolog for putting this script together. If you follow along on the post, it's pretty much cut-paste with the scripts. https://forums.indigodomo.com/viewtopic.php?f=138&t=20535&start=16

My python abilities are pretty much limited to command-c / command-v.

If all of your home audio speakers are echo/alexa devices, you can use Plex to listen to your home iTunes music collection. When you ask Plex to play an audio track, "Plex, play dubstep playlist", it creates a mini Plex player in the amazon device and will play the playlist you created in Plex Manager (which can run on your Indigo Mac, another Mac or in my case, on my QNap external drive.) Then say, Echo, play on all devices" and you get whole house that way. If you ask Plex to play a video/movie title, it will look for whatever video device is set up as your default player (living room tv, appleTV4, etc) and will start playing the video on that device. What I haven't tinkered with yet..... If you use the Plex Manager plugin, you can see it when an Alexa device creates a plex player. I haven't tried to use Plex (from within indigo) to push to my Alexa devices. I've only pulled it from the Alexa side. So that may be another option for pushing music out to your Alexa devices.

Sorry again about my lack of help on the coding side of your question.

Re: Help getting/setting current bluetooth sound/output devi

PostPosted: Mon Feb 03, 2020 1:22 pm
by mediabox
30sec delay. Yikes. Do like that it can send to all Echo/Dot's at the same time. I do push music not only announcements, so still searching...

I'm decent at python, it's really one of the reasons I selected Indigo.

Saw Airfoil stall/lock up on me once already. Not too keen on it. Today, Airfoil limits trial by adding noise to audio after 10 mins - Boo! Also can't figure out how to auto-select the [source].

Anyhow, Instead of converting all this AppleScript I have, would something like this work (for the not so trivial to convert AppleScripts):

https://stackoverflow.com/questions/8552823/get-safari-and-chrome-opened-tabs-on-mac-with-appcelerator

Thinking of doing the external/file call to a python script since some of these scripts take longer than 10 seconds, and I get the nag/kill alert when my scripts take too long.

Re: Help getting/setting current bluetooth sound/output devi

PostPosted: Mon Feb 03, 2020 2:18 pm
by jay (support)
appscript is no longer supported and I believe it stopped working several years ago (we used to include it in the Indigo installation).

Given all the information, I think you could refactor your AppleScript to remove the direct "tell application IndigoServer" such that the information needed from Indigo was passed in through an AppleScript handler. Then, you can use py-applescript to call the handlers, passing in the appropriate information.