Connect a Bluetooth Mouse Using a Macro

Hi

I believe that its not easy to select a menu item from the top tool bar in finder but maybe there is somebody clever that knows a method (BTW I could get this to work in QuicKeys).

So how do I select 'Connect' for my Bluetooth mouse?

18

Try this AppleScript:

tell application "System Events" to tell process "SystemUIServer"
	set bt to (first menu bar item whose description is "bluetooth") of menu bar 1
	click bt
	tell (first menu item whose title contains "mouse") of menu of bt
		click
		tell menu 1
			if exists menu item "Connect" then
				click menu item "Connect"
				say "Connected"
			else
				click menu item "Disconnect"
			end if
		end tell
	end tell
end tell

Hi Jim

Thanks for the quick response and it does work but it's also selecting another menu item (2 to the left) which is Little Snitch - Show Network Monitor. Can you please suggest a minor tweak to stop this happening.

Andrew

Hi Andrew,

I have no idea why Little Snitch is being activated, unless it has Bluetooth in its name.

I am at a loss. Sorry!

Jim

I agree with @Jim that is very strange.
Try running this script in Script Editor (or better, Script Debugger 7, and post the results.

tell application "System Events" to tell process "SystemUIServer"
  set bt to (first menu bar item whose description is "bluetooth") of menu bar 1
  click bt
  
  set menuList to (title of every menu item of menu of bt)
  set menuList to item 1 of menuList
  
  return menuList
  
end tell

1 Like

Hi JMichaelTX

So I tried your modified script and that opened Bluetooth tool menu and the Little Snitch window like before see the attached screen dump. I also downloaded Script Debugger 7 and ran your code, which only opened the Bluetooth menu but I don't understand what I need to do or how to save debug results to post, please advise.

Andrew

I presume this is more difficult to solve than first thought. Just one extra piece of information, if after triggering the macro when the Little Snitch window appears, I trigger the sam macro again, the LS window closes and mouse remains connected.

As a test, I added the close window command to the macro so after a short delay the Keystroke Cmd W is actioned but this doesn't close the window.

Andrew

Hi JMichaelTX

I know its been a LONG since you answered this question and I should have updated you before what I found, so please accept my apology. The reason Little Snitch was launching was I had assigned the same keyboard shortcut to this macro and Little Snitch, my stupid error. I have been using your macro successfully for some years now, but when I updated to Ventura, it all stopped working.

Could you or Jim please offer a script that works in Ventura? I know there were issues with AppleScript in Ventura but I am running the latest version macOS and I have other macros also don't run either.

I have another question about running an AppleScript, is there a
way to actually see how the script actions? I have tried several options in the pulldown menu, but no output is produced.

Andrew

Hey Andrew,

I'm sad to say that @JMichaelTX will not be able to respond...

The Passing of JMichaelTX

JMichaelTX – Unforgettable Generosity With His Time and Knowledge

Take Care,
Chris

(Keyboard Maestro Moderator)

Hi Chris

Sorry to hear of the sad news, I am sure he will missed by many.

Andrew

1 Like

The best way to dig into AppleScripts is in a proper script editor like Script Debugger (the free version does most of what you'll need) or the less-featured Script Editor that's already installed on your machine.

The "results" in your screenshot are whatever is returned from the entire AppleScript, either explicitly with a return statement or implicitly by the last statement in the script. Demo:

AS Results.kmmacros (3.6 KB)

Image

But back to the actual problem...

Ventura has moved things around -- the Bluetooth menu item is now under the "ControlCenter" process rather than "SystemUIServer". There are also some "interesting" omissions to the Accessibility interfaces of many menu bar items, which might scupper any attempt to update your script... I'll try and have look when I'm next at a Ventura machine but, given that your menu will probably be different to mine, can you post a screenshot of how it looks now and note the item you want to select?

1 Like

This might get you started. For my Bluetooth menu:

image

...and to connect/disconnect the soundbar (which I'm guessing is similar to what you want to do):

tell application "System Events"
	tell process "ControlCenter"
		set theMenu to item 1 of (get every menu bar item of menu bar 1 whose description is "Bluetooth")
		click theMenu
		delay 1
		click checkbox 1 of scroll area 1 of group 1 of window "Control Centre"
		delay 1
		click theMenu
	end tell
end tell

I've left long pauses in there so you can see what happens, but you can reduce those to whatever your system is comfortable with. You do need them though because the menu is dynamic and you need to allow "build time" before clicking the checkbox item.

AFAIK, all devices are in scroll area 1... etc and the checkboxes are numbered from top to bottom, so you should be able to count down the list and replace checkbox 1 with checkbox <position-in-list>.

But there's a lot of "suck it and see", so have a go and let us know if it works!

Hi Nige_S

Thank you for answering my question and producing the demo, which I have tried.

I will respond to your second reply but in the meantime for completeness, this is a screen dump of my Bluetooth menu.

Screenshot 2023-03-21 at 12.02.14

A post was split to a new topic: Ventura – Terminal Won't Quit Via AppleScript

So, assuming you still want to toggle your mouse's connection and that is always the second item, change the middle line of the above AppleScript to

click checkbox 2 of scroll area 1 of group 1 of window "Control Centre"

If the order of devices can change -- you're stuck. Ventura Accessibility is currently so broken that there's no distinguishing values available to use. So you're options are to either use KM's "Click on found image" to click at a point offset to the left of the item's text (in which case, do the whole procedure in KM rather than AppleScript) or to install a third-party command line utility such as blueutil or bluetoothctl (I have no experience of either, so won't link to them -- they're just the first two that popped up in a Google search) and drive those with a KM "Execute Shell Script" action.

In searching for a solution to automatically connect to my BT speaker bar, I found your helpful script from November 2019. The menu bar has changed (as it relates to switching sound output). Is there a way that we can modify this script to connect to my speaker bar? I would think that instead of the word "mouse," we'd tell the script to look for the word "bar."

Thank you!

Hi

When I updated to Ventura my AppleScript stopped working but finally resolved the problem with this :-

tell application "System Events"
	tell process "ControlCenter"
		set theMenu to item 1 of (get every menu bar item of menu bar 1 whose description is "Bluetooth")
		click theMenu
		click checkbox 2 of scroll area 1 of group 1 of window "Control Centre"
		delay 1
		click theMenu
	end tell
end tell

and my macOS menu looks like this :-
BlueTooth Meno Screenshot

Hope this helps, Andrew

Thank you for this. It worked, but I will note two important caveats (mainly for anyone who comes across this later and wants to do the same thing).

  1. The Bluetooth status menu has to be visible in the menu bar, not just accessible via Control Center. So, in the Control Center settings, you need to select "Show in Menu Bar" for Bluetooth.
    2.The second instance of "Control Center" is misspelled, so I think if you were to run the script with the misspelling, it wouldn't work.

My only concern here is that this script is dependent on my hardware (speaker bar in my case) being in a certain order in the menu list. If there was a way to look for the "checkbox" with the name of my speaker bar, that would be ideal. But, I am not at all a programmer (other than KM!), so I don't know how to do this or if it is even possible.

Thanks for your comments which has prompted me to do more testing.

Item 1/ Can you please give example snippet if you have added "So, in the Control Center settings, you need to select "Show in Menu Bar". I did try searching for the mouse in my original script but that didn't work, also the mouse is always listed second so if I add another BlueTooth device I will have to tweak the script.

2/ I have tried all combinations of the spellings of "Control Center" and only the version I posted works but I am not a KM programmer only Apple Swift.