Flipping a Switch in AppleScript (or Directly in Keyboard Maestro)?

I wrote a macro for Bear that changes the sorting order of my notes from reverse chronological to reverse alphabetical. I had to use a combination of Keyboard Maestro and AppleScript (first time ever using both!) that feels unnecessarily hacky, and I was wondering if folks could help me clean up my macro.

The macro opens the Preferences window from the Bear menu via Keyboard Maestro, clicks the second popup menu ("Notes List Sorting") and selects "Title" via AppleScript, then clicks the switch, "A to Z," using a Keyboard Maestro mouse action.

Here's what the Preferences window looks like:

And here's my AppleScript for selecting "Title" from the popup menu:

tell application "System Events"
	tell process "Bear"
		set frontmost to true
		tell window "Preferences"
			tell group 1
				tell pop up button 1
					perform action "AXShowMenu"
					click menu item "Title" of menu 1
				end tell
			end tell
		end tell
	end tell
end tell

I have two questions:

  1. Can I click the "A to Z" switch directly in AppleScript (rather than using the KM mouse action)? I tried using Accessibility Inspector to find an element for the switch, but hovering over it doesn't reveal anything, and a Google search didn't turn up anything either.

  2. Alternatively, can I rewrite the whole macro solely in Keyboard Maestro without using mouse actions?

Thanks for your help!

Perhaps. If you use KM to turn on some features Accessibility Mode, which has some abilities to let users use the keyboard instead of a mouse, you may be able to find keyboard equivalents for nearly any program that doesn't directly support the keyboard.

I really have no idea if you will like this solution, but I think it's a potential solution, so I'm mentioning it.

1 Like

Unfortunately not – System Events doesn’t see any of those slider widgets – at least on Mojave.

When Change the way Tab moves focus is on in the macOS system keyboard preferences Tab will in fact select those widgets, so macOS knows about them – but it appears accessibility via AppleScript is a problem.

I think this is most likely Apple's fault (Bad Apple!), but it's just barely possible that accessibility hasn't been done properly by the developer.

If you turn on Change the way Tab moves focus then you can tab around and use the spacebar to toggle the slider buttons.

But you're probably better off using Click at Found Image action.

In any case – this task should be doable and pretty fast, however it'll be a trifle kludgey any way you look at it. Brute-forcing the UI is frequently a little awkward, but it's nearly always waaay faster than doing the job by hand.


1 Like

I'm posting a screenshot of where that feature is visible in macOS, because it took me over 5 minutes to find it:

There is some similar behavior in Accessibility.


This is super helpful @ccstone. Thanks for letting me know! And thank you @Sleepy for the quick reference to the accessibility settings!

1 Like