Connect a Bluetooth Mouse Using a Macro


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?


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
		tell menu 1
			if exists menu item "Connect" then
				click menu item "Connect"
				say "Connected"
				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.


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!


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.


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.


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.


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,

(Keyboard Maestro Moderator)

Hi Chris

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


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)


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:


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