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:


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


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

Have you tried the script here?
https://stackoverflow.com/questions/17519804/connect-to-bluetooth-device-iphone-via-command-line-on-macosx

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:

-- 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