Tuesday, June 27, 2017

#1 2017-06-06 12:39:44 pm

eliasheuninck
Member
Registered: 2011-07-12
Posts: 21
Website

click UI button containing a specific text

Hi,

For an autonomous sound installation I'm trying to write a script that connects to 2 Bluetooth headsets.
Because I didn't find a way to do this in the command line, I'm trying to accomplish it with GUI scripting.
Thanks to all the good resources on this forum it's working!
The way it works now is that the script clicks the 'pair' button of the first, and later the second line of Bluetooth devices in the System Preference panel.

But now there might be a third bluetooth device present in the exhibition space which messes up the script.
I thought it would be helpful if the script could take the name of the Bluetooth device into account, but I couldn't get that to work yet.

I imagine some pseudo script as follows:

Applescript:


click button "Pair" of row containing "JABRA TALK"

One complication is that both headsets have the same name. But after successfully pairing, the button "Pair" changes into a (x) button, that might be a way to select only rows that have the "Pair" button.

This is the current script, where the connection to both headsets is made

Applescript:


tell application "System Events" to tell process "System Preferences"
   set frontmost to true
   delay 1
   tell window "Bluetooth"
       delay 0.1
       tell scroll area 1
           delay 0.1
           tell table 1
               delay 0.1
               tell row 2 -- FIRST JABRA
                   delay 0.1
                   tell UI element 1
                       delay 0.1
                       tell button 1
                           delay 0.3
                           click
                       end tell
                   end tell
               end tell
           end tell
       end tell
   end tell
end tell

delay 5
tell application "Finder"
   activate
   delay 5
end tell

tell application "System Events" to tell process "System Preferences"
   delay 0.5
   set frontmost to true
   delay 1
   tell window "Bluetooth"
       delay 0.1
       tell scroll area 1
           delay 0.1
           tell table 1
               delay 0.1
               tell row 4 -- SECOND JABRA, AFTER THE FIRST IS PAIRED
                   delay 0.5
                   delay 0.1
                   tell UI element 1
                       activate
                       delay 0.3
                       UI elements
                       delay 0.5
                       tell button 1
                           delay 0.5
                           click
                       end tell
                   end tell
               end tell
           end tell
       end tell
   end tell
end tell

It would be nice to get some pointers for a more robust script!

Thanks you!
Elias

Last edited by eliasheuninck (2017-06-06 12:40:21 pm)


Filed under: GUI, bluetooth

Offline

 

#2 2017-06-06 02:49:38 pm

t.spoon
Member
From: BFE, Massachusetts
Registered: 2013-01-12
Posts: 185

Re: click UI button containing a specific text

Have you tried the script here?
https://stackoverflow.com/questions/175 … -on-macosx

Last edited by t.spoon (2017-06-06 02:49:54 pm)


Hackintosh built February, 2012 |  Mac OS Sierra
GIGABYTE GA-Z68X-UD3H-B3 | Core i5 2500k | 16 GB DDR3 | GIGABYTE Geforce 1050 TI 4GB
250 GB Samsung 850 EVO | 4 TB RAID
Dell Ultrasharp U3011 | Dell Ultrasharp 2007FPb

Offline

 

#3 2017-06-12 01:29:47 pm

eliasheuninck
Member
Registered: 2011-07-12
Posts: 21
Website

Re: click UI button containing a specific text

Hi t.spoon, that was a great answer!

I looked at the script you linked to and learned enough from it to make it work as I wanted. Nice!

Just for reference: this is the full part where the script connects to the two headsets:

Applescript:

-- look for a row in the bluetooth scroll window with the name JABRA TALK.
-- If found, look for a button labeled "Pair". If so, click it. If not found, repeat.
tell application "System Events" to tell process "System Preferences"
   set frontmost to true -- system preferences seems to only update devices properly when in focus
   tell table 1 of scroll area 1 of window "Bluetooth"
       set pairedJabras to 0
       repeat until pairedJabras is 2
           log "pairedJabras has a value of " & pairedJabras
           try
               set JABRA_row to (row (pairedJabras + 1) where value of static text 1 of UI element 1 is "JABRA TALK")
               tell JABRA_row
                   select -- if I don't do this the script will click the remove button of the previous row
                   delay 1
                   try
                       set JABRA_ButtonLabel to description of button 1 of UI element 1 -- get name of button
                       if ((JABRA_ButtonLabel as string) is equal to "Pair") then
                           log "JABRA_ButtonLabel: " & JABRA_ButtonLabel
                           try
                               click button 1 of UI element 1
                               log "Clicked Pair-button of Jabra " & pairedJabras + 1
                               set pairedJabras to pairedJabras + 1
                               delay 1
                           on error
                               log "error trying to click Pair-button of Jabra " & pairedJabras + 1
                           end try
                       else
                           log "Button label is not the same as PAIR"
                       end if
                   on error
                       log "no button found to get value from. We already paired Jabra " & pairedJabras + 1
                       set pairedJabras to pairedJabras + 1
                   end try
               end tell
           on error
               log "no Jabra's found yet"
           end try
           delay 4
           log "repeat"
       end repeat
   end tell
end tell
log "The 2 headsets are connected"

Thank you!
Elias

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.038 seconds, 8 queries executed ]

RSS (new topics) RSS (active topics)